Skip to content

Add no_std + alloc environment support take 2 #1479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -74,6 +82,10 @@ matrix:
--target thumbv6m-none-eabi
--no-default-features
--features nightly,cfg-target-has-atomic
- cargo build --manifest-path futures/Cargo.toml
--target thumbv6m-none-eabi
--no-default-features
--features nightly,alloc,cfg-target-has-atomic

- name: cargo build --target=thumbv7m-none-eabi
rust: nightly
Expand All @@ -84,6 +96,10 @@ matrix:
- cargo build --manifest-path futures/Cargo.toml
--target thumbv7m-none-eabi
--no-default-features
- cargo build --manifest-path futures/Cargo.toml
--target thumbv7m-none-eabi
--no-default-features
--features nightly,alloc

- name: cargo doc
rust: nightly
Expand Down
3 changes: 2 additions & 1 deletion futures-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ name = "futures_core"

[features]
default = ["std"]
std = ["either/use_std"]
std = ["alloc", "either/use_std"]
nightly = []
cfg-target-has-atomic = []
alloc = []

[dependencies]
either = { version = "1.4", default-features = false, optional = true }
Expand Down
7 changes: 4 additions & 3 deletions futures-core/src/future/future_obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>
where F: Future<Output = T> + 'a
Expand Down
7 changes: 5 additions & 2 deletions futures-core/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ impl<F: FusedFuture + ?Sized> FusedFuture for &mut F {
}
}

#[cfg(feature = "std")]
mod if_std {
#[cfg(feature = "alloc")]
mod if_alloc {
use alloc::boxed::Box;
use super::*;

impl<F: FusedFuture + ?Sized> FusedFuture for Box<F> {
fn is_terminated(&self) -> bool {
<F as FusedFuture>::is_terminated(&**self)
Expand All @@ -48,6 +50,7 @@ mod if_std {
}
}

#[cfg(feature = "std")]
impl<F: FusedFuture> FusedFuture for std::panic::AssertUnwindSafe<F> {
fn is_terminated(&self) -> bool {
<F as FusedFuture>::is_terminated(&**self)
Expand Down
9 changes: 9 additions & 0 deletions futures-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![feature(futures_api)]
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]

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

Expand All @@ -12,6 +13,14 @@
#[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");

#[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");

#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarifying: because the portions of std that appear in alloc use an identical API, this declaration makes it possible for everything to compile on stable when the alloc feature is disabled but the std feature is enabled, or when both features are enabled (in which case the alloc crate won't be used, only the std crate).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.


pub mod future;
#[doc(hidden)] pub use self::future::{Future, FusedFuture, TryFuture};

Expand Down
9 changes: 5 additions & 4 deletions futures-core/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ impl<S, T, E> 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<S: ?Sized + Stream + Unpin> Stream for Box<S> {
Expand All @@ -161,6 +161,7 @@ mod if_std {
}
}

#[cfg(feature = "std")]
impl<S: Stream> Stream for ::std::panic::AssertUnwindSafe<S> {
type Item = S::Item;

Expand All @@ -172,7 +173,7 @@ mod if_std {
}
}

impl<T: Unpin> Stream for ::std::collections::VecDeque<T> {
impl<T: Unpin> Stream for ::alloc::collections::VecDeque<T> {
type Item = T;

fn poll_next(
Expand Down
8 changes: 4 additions & 4 deletions futures-core/src/stream/stream_obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ where
unsafe fn drop(_ptr: *mut ()) {}
}

#[cfg(feature = "std")]
mod if_std {
use std::boxed::Box;
use std::mem;
#[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<F>
where F: Stream<Item = T> + 'a
Expand Down
4 changes: 3 additions & 1 deletion futures-sink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ 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"]
default = ["std"]
nightly = ["futures-core-preview/nightly"]
alloc = ["futures-core-preview/alloc"]

[dependencies]
either = { version = "1.4", default-features = false, optional = true }
Expand Down
23 changes: 16 additions & 7 deletions futures-sink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.13/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");

#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;

use futures_core::task::{Waker, Poll};
use core::pin::Pin;
Expand Down Expand Up @@ -155,16 +164,16 @@ 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.
/// Values of this type can never be created.
#[derive(Copy, Clone, Debug)]
pub enum VecSinkError {}

impl<T> Sink for ::std::vec::Vec<T> {
impl<T> Sink for ::alloc::vec::Vec<T> {
type SinkItem = T;
type SinkError = VecSinkError;

Expand All @@ -187,7 +196,7 @@ mod if_std {
}
}

impl<T> Sink for ::std::collections::VecDeque<T> {
impl<T> Sink for ::alloc::collections::VecDeque<T> {
type SinkItem = T;
type SinkError = VecSinkError;

Expand All @@ -210,7 +219,7 @@ mod if_std {
}
}

impl<S: ?Sized + Sink + Unpin> Sink for ::std::boxed::Box<S> {
impl<S: ?Sized + Sink + Unpin> Sink for ::alloc::boxed::Box<S> {
type SinkItem = S::SinkItem;
type SinkError = S::SinkError;

Expand All @@ -232,8 +241,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;
Expand Down
5 changes: 3 additions & 2 deletions futures-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ 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"]
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"]
cfg-target-has-atomic = []
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc"]

[dependencies]
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.13", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions futures-util/src/future/abortable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::task::AtomicWaker;
use futures_core::future::Future;
use futures_core::task::{Waker, 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)]
Expand Down
19 changes: 8 additions & 11 deletions futures-util/src/future/join_all.rs
Original file line number Diff line number Diff line change
@@ -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 core::task::{Poll, Waker};
use alloc::prelude::v1::*;

#[derive(Debug)]
enum ElemState<F>
Expand Down Expand Up @@ -127,10 +127,7 @@ where
{
type Output = Vec<F::Output>;

fn poll(
mut self: Pin<&mut Self>,
waker: &::std::task::Waker,
) -> Poll<Self::Output> {
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
let mut all_done = true;

for mut elem in iter_pin_mut(self.elems.as_mut()) {
Expand Down
25 changes: 14 additions & 11 deletions futures-util/src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use core::pin::Pin;
use futures_core::future::Future;
use futures_core::stream::Stream;
use futures_core::task::{Waker, Poll};
#[cfg(feature = "alloc")]
use alloc::boxed::Box;

// re-export for `select!`
#[doc(hidden)]
Expand Down Expand Up @@ -67,10 +69,17 @@ pub use self::unit_error::UnitError;
mod chain;
pub(crate) use self::chain::Chain;

#[cfg(feature = "std")]
mod abortable;
#[cfg(feature = "std")]
pub use self::abortable::{abortable, Abortable, AbortHandle, AbortRegistration, Aborted};
#[cfg(feature = "alloc")]
mod join_all;
#[cfg(feature = "alloc")]
pub use self::join_all::{join_all, JoinAll};

cfg_target_has_atomic! {
#[cfg(feature = "alloc")]
mod abortable;
#[cfg(feature = "alloc")]
pub use self::abortable::{abortable, Abortable, AbortHandle, AbortRegistration, Aborted};
}

#[cfg(feature = "std")]
mod catch_unwind;
Expand All @@ -82,12 +91,6 @@ mod remote_handle;
#[cfg(feature = "std")]
pub use self::remote_handle::{Remote, RemoteHandle};

#[cfg(feature = "std")]
mod join_all;

#[cfg(feature = "std")]
pub use self::join_all::{join_all, JoinAll};

// #[cfg(feature = "std")]
// mod select_all;
// #[cfg(feature = "std")]
Expand Down Expand Up @@ -653,7 +656,7 @@ pub trait FutureExt: Future {
}

/// Wrap the future in a Box, pinning it.
#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
fn boxed(self) -> Pin<Box<Self>>
where Self: Sized
{
Expand Down
18 changes: 15 additions & 3 deletions futures-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//! and the `AsyncRead` and `AsyncWrite` traits.

#![feature(futures_api)]
#![cfg_attr(feature = "std", feature(async_await, await_macro, box_into_pin))]
#![cfg_attr(feature = "alloc", feature(box_into_pin))]
#![cfg_attr(feature = "std", feature(async_await, await_macro))]
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc, alloc_prelude))]

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
Expand All @@ -13,6 +15,14 @@
#[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");

#[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");

#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc;

#[macro_use]
mod macros;

Expand Down Expand Up @@ -91,5 +101,7 @@ pub mod io;
#[cfg(feature = "std")]
#[doc(hidden)] pub use crate::io::{AsyncReadExt, AsyncWriteExt};

#[cfg(feature = "std")]
pub mod lock;
cfg_target_has_atomic! {
#[cfg(feature = "alloc")]
pub mod lock;
}
Loading