diff --git a/.travis.yml b/.travis.yml index 92d42d8f3f..ac2162d25c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/futures-channel/Cargo.toml b/futures-channel/Cargo.toml index 98eb597758..b0c3efc57a 100644 --- a/futures-channel/Cargo.toml +++ b/futures-channel/Cargo.toml @@ -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 } diff --git a/futures-channel/src/lib.rs b/futures-channel/src/lib.rs index b931a9e7ac..bdc5c1be8c 100644 --- a/futures-channel/src/lib.rs +++ b/futures-channel/src/lib.rs @@ -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)] @@ -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; +} diff --git a/futures-channel/src/oneshot.rs b/futures-channel/src/oneshot.rs index ca05adf73c..87df6b8b8c 100644 --- a/futures-channel/src/oneshot.rs +++ b/futures-channel/src/oneshot.rs @@ -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; @@ -387,7 +386,8 @@ impl fmt::Display for Canceled { } } -impl Error for Canceled {} +#[cfg(feature = "std")] +impl std::error::Error for Canceled {} impl Receiver { /// Gracefully close this receiver, preventing any subsequent attempts to diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 99df214c06..b598bfb6fc 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -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 diff --git a/futures/src/lib.rs b/futures/src/lib.rs index 68dd2a9385..2a8b65c65f 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -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. //! @@ -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")]