From a822c5e0d061f5c643c1da78f28cf652efea7263 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 29 Jan 2019 21:49:13 +0900 Subject: [PATCH] Add no_std + alloc support --- .travis.yml | 8 ++++ futures-core/Cargo.toml | 4 +- futures-core/src/future/future_obj.rs | 7 +-- futures-core/src/future/mod.rs | 7 ++- futures-core/src/lib.rs | 4 ++ futures-core/src/stream/mod.rs | 9 ++-- futures-core/src/stream/stream_obj.rs | 8 ++-- futures-core/src/task/mod.rs | 4 +- futures-sink/Cargo.toml | 5 +- futures-sink/src/lib.rs | 18 +++++--- futures-util/Cargo.toml | 6 ++- futures-util/src/future/abortable.rs | 6 +-- futures-util/src/future/join_all.rs | 16 +++---- futures-util/src/future/mod.rs | 12 +++-- futures-util/src/lib.rs | 6 ++- futures-util/src/lock/bilock.rs | 21 +++++---- futures-util/src/lock/mod.rs | 2 + futures-util/src/sink/buffer.rs | 4 +- futures-util/src/sink/mod.rs | 6 +-- futures-util/src/stream/buffer_unordered.rs | 4 +- futures-util/src/stream/buffered.rs | 4 +- futures-util/src/stream/chunks.rs | 6 +-- futures-util/src/stream/futures_ordered.rs | 10 ++-- .../src/stream/futures_unordered/iter.rs | 4 +- .../src/stream/futures_unordered/mod.rs | 22 ++++----- .../futures_unordered/ready_to_run_queue.rs | 10 ++-- .../src/stream/futures_unordered/task.rs | 14 +++--- futures-util/src/stream/mod.rs | 46 ++++++++++--------- futures-util/src/stream/select_all.rs | 4 +- futures-util/src/stream/split.rs | 7 ++- futures-util/src/task/local_waker_ref.rs | 10 ++-- futures-util/src/task/mod.rs | 4 +- futures-util/src/task/spawn.rs | 10 ++-- futures-util/src/try_future/mod.rs | 4 +- futures-util/src/try_future/try_join_all.rs | 16 +++---- futures-util/src/try_stream/mod.rs | 20 ++++---- .../src/try_stream/try_buffer_unordered.rs | 2 +- futures-util/src/try_stream/try_collect.rs | 5 +- futures/Cargo.toml | 5 +- futures/src/lib.rs | 32 +++++++++---- 40 files changed, 225 insertions(+), 167 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3ef7820b62..818c79acc6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,6 +54,14 @@ matrix: - cargo build --manifest-path futures-sink/Cargo.toml --no-default-features - cargo build --manifest-path futures-util/Cargo.toml --no-default-features + - name: cargo build (alloc) + rust: nightly + script: + - cargo build --manifest-path futures/Cargo.toml --no-default-features --features=alloc,nightly + - cargo build --manifest-path futures-core/Cargo.toml --no-default-features --features=alloc,nightly + - cargo build --manifest-path futures-sink/Cargo.toml --no-default-features --features=alloc,nightly + - cargo build --manifest-path futures-util/Cargo.toml --no-default-features --features=alloc,nightly + - name: cargo build (default features) rust: nightly script: diff --git a/futures-core/Cargo.toml b/futures-core/Cargo.toml index ffe8627d70..069828dec1 100644 --- a/futures-core/Cargo.toml +++ b/futures-core/Cargo.toml @@ -16,11 +16,13 @@ name = "futures_core" [features] default = ["std"] -std = ["either/use_std"] +std = ["alloc", "either/use_std", "alloc-shim/std"] nightly = [] +alloc = ["alloc-shim/alloc"] [dependencies] either = { version = "1.4", default-features = false, optional = true } +alloc-shim = { version = "0.3.0", features = ["futures"], optional = true } [dev-dependencies] futures-preview = { path = "../futures", version = "=0.3.0-alpha.12" } diff --git a/futures-core/src/future/future_obj.rs b/futures-core/src/future/future_obj.rs index 3a2d2aa2c4..36c3348888 100644 --- a/futures-core/src/future/future_obj.rs +++ b/futures-core/src/future/future_obj.rs @@ -185,10 +185,11 @@ where unsafe fn drop(_ptr: *mut ()) {} } -#[cfg(feature = "std")] -mod if_std { +#[cfg(feature = "alloc")] +mod if_alloc { use super::*; - use std::mem; + use core::mem; + use alloc::boxed::Box; unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box where F: Future + 'a diff --git a/futures-core/src/future/mod.rs b/futures-core/src/future/mod.rs index 8675c57551..93b96f2e2e 100644 --- a/futures-core/src/future/mod.rs +++ b/futures-core/src/future/mod.rs @@ -33,9 +33,11 @@ impl FusedFuture for &mut F { } } -#[cfg(feature = "std")] -mod if_std { +#[cfg(feature = "alloc")] +mod if_alloc { + use alloc::boxed::Box; use super::*; + impl FusedFuture for Box { fn is_terminated(&self) -> bool { ::is_terminated(&**self) @@ -48,6 +50,7 @@ mod if_std { } } + #[cfg(feature = "std")] impl FusedFuture for std::panic::AssertUnwindSafe { fn is_terminated(&self) -> bool { ::is_terminated(&**self) diff --git a/futures-core/src/lib.rs b/futures-core/src/lib.rs index 78cbfca4d5..1bb2c56b32 100644 --- a/futures-core/src/lib.rs +++ b/futures-core/src/lib.rs @@ -2,6 +2,7 @@ #![feature(futures_api)] #![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))] +#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] #![cfg_attr(not(feature = "std"), no_std)] @@ -10,6 +11,9 @@ #![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.12/futures_core")] +#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))] +compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features"); + pub mod future; #[doc(hidden)] pub use self::future::{Future, FusedFuture, TryFuture}; diff --git a/futures-core/src/stream/mod.rs b/futures-core/src/stream/mod.rs index 193b99a690..0980915004 100644 --- a/futures-core/src/stream/mod.rs +++ b/futures-core/src/stream/mod.rs @@ -145,9 +145,9 @@ impl TryStream for S } } -#[cfg(feature = "std")] -mod if_std { - use std::boxed::Box; +#[cfg(feature = "alloc")] +mod if_alloc { + use alloc::boxed::Box; use super::*; impl Stream for Box { @@ -161,6 +161,7 @@ mod if_std { } } + #[cfg(feature = "std")] impl Stream for ::std::panic::AssertUnwindSafe { type Item = S::Item; @@ -172,7 +173,7 @@ mod if_std { } } - impl Stream for ::std::collections::VecDeque { + impl Stream for ::alloc::collections::VecDeque { type Item = T; fn poll_next( diff --git a/futures-core/src/stream/stream_obj.rs b/futures-core/src/stream/stream_obj.rs index a83980c1e2..f1a5166a2c 100644 --- a/futures-core/src/stream/stream_obj.rs +++ b/futures-core/src/stream/stream_obj.rs @@ -2,7 +2,6 @@ use super::Stream; use crate::task::{LocalWaker, Poll}; use core::fmt; use core::marker::PhantomData; -use core::mem; use core::pin::Pin; /// A custom trait object for polling streams, roughly akin to @@ -188,10 +187,11 @@ where unsafe fn drop(_ptr: *mut ()) {} } -#[cfg(feature = "std")] -mod if_std { - use std::boxed::Box; +#[cfg(feature = "alloc")] +mod if_alloc { use super::*; + use core::mem; + use alloc::boxed::Box; unsafe impl<'a, T, F> UnsafeStreamObj<'a, T> for Box where F: Stream + 'a diff --git a/futures-core/src/task/mod.rs b/futures-core/src/task/mod.rs index 95dc3cb57f..45d512359a 100644 --- a/futures-core/src/task/mod.rs +++ b/futures-core/src/task/mod.rs @@ -6,5 +6,5 @@ pub mod __internal; pub use self::spawn::{Spawn, LocalSpawn, SpawnError}; pub use core::task::{Poll, Waker, LocalWaker, UnsafeWake}; -#[cfg(feature = "std")] -pub use std::task::{Wake, local_waker, local_waker_from_nonlocal}; +#[cfg(feature = "alloc")] +pub use alloc::task::{Wake, local_waker, local_waker_from_nonlocal}; diff --git a/futures-sink/Cargo.toml b/futures-sink/Cargo.toml index bbf0a4a9b6..b310c5b8bb 100644 --- a/futures-sink/Cargo.toml +++ b/futures-sink/Cargo.toml @@ -15,10 +15,13 @@ The asynchronous `Sink` trait for the futures-rs library. name = "futures_sink" [features] -std = ["either/use_std", "futures-core-preview/std", "futures-channel-preview/std"] +std = ["alloc", "either/use_std", "futures-core-preview/std", "futures-channel-preview/std", "alloc-shim/std"] default = ["std"] +nightly = ["futures-core-preview/nightly"] +alloc = ["futures-core-preview/alloc", "alloc-shim/alloc"] [dependencies] either = { version = "1.4", default-features = false, optional = true } futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.12", default-features = false } futures-channel-preview = { path = "../futures-channel", version = "=0.3.0-alpha.12", default-features = false } +alloc-shim = { version = "0.3.0", features = ["futures"], optional = true } diff --git a/futures-sink/src/lib.rs b/futures-sink/src/lib.rs index 180f534963..7457e45480 100644 --- a/futures-sink/src/lib.rs +++ b/futures-sink/src/lib.rs @@ -8,6 +8,10 @@ #![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.12/futures_sink")] #![feature(futures_api)] +#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] + +#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))] +compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features"); use futures_core::task::{LocalWaker, Poll}; use core::pin::Pin; @@ -155,8 +159,8 @@ impl<'a, S: ?Sized + Sink> Sink for Pin<&'a mut S> { #[cfg(feature = "std")] mod channel_impls; -#[cfg(feature = "std")] -mod if_std { +#[cfg(feature = "alloc")] +mod if_alloc { use super::*; /// The error type for `Vec` and `VecDequeue` when used as `Sink`s. @@ -164,7 +168,7 @@ mod if_std { #[derive(Copy, Clone, Debug)] pub enum VecSinkError {} - impl Sink for ::std::vec::Vec { + impl Sink for ::alloc::vec::Vec { type SinkItem = T; type SinkError = VecSinkError; @@ -187,7 +191,7 @@ mod if_std { } } - impl Sink for ::std::collections::VecDeque { + impl Sink for ::alloc::collections::VecDeque { type SinkItem = T; type SinkError = VecSinkError; @@ -210,7 +214,7 @@ mod if_std { } } - impl Sink for ::std::boxed::Box { + impl Sink for ::alloc::boxed::Box { type SinkItem = S::SinkItem; type SinkError = S::SinkError; @@ -232,8 +236,8 @@ mod if_std { } } -#[cfg(feature = "std")] -pub use self::if_std::*; +#[cfg(feature = "alloc")] +pub use self::if_alloc::*; #[cfg(feature = "either")] use either::Either; diff --git a/futures-util/Cargo.toml b/futures-util/Cargo.toml index 5c59ee3708..8bae13cca0 100644 --- a/futures-util/Cargo.toml +++ b/futures-util/Cargo.toml @@ -15,12 +15,13 @@ Common utilities and extension traits for the futures-rs library. name = "futures_util" [features] -std = ["futures-core-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-select-macro-preview/std", "either/use_std", "rand", "rand_core", "slab"] +std = ["alloc", "futures-core-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-select-macro-preview/std", "either/use_std", "rand", "rand_core", "slab", "alloc-shim/std"] default = ["std", "futures-core-preview/either", "futures-sink-preview/either"] compat = ["std", "futures_01"] io-compat = ["compat", "tokio-io"] bench = [] -nightly = [] +nightly = ["futures-core-preview/nightly", "futures-sink-preview/nightly"] +alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "alloc-shim/alloc"] [dependencies] futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.12", default-features = false } @@ -37,6 +38,7 @@ slab = { version = "0.4", optional = true } futures_01 = { version = "0.1.25", optional = true, package = "futures" } tokio-io = { version = "0.1.9", optional = true } pin-utils = "0.1.0-alpha.4" +alloc-shim = { version = "0.3.0", features = ["futures"], optional = true } [dev-dependencies] futures-preview = { path = "../futures", version = "=0.3.0-alpha.12" } diff --git a/futures-util/src/future/abortable.rs b/futures-util/src/future/abortable.rs index 91696ed666..1cc118f05c 100644 --- a/futures-util/src/future/abortable.rs +++ b/futures-util/src/future/abortable.rs @@ -2,9 +2,9 @@ use crate::task::AtomicWaker; use futures_core::future::Future; use futures_core::task::{LocalWaker, Poll}; use pin_utils::unsafe_pinned; -use std::pin::Pin; -use std::sync::Arc; -use std::sync::atomic::{AtomicBool, Ordering}; +use core::pin::Pin; +use core::sync::atomic::{AtomicBool, Ordering}; +use alloc::sync::Arc; /// A future which can be remotely short-circuited using an `AbortHandle`. #[derive(Debug, Clone)] diff --git a/futures-util/src/future/join_all.rs b/futures-util/src/future/join_all.rs index 5512eed6e2..2945d3c23d 100644 --- a/futures-util/src/future/join_all.rs +++ b/futures-util/src/future/join_all.rs @@ -1,13 +1,13 @@ //! Definition of the `JoinAll` combinator, waiting for all of a list of futures //! to finish. -use std::fmt; -use std::future::Future; -use std::iter::FromIterator; -use std::mem; -use std::pin::Pin; -use std::prelude::v1::*; -use std::task::Poll; +use core::fmt; +use core::future::Future; +use core::iter::FromIterator; +use core::mem; +use core::pin::Pin; +use alloc::prelude::*; +use alloc::task::Poll; #[derive(Debug)] enum ElemState @@ -129,7 +129,7 @@ where fn poll( mut self: Pin<&mut Self>, - lw: &::std::task::LocalWaker, + lw: &::alloc::task::LocalWaker, ) -> Poll { let mut all_done = true; diff --git a/futures-util/src/future/mod.rs b/futures-util/src/future/mod.rs index d21e44eed4..db31e8b6c9 100644 --- a/futures-util/src/future/mod.rs +++ b/futures-util/src/future/mod.rs @@ -7,6 +7,8 @@ use core::pin::Pin; use futures_core::future::Future; use futures_core::stream::Stream; use futures_core::task::{LocalWaker, Poll}; +#[cfg(feature = "alloc")] +use alloc::prelude::*; // re-export for `select!` #[doc(hidden)] @@ -67,9 +69,9 @@ pub use self::unit_error::UnitError; mod chain; pub(crate) use self::chain::Chain; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod abortable; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::abortable::{abortable, Abortable, AbortHandle, AbortRegistration, Aborted}; #[cfg(feature = "std")] @@ -82,10 +84,10 @@ mod remote_handle; #[cfg(feature = "std")] pub use self::remote_handle::{Remote, RemoteHandle}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod join_all; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::join_all::{join_all, JoinAll}; // #[cfg(feature = "std")] @@ -653,7 +655,7 @@ pub trait FutureExt: Future { } /// Wrap the future in a Box, pinning it. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn boxed(self) -> Pin> where Self: Sized { diff --git a/futures-util/src/lib.rs b/futures-util/src/lib.rs index b27adb4add..333e75ee71 100644 --- a/futures-util/src/lib.rs +++ b/futures-util/src/lib.rs @@ -4,6 +4,7 @@ #![feature(futures_api, box_into_pin)] #![cfg_attr(feature = "std", feature(async_await, await_macro))] #![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))] +#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] #![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs, missing_debug_implementations)] @@ -11,6 +12,9 @@ #![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.12/futures_util")] +#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))] +compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features"); + #[macro_use] mod macros; @@ -89,5 +93,5 @@ pub mod io; #[cfg(feature = "std")] #[doc(hidden)] pub use crate::io::{AsyncReadExt, AsyncWriteExt}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub mod lock; diff --git a/futures-util/src/lock/bilock.rs b/futures-util/src/lock/bilock.rs index f5d8c61c9f..8a7437471c 100644 --- a/futures-util/src/lock/bilock.rs +++ b/futures-util/src/lock/bilock.rs @@ -3,17 +3,19 @@ use futures_core::future::Future; use futures_core::task::{LocalWaker, Poll, Waker}; +use core::cell::UnsafeCell; +use core::fmt; +use core::mem; +use core::ops::{Deref, DerefMut}; +use core::pin::Pin; +use core::sync::atomic::AtomicUsize; +use core::sync::atomic::Ordering::SeqCst; +use alloc::boxed::Box; +use alloc::sync::Arc; +#[cfg(feature = "std")] use std::any::Any; -use std::boxed::Box; -use std::cell::UnsafeCell; +#[cfg(feature = "std")] use std::error::Error; -use std::fmt; -use std::mem; -use std::ops::{Deref, DerefMut}; -use std::pin::Pin; -use std::sync::Arc; -use std::sync::atomic::AtomicUsize; -use std::sync::atomic::Ordering::SeqCst; /// A type of futures-powered synchronization primitive which is a mutex between /// two possible owners. @@ -210,6 +212,7 @@ impl fmt::Display for ReuniteError { } } +#[cfg(feature = "std")] impl Error for ReuniteError { fn description(&self) -> &str { "tried to reunite two BiLocks that don't form a pair" diff --git a/futures-util/src/lock/mod.rs b/futures-util/src/lock/mod.rs index e768c22332..38a5537724 100644 --- a/futures-util/src/lock/mod.rs +++ b/futures-util/src/lock/mod.rs @@ -1,6 +1,8 @@ //! Futures-powered synchronization primitives. +#[cfg(feature = "std")] mod mutex; +#[cfg(feature = "std")] pub use self::mutex::{Mutex, MutexLockFuture, MutexGuard}; mod bilock; diff --git a/futures-util/src/sink/buffer.rs b/futures-util/src/sink/buffer.rs index a715903cf5..61984c84b3 100644 --- a/futures-util/src/sink/buffer.rs +++ b/futures-util/src/sink/buffer.rs @@ -2,8 +2,8 @@ use futures_core::stream::Stream; use futures_core::task::{LocalWaker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::collections::VecDeque; -use std::pin::Pin; +use core::pin::Pin; +use alloc::collections::VecDeque; /// Sink for the `Sink::buffer` combinator, which buffers up to some fixed /// number of values when the underlying sink is unable to accept them. diff --git a/futures-util/src/sink/mod.rs b/futures-util/src/sink/mod.rs index bde6d241d6..684735898b 100644 --- a/futures-util/src/sink/mod.rs +++ b/futures-util/src/sink/mod.rs @@ -41,9 +41,9 @@ pub use self::with::With; mod with_flat_map; pub use self::with_flat_map::WithFlatMap; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod buffer; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::buffer::Buffer; impl SinkExt for T where T: Sink {} @@ -156,7 +156,7 @@ pub trait SinkExt: Sink { /// /// This method is only available when the `std` feature of this /// library is activated, and it is activated by default. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn buffer(self, capacity: usize) -> Buffer where Self: Sized, { diff --git a/futures-util/src/stream/buffer_unordered.rs b/futures-util/src/stream/buffer_unordered.rs index d6b98dc8a9..d21f1c0321 100644 --- a/futures-util/src/stream/buffer_unordered.rs +++ b/futures-util/src/stream/buffer_unordered.rs @@ -4,8 +4,8 @@ use futures_core::stream::{Stream, FusedStream}; use futures_core::task::{LocalWaker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::fmt; -use std::pin::Pin; +use core::fmt; +use core::pin::Pin; /// An adaptor for a stream of futures to execute the futures concurrently, if /// possible, delivering results as they become available. diff --git a/futures-util/src/stream/buffered.rs b/futures-util/src/stream/buffered.rs index 284fff4abf..5acc9fbf7b 100644 --- a/futures-util/src/stream/buffered.rs +++ b/futures-util/src/stream/buffered.rs @@ -4,8 +4,8 @@ use futures_core::stream::Stream; use futures_core::task::{LocalWaker, Poll}; use futures_sink::Sink; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::fmt; -use std::pin::Pin; +use core::fmt; +use core::pin::Pin; /// An adaptor for a stream of futures to execute the futures concurrently, if /// possible. diff --git a/futures-util/src/stream/chunks.rs b/futures-util/src/stream/chunks.rs index f6407ab8c1..39888f2f63 100644 --- a/futures-util/src/stream/chunks.rs +++ b/futures-util/src/stream/chunks.rs @@ -2,9 +2,9 @@ use crate::stream::Fuse; use futures_core::stream::Stream; use futures_core::task::{LocalWaker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::mem; -use std::pin::Pin; -use std::prelude::v1::*; +use core::mem; +use core::pin::Pin; +use alloc::prelude::*; /// An adaptor that chunks up elements in a vector. /// diff --git a/futures-util/src/stream/futures_ordered.rs b/futures-util/src/stream/futures_ordered.rs index a2f6912238..53c9f16c7b 100644 --- a/futures-util/src/stream/futures_ordered.rs +++ b/futures-util/src/stream/futures_ordered.rs @@ -3,11 +3,11 @@ use futures_core::future::Future; use futures_core::stream::Stream; use futures_core::task::{LocalWaker, Poll}; use pin_utils::unsafe_pinned; -use std::cmp::{Eq, PartialEq, PartialOrd, Ord, Ordering}; -use std::collections::binary_heap::{BinaryHeap, PeekMut}; -use std::fmt::{self, Debug}; -use std::iter::FromIterator; -use std::pin::Pin; +use core::cmp::{Eq, PartialEq, PartialOrd, Ord, Ordering}; +use core::fmt::{self, Debug}; +use core::iter::FromIterator; +use core::pin::Pin; +use alloc::collections::binary_heap::{BinaryHeap, PeekMut}; #[must_use = "futures do nothing unless polled"] #[derive(Debug)] diff --git a/futures-util/src/stream/futures_unordered/iter.rs b/futures-util/src/stream/futures_unordered/iter.rs index 1eef3e5265..b03e10bb2d 100644 --- a/futures-util/src/stream/futures_unordered/iter.rs +++ b/futures-util/src/stream/futures_unordered/iter.rs @@ -1,7 +1,7 @@ use super::FuturesUnordered; use super::task::Task; -use std::marker::PhantomData; -use std::pin::Pin; +use core::marker::PhantomData; +use core::pin::Pin; #[derive(Debug)] /// Mutable iterator over all futures in the unordered set. diff --git a/futures-util/src/stream/futures_unordered/mod.rs b/futures-util/src/stream/futures_unordered/mod.rs index 27b7d132ae..d1f63005fa 100644 --- a/futures-util/src/stream/futures_unordered/mod.rs +++ b/futures-util/src/stream/futures_unordered/mod.rs @@ -4,17 +4,17 @@ use crate::task::AtomicWaker; use futures_core::future::{Future, FutureObj, LocalFutureObj}; use futures_core::stream::{FusedStream, Stream}; use futures_core::task::{LocalWaker, Poll, Spawn, LocalSpawn, SpawnError}; -use std::cell::UnsafeCell; -use std::fmt::{self, Debug}; -use std::iter::FromIterator; -use std::marker::PhantomData; -use std::mem; -use std::pin::Pin; -use std::ptr; -use std::sync::atomic::Ordering::SeqCst; -use std::sync::atomic::{AtomicPtr, AtomicBool}; -use std::sync::{Arc, Weak}; -use std::usize; +use core::cell::UnsafeCell; +use core::fmt::{self, Debug}; +use core::iter::FromIterator; +use core::marker::PhantomData; +use core::mem; +use core::pin::Pin; +use core::ptr; +use core::sync::atomic::Ordering::SeqCst; +use core::sync::atomic::{AtomicPtr, AtomicBool}; +use core::usize; +use alloc::sync::{Arc, Weak}; mod abort; diff --git a/futures-util/src/stream/futures_unordered/ready_to_run_queue.rs b/futures-util/src/stream/futures_unordered/ready_to_run_queue.rs index f71b8ff877..210519585e 100644 --- a/futures-util/src/stream/futures_unordered/ready_to_run_queue.rs +++ b/futures-util/src/stream/futures_unordered/ready_to_run_queue.rs @@ -1,9 +1,9 @@ use crate::task::AtomicWaker; -use std::cell::UnsafeCell; -use std::ptr; -use std::sync::Arc; -use std::sync::atomic::AtomicPtr; -use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; +use core::cell::UnsafeCell; +use core::ptr; +use core::sync::atomic::AtomicPtr; +use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; +use alloc::sync::Arc; use super::abort::abort; use super::task::Task; diff --git a/futures-util/src/stream/futures_unordered/task.rs b/futures-util/src/stream/futures_unordered/task.rs index 8acf331419..ff6aafa4d6 100644 --- a/futures-util/src/stream/futures_unordered/task.rs +++ b/futures-util/src/stream/futures_unordered/task.rs @@ -1,10 +1,10 @@ -use std::cell::UnsafeCell; -use std::marker::PhantomData; -use std::mem; -use std::ptr::{self, NonNull}; -use std::sync::{Arc, Weak}; -use std::sync::atomic::{AtomicPtr, AtomicBool}; -use std::sync::atomic::Ordering::SeqCst; +use core::cell::UnsafeCell; +use core::marker::PhantomData; +use core::mem; +use core::ptr::{self, NonNull}; +use core::sync::atomic::{AtomicPtr, AtomicBool}; +use core::sync::atomic::Ordering::SeqCst; +use alloc::sync::{Arc, Weak}; use futures_core::task::{UnsafeWake, Waker, LocalWaker}; diff --git a/futures-util/src/stream/mod.rs b/futures-util/src/stream/mod.rs index a410773e37..6ca7d864e5 100644 --- a/futures-util/src/stream/mod.rs +++ b/futures-util/src/stream/mod.rs @@ -9,6 +9,8 @@ use futures_core::future::Future; use futures_core::stream::{FusedStream, Stream}; use futures_core::task::{LocalWaker, Poll}; use futures_sink::Sink; +#[cfg(feature = "alloc")] +use alloc::prelude::*; mod iter; pub use self::iter::{iter, Iter}; @@ -100,14 +102,14 @@ pub use self::zip::Zip; #[cfg(feature = "std")] use std; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod buffer_unordered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::buffer_unordered::BufferUnordered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod buffered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::buffered::Buffered; #[cfg(feature = "std")] @@ -115,34 +117,34 @@ mod catch_unwind; #[cfg(feature = "std")] pub use self::catch_unwind::CatchUnwind; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod chunks; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::chunks::Chunks; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod for_each_concurrent; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::for_each_concurrent::ForEachConcurrent; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod futures_ordered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::futures_ordered::{futures_ordered, FuturesOrdered}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod futures_unordered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::futures_unordered::{futures_unordered, FuturesUnordered}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod split; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::split::{SplitStream, SplitSink, ReuniteError}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod select_all; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::select_all::{select_all, SelectAll}; impl StreamExt for T where T: Stream {} @@ -614,7 +616,7 @@ pub trait StreamExt: Stream { /// await!(fut); /// # }) /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn for_each_concurrent( self, limit: impl Into>, @@ -791,7 +793,7 @@ pub trait StreamExt: Stream { } /// Wrap the stream in a Box, pinning it. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn boxed(self) -> Pin> where Self: Sized { @@ -810,7 +812,7 @@ pub trait StreamExt: Stream { /// /// This method is only available when the `std` feature of this /// library is activated, and it is activated by default. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn buffered(self, n: usize) -> Buffered where Self::Item: Future, Self: Sized @@ -854,7 +856,7 @@ pub trait StreamExt: Stream { /// assert_eq!(await!(buffered.next()), None); /// # }) /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn buffer_unordered(self, n: usize) -> BufferUnordered where Self::Item: Future, Self: Sized @@ -945,7 +947,7 @@ pub trait StreamExt: Stream { /// # Panics /// /// This method will panic of `capacity` is zero. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn chunks(self, capacity: usize) -> Chunks where Self: Sized { @@ -999,7 +1001,7 @@ pub trait StreamExt: Stream { /// /// This method is only available when the `std` feature of this /// library is activated, and it is activated by default. - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn split(self) -> (SplitSink, SplitStream) where Self: Sink + Sized { diff --git a/futures-util/src/stream/select_all.rs b/futures-util/src/stream/select_all.rs index 2dae183fa4..1df5237057 100644 --- a/futures-util/src/stream/select_all.rs +++ b/futures-util/src/stream/select_all.rs @@ -1,7 +1,7 @@ //! An unbounded set of streams -use std::fmt::{self, Debug}; -use std::pin::Pin; +use core::fmt::{self, Debug}; +use core::pin::Pin; use futures_core::{Poll, Stream, FusedStream}; use futures_core::task::LocalWaker; diff --git a/futures-util/src/stream/split.rs b/futures-util/src/stream/split.rs index 4955efab29..6e6fb472c2 100644 --- a/futures-util/src/stream/split.rs +++ b/futures-util/src/stream/split.rs @@ -1,10 +1,12 @@ use futures_core::stream::Stream; use futures_core::task::{LocalWaker, Poll}; use futures_sink::Sink; +use core::fmt; +use core::pin::Pin; +#[cfg(feature = "std")] use std::any::Any; +#[cfg(feature = "std")] use std::error::Error; -use std::fmt; -use std::pin::Pin; use crate::lock::BiLock; @@ -139,6 +141,7 @@ impl fmt::Display for ReuniteError { } } +#[cfg(feature = "std")] impl Error for ReuniteError { fn description(&self) -> &str { "tried to reunite a SplitStream and SplitSink that don't form a pair" diff --git a/futures-util/src/task/local_waker_ref.rs b/futures-util/src/task/local_waker_ref.rs index ea0e360d44..daea19db73 100644 --- a/futures-util/src/task/local_waker_ref.rs +++ b/futures-util/src/task/local_waker_ref.rs @@ -1,10 +1,10 @@ #![allow(clippy::cast_ptr_alignment)] // clippy is too strict here -use std::marker::PhantomData; -use std::ops::Deref; -use std::ptr::NonNull; -use std::sync::Arc; -use std::task::{LocalWaker, Waker, Wake, UnsafeWake}; +use core::marker::PhantomData; +use core::ops::Deref; +use core::ptr::NonNull; +use alloc::sync::Arc; +use alloc::task::{LocalWaker, Waker, Wake, UnsafeWake}; /// A [`LocalWaker`](::std::task::LocalWaker) that is only valid for a given lifetime. /// diff --git a/futures-util/src/task/mod.rs b/futures-util/src/task/mod.rs index 5d87ed8f73..45288b3c2d 100644 --- a/futures-util/src/task/mod.rs +++ b/futures-util/src/task/mod.rs @@ -6,9 +6,9 @@ pub use self::noop_waker::{noop_local_waker, noop_local_waker_ref}; mod spawn; pub use self::spawn::{SpawnExt, LocalSpawnExt}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod local_waker_ref; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::local_waker_ref::{local_waker_ref, local_waker_ref_from_nonlocal, LocalWakerRef}; #[cfg_attr( diff --git a/futures-util/src/task/spawn.rs b/futures-util/src/task/spawn.rs index 58fd89bec7..46274aa643 100644 --- a/futures-util/src/task/spawn.rs +++ b/futures-util/src/task/spawn.rs @@ -4,10 +4,12 @@ use futures_core::task::{LocalSpawn, Spawn}; #[cfg(feature = "std")] use crate::future::{FutureExt, RemoteHandle}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use futures_core::future::{Future, FutureObj, LocalFutureObj}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use futures_core::task::SpawnError; +#[cfg(feature = "alloc")] +use alloc::prelude::*; impl SpawnExt for Sp where Sp: Spawn {} impl LocalSpawnExt for Sp where Sp: LocalSpawn {} @@ -39,7 +41,7 @@ pub trait SpawnExt: Spawn { /// let future = async { /* ... */ }; /// executor.spawn(future).unwrap(); /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn spawn(&mut self, future: Fut) -> Result<(), SpawnError> where Fut: Future + Send + 'static, @@ -118,7 +120,7 @@ pub trait LocalSpawnExt: LocalSpawn { /// let future = async { /* ... */ }; /// spawner.spawn_local(future).unwrap(); /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn spawn_local(&mut self, future: Fut) -> Result<(), SpawnError> where Fut: Future + 'static, diff --git a/futures-util/src/try_future/mod.rs b/futures-util/src/try_future/mod.rs index add19f88d0..7debafb699 100644 --- a/futures-util/src/try_future/mod.rs +++ b/futures-util/src/try_future/mod.rs @@ -54,10 +54,10 @@ pub use self::or_else::OrElse; mod unwrap_or_else; pub use self::unwrap_or_else::UnwrapOrElse; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod try_join_all; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::try_join_all::{try_join_all, TryJoinAll}; // Implementation details diff --git a/futures-util/src/try_future/try_join_all.rs b/futures-util/src/try_future/try_join_all.rs index 3610cf35da..0bc5be7705 100644 --- a/futures-util/src/try_future/try_join_all.rs +++ b/futures-util/src/try_future/try_join_all.rs @@ -1,13 +1,13 @@ //! Definition of the `TryJoinAll` combinator, waiting for all of a list of //! futures to finish with either success or error. -use std::fmt; -use std::future::Future; -use std::iter::FromIterator; -use std::mem; -use std::pin::Pin; -use std::prelude::v1::*; -use std::task::Poll; +use core::fmt; +use core::future::Future; +use core::iter::FromIterator; +use core::mem; +use core::pin::Pin; +use alloc::prelude::*; +use alloc::task::Poll; use super::TryFuture; @@ -139,7 +139,7 @@ where fn poll( mut self: Pin<&mut Self>, - lw: &::std::task::LocalWaker, + lw: &::alloc::task::LocalWaker, ) -> Poll { let mut state = FinalState::AllDone; diff --git a/futures-util/src/try_stream/mod.rs b/futures-util/src/try_stream/mod.rs index 158d63a04a..f64baecb8f 100644 --- a/futures-util/src/try_stream/mod.rs +++ b/futures-util/src/try_stream/mod.rs @@ -41,21 +41,21 @@ pub use self::try_fold::TryFold; mod try_skip_while; pub use self::try_skip_while::TrySkipWhile; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod try_buffer_unordered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::try_buffer_unordered::TryBufferUnordered; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod try_collect; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::try_collect::TryCollect; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod try_for_each_concurrent; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] pub use self::try_for_each_concurrent::TryForEachConcurrent; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use futures_core::future::Future; #[cfg(feature = "std")] @@ -312,7 +312,7 @@ pub trait TryStreamExt: TryStream { /// assert_eq!(Err(oneshot::Canceled), await!(fut)); /// # }) /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn try_for_each_concurrent( self, limit: impl Into>, @@ -360,7 +360,7 @@ pub trait TryStreamExt: TryStream { /// assert_eq!(output, Err(6)); /// # }) /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn try_collect>(self) -> TryCollect where Self: Sized { @@ -548,7 +548,7 @@ pub trait TryStreamExt: TryStream { /// assert_eq!(await!(buffered.next()), Some(Err("error in the stream"))); /// # }) /// ``` - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] fn try_buffer_unordered(self, n: usize) -> TryBufferUnordered where Self::Ok: TryFuture, Self: Sized diff --git a/futures-util/src/try_stream/try_buffer_unordered.rs b/futures-util/src/try_stream/try_buffer_unordered.rs index 4a745481f3..ece94b29ae 100644 --- a/futures-util/src/try_stream/try_buffer_unordered.rs +++ b/futures-util/src/try_stream/try_buffer_unordered.rs @@ -5,7 +5,7 @@ use futures_core::future::TryFuture; use futures_core::stream::{Stream, TryStream}; use futures_core::task::{LocalWaker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::pin::Pin; +use core::pin::Pin; /// A stream returned by the /// [`try_buffer_unordered`](super::TryStreamExt::try_buffer_unordered) method diff --git a/futures-util/src/try_stream/try_collect.rs b/futures-util/src/try_stream/try_collect.rs index a78743f18b..f6c5f3e1c0 100644 --- a/futures-util/src/try_stream/try_collect.rs +++ b/futures-util/src/try_stream/try_collect.rs @@ -2,9 +2,8 @@ use futures_core::future::{FusedFuture, Future}; use futures_core::stream::{FusedStream, TryStream}; use futures_core::task::{LocalWaker, Poll}; use pin_utils::{unsafe_pinned, unsafe_unpinned}; -use std::mem; -use std::pin::Pin; -use std::prelude::v1::*; +use core::mem; +use core::pin::Pin; /// A future which attempts to collect all of the values of a stream. /// diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 9e2342a38c..986664b412 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -36,8 +36,9 @@ futures-test-preview = { path = "../futures-test", version = "=0.3.0-alpha.12" } tokio = "0.1.11" [features] -nightly = ["futures-util-preview/nightly", "futures-core-preview/nightly"] -std = ["futures-core-preview/std", "futures-executor-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-util-preview/std"] +nightly = ["futures-util-preview/nightly", "futures-core-preview/nightly", "futures-sink-preview/nightly"] +std = ["alloc", "futures-core-preview/std", "futures-executor-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-util-preview/std"] default = ["std"] compat = ["std", "futures-util-preview/compat"] io-compat = ["compat", "futures-util-preview/io-compat"] +alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-util-preview/alloc"] diff --git a/futures/src/lib.rs b/futures/src/lib.rs index 18d105d329..857c06320e 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -31,6 +31,10 @@ #![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.12/futures")] #![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))] +#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] + +#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))] +compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features"); #[doc(hidden)] pub use futures_util::core_reexport; @@ -189,15 +193,18 @@ pub mod future { Join5, Map, Then, }; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] pub use futures_util::future::{ abortable, Abortable, AbortHandle, AbortRegistration, Aborted, + + join_all, JoinAll, + }; + #[cfg(feature = "std")] + pub use futures_util::future::{ Remote, RemoteHandle, // For FutureExt: CatchUnwind, Shared, - join_all, JoinAll, - // ToDo: SelectAll, SelectOk, select_all, select_ok }; @@ -282,7 +289,7 @@ pub mod sink { // WithFlatMap, }; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] pub use futures_util::sink::Buffer; } @@ -319,18 +326,23 @@ pub mod stream { Take, TakeWhile, Then, Zip }; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] pub use futures_util::stream::{ futures_ordered, FuturesOrdered, futures_unordered, FuturesUnordered, // For StreamExt: - BufferUnordered, Buffered, CatchUnwind, Chunks, Collect, SplitStream, - SplitSink, ReuniteError, + BufferUnordered, Buffered, Chunks, Collect, SplitStream, SplitSink, select_all, SelectAll, }; + #[cfg(feature = "std")] + pub use futures_util::stream::{ + // For StreamExt: + CatchUnwind, ReuniteError, + }; + pub use futures_util::try_stream::{ TryStreamExt, TryNext, TryForEach, ErrInto, @@ -339,7 +351,7 @@ pub mod stream { // ToDo: AndThen, ErrInto, InspectErr, MapErr, OrElse }; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] pub use futures_util::try_stream::{ // For TryStreamExt: TryCollect, TryBufferUnordered, @@ -363,12 +375,12 @@ pub mod task { Waker, LocalWaker, UnsafeWake, }; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] pub use futures_core::task::{ Wake, local_waker, local_waker_from_nonlocal }; - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] pub use futures_util::task::{ LocalWakerRef, local_waker_ref, local_waker_ref_from_nonlocal, SpawnExt, LocalSpawnExt,