Skip to content

Commit

Permalink
Support oneshot in no_std environment
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jul 20, 2019
1 parent 2c54502 commit f58c0d7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 23 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ matrix:
- cargo build --manifest-path futures/Cargo.toml --no-default-features --features alloc
- cargo build --manifest-path futures-core/Cargo.toml --no-default-features --features alloc
- cargo build --manifest-path futures-sink/Cargo.toml --no-default-features --features alloc
- cargo build --manifest-path futures-channel/Cargo.toml --no-default-features --features alloc
- cargo build --manifest-path futures-util/Cargo.toml --no-default-features --features alloc

- name: cargo build --target=thumbv6m-none-eabi
Expand Down
7 changes: 5 additions & 2 deletions futures-channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ Channels for asynchronous communication using futures-rs.
name = "futures_channel"

[features]
std = ["futures-core-preview/std"]
sink = ["futures-sink-preview"]
default = ["std"]
std = ["alloc", "futures-core-preview/std"]
alloc = ["futures-core-preview/alloc"]
sink = ["futures-sink-preview"]
nightly = ["futures-core-preview/nightly"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic"]

[dependencies]
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.17", default-features = false }
Expand Down
34 changes: 27 additions & 7 deletions futures-channel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//! This crate provides channels that can be used to communicate between
//! asynchronous tasks.
//!
//! All items of this library are only available when the `std` feature of this
//! All items of this library are only available when the `std` or `alloc` feature of this
//! library is activated, and it is activated by default.

#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]

#![cfg_attr(not(feature = "std"), no_std)]

#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
Expand All @@ -17,9 +19,27 @@

#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.17/futures_channel")]

#[cfg(feature = "std")]
mod lock;
#[cfg(feature = "std")]
pub mod mpsc;
#[cfg(feature = "std")]
pub mod oneshot;
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");

macro_rules! cfg_target_has_atomic {
($($item:item)*) => {$(
#[cfg_attr(
feature = "cfg-target-has-atomic",
cfg(all(target_has_atomic = "cas", target_has_atomic = "ptr"))
)]
$item
)*};
}

cfg_target_has_atomic! {
#[cfg(feature = "alloc")]
extern crate alloc;

#[cfg(feature = "alloc")]
mod lock;
#[cfg(feature = "std")]
pub mod mpsc;
#[cfg(feature = "alloc")]
pub mod oneshot;
}
14 changes: 7 additions & 7 deletions futures-channel/src/oneshot.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! A channel for sending a single message between asynchronous tasks.

use alloc::sync::Arc;
use core::fmt;
use core::pin::Pin;
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering::SeqCst;
use futures_core::future::Future;
use futures_core::task::{Context, Poll, Waker};
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::error::Error;
use std::fmt;

use crate::lock::Lock;

Expand Down Expand Up @@ -387,7 +386,8 @@ impl fmt::Display for Canceled {
}
}

impl Error for Canceled {}
#[cfg(feature = "std")]
impl std::error::Error for Canceled {}

impl<T> Receiver<T> {
/// Gracefully close this receiver, preventing any subsequent attempts to
Expand Down
8 changes: 4 additions & 4 deletions futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ tokio = "0.1.11"
assert_matches = "1.3.0"

[features]
nightly = ["futures-core-preview/nightly", "futures-util-preview/nightly"]
default = ["std"]
std = ["alloc", "futures-core-preview/std", "futures-executor-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-util-preview/std", "futures-util-preview/io", "futures-util-preview/channel"]
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-channel-preview/alloc", "futures-util-preview/alloc"]
nightly = ["futures-core-preview/nightly", "futures-channel-preview/nightly", "futures-util-preview/nightly"]
async-await = ["futures-util-preview/async-await", "futures-util-preview/select-macro"]
default = ["std"]
compat = ["std", "futures-util-preview/compat"]
io-compat = ["compat", "futures-util-preview/io-compat"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic", "futures-util-preview/cfg-target-has-atomic"]
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-util-preview/alloc"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic", "futures-channel-preview/cfg-target-has-atomic", "futures-util-preview/cfg-target-has-atomic"]

[package.metadata.docs.rs]
all-features = true
13 changes: 10 additions & 3 deletions futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ pub use futures_util::{
join, try_join, pending, poll,
};

#[cfg(feature = "std")]
#[cfg_attr(
feature = "cfg-target-has-atomic",
cfg(all(target_has_atomic = "cas", target_has_atomic = "ptr"))
)]
#[cfg(feature = "alloc")]
pub mod channel {
//! Cross-task communication.
//!
Expand All @@ -86,10 +90,13 @@ pub mod channel {
//! channel for sending values between tasks, analogous to the
//! similarly-named structure in the standard library.
//!
//! This module is only available when the `std` feature of this
//! This module is only available when the `std` or `alloc` feature of this
//! library is activated, and it is activated by default.

pub use futures_channel::{oneshot, mpsc};
pub use futures_channel::oneshot;

#[cfg(feature = "std")]
pub use futures_channel::mpsc;
}

#[cfg(feature = "compat")]
Expand Down

0 comments on commit f58c0d7

Please sign in to comment.