Skip to content

Commit d6b7f5b

Browse files
committed
Add no_std + alloc environment support
1 parent c949076 commit d6b7f5b

40 files changed

+253
-185
lines changed

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ matrix:
5454
- cargo build --manifest-path futures-sink/Cargo.toml --no-default-features
5555
- cargo build --manifest-path futures-util/Cargo.toml --no-default-features
5656

57+
- name: cargo build (alloc)
58+
rust: nightly
59+
script:
60+
- cargo build --manifest-path futures/Cargo.toml --no-default-features --features alloc,nightly
61+
- cargo build --manifest-path futures-core/Cargo.toml --no-default-features --features alloc,nightly
62+
- cargo build --manifest-path futures-sink/Cargo.toml --no-default-features --features alloc,nightly
63+
- cargo build --manifest-path futures-util/Cargo.toml --no-default-features --features alloc,nightly
64+
5765
- name: cargo build (default features)
5866
rust: nightly
5967
script:
@@ -74,6 +82,10 @@ matrix:
7482
--target thumbv6m-none-eabi
7583
--no-default-features
7684
--features nightly,cfg-target-has-atomic
85+
- cargo build --manifest-path futures/Cargo.toml
86+
--target thumbv6m-none-eabi
87+
--no-default-features
88+
--features nightly,alloc,cfg-target-has-atomic
7789

7890
- name: cargo build --target=thumbv7m-none-eabi
7991
rust: nightly
@@ -84,6 +96,10 @@ matrix:
8496
- cargo build --manifest-path futures/Cargo.toml
8597
--target thumbv7m-none-eabi
8698
--no-default-features
99+
- cargo build --manifest-path futures/Cargo.toml
100+
--target thumbv7m-none-eabi
101+
--no-default-features
102+
--features nightly,alloc
87103

88104
- name: cargo doc
89105
rust: nightly

futures-core/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ name = "futures_core"
1616

1717
[features]
1818
default = ["std"]
19-
std = ["either/use_std"]
19+
std = ["alloc", "either/use_std"]
2020
nightly = []
2121
cfg-target-has-atomic = []
22+
alloc = []
2223

2324
[dependencies]
2425
either = { version = "1.4", default-features = false, optional = true }

futures-core/src/future/future_obj.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,11 @@ where
185185
unsafe fn drop(_ptr: *mut ()) {}
186186
}
187187

188-
#[cfg(feature = "std")]
189-
mod if_std {
188+
#[cfg(feature = "alloc")]
189+
mod if_alloc {
190190
use super::*;
191-
use std::mem;
191+
use core::mem;
192+
use alloc::boxed::Box;
192193

193194
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>
194195
where F: Future<Output = T> + 'a

futures-core/src/future/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ impl<F: FusedFuture + ?Sized> FusedFuture for &mut F {
3333
}
3434
}
3535

36-
#[cfg(feature = "std")]
37-
mod if_std {
36+
#[cfg(feature = "alloc")]
37+
mod if_alloc {
38+
use alloc::boxed::Box;
3839
use super::*;
40+
3941
impl<F: FusedFuture + ?Sized> FusedFuture for Box<F> {
4042
fn is_terminated(&self) -> bool {
4143
<F as FusedFuture>::is_terminated(&**self)
@@ -48,6 +50,7 @@ mod if_std {
4850
}
4951
}
5052

53+
#[cfg(feature = "std")]
5154
impl<F: FusedFuture> FusedFuture for std::panic::AssertUnwindSafe<F> {
5255
fn is_terminated(&self) -> bool {
5356
<F as FusedFuture>::is_terminated(&**self)

futures-core/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![feature(futures_api)]
44
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
5+
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]
56

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

@@ -12,6 +13,14 @@
1213
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
1314
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");
1415

16+
#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))]
17+
compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features");
18+
19+
#[cfg(all(feature = "alloc", not(feature = "std")))]
20+
extern crate alloc;
21+
#[cfg(feature = "std")]
22+
extern crate std as alloc;
23+
1524
pub mod future;
1625
#[doc(hidden)] pub use self::future::{Future, FusedFuture, TryFuture};
1726

futures-core/src/stream/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ impl<S, T, E> TryStream for S
145145
}
146146
}
147147

148-
#[cfg(feature = "std")]
149-
mod if_std {
150-
use std::boxed::Box;
148+
#[cfg(feature = "alloc")]
149+
mod if_alloc {
150+
use alloc::boxed::Box;
151151
use super::*;
152152

153153
impl<S: ?Sized + Stream + Unpin> Stream for Box<S> {
@@ -161,6 +161,7 @@ mod if_std {
161161
}
162162
}
163163

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

@@ -172,7 +173,7 @@ mod if_std {
172173
}
173174
}
174175

175-
impl<T: Unpin> Stream for ::std::collections::VecDeque<T> {
176+
impl<T: Unpin> Stream for ::alloc::collections::VecDeque<T> {
176177
type Item = T;
177178

178179
fn poll_next(

futures-core/src/stream/stream_obj.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ where
187187
unsafe fn drop(_ptr: *mut ()) {}
188188
}
189189

190-
#[cfg(feature = "std")]
191-
mod if_std {
192-
use std::boxed::Box;
193-
use std::mem;
190+
#[cfg(feature = "alloc")]
191+
mod if_alloc {
194192
use super::*;
193+
use core::mem;
194+
use alloc::boxed::Box;
195195

196196
unsafe impl<'a, T, F> UnsafeStreamObj<'a, T> for Box<F>
197197
where F: Stream<Item = T> + 'a

futures-sink/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ The asynchronous `Sink` trait for the futures-rs library.
1515
name = "futures_sink"
1616

1717
[features]
18-
std = ["either/use_std", "futures-core-preview/std", "futures-channel-preview/std"]
18+
std = ["alloc", "either/use_std", "futures-core-preview/std", "futures-channel-preview/std"]
1919
default = ["std"]
20+
nightly = ["futures-core-preview/nightly"]
21+
alloc = ["futures-core-preview/alloc"]
2022

2123
[dependencies]
2224
either = { version = "1.4", default-features = false, optional = true }

futures-sink/src/lib.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.13/futures_sink")]
99

1010
#![feature(futures_api)]
11+
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]
12+
13+
#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))]
14+
compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features");
15+
16+
#[cfg(all(feature = "alloc", not(feature = "std")))]
17+
extern crate alloc;
18+
#[cfg(feature = "std")]
19+
extern crate std as alloc;
1120

1221
use futures_core::task::{Waker, Poll};
1322
use core::pin::Pin;
@@ -155,16 +164,16 @@ impl<'a, S: ?Sized + Sink> Sink for Pin<&'a mut S> {
155164
#[cfg(feature = "std")]
156165
mod channel_impls;
157166

158-
#[cfg(feature = "std")]
159-
mod if_std {
167+
#[cfg(feature = "alloc")]
168+
mod if_alloc {
160169
use super::*;
161170

162171
/// The error type for `Vec` and `VecDequeue` when used as `Sink`s.
163172
/// Values of this type can never be created.
164173
#[derive(Copy, Clone, Debug)]
165174
pub enum VecSinkError {}
166175

167-
impl<T> Sink for ::std::vec::Vec<T> {
176+
impl<T> Sink for ::alloc::vec::Vec<T> {
168177
type SinkItem = T;
169178
type SinkError = VecSinkError;
170179

@@ -187,7 +196,7 @@ mod if_std {
187196
}
188197
}
189198

190-
impl<T> Sink for ::std::collections::VecDeque<T> {
199+
impl<T> Sink for ::alloc::collections::VecDeque<T> {
191200
type SinkItem = T;
192201
type SinkError = VecSinkError;
193202

@@ -210,7 +219,7 @@ mod if_std {
210219
}
211220
}
212221

213-
impl<S: ?Sized + Sink + Unpin> Sink for ::std::boxed::Box<S> {
222+
impl<S: ?Sized + Sink + Unpin> Sink for ::alloc::boxed::Box<S> {
214223
type SinkItem = S::SinkItem;
215224
type SinkError = S::SinkError;
216225

@@ -232,8 +241,8 @@ mod if_std {
232241
}
233242
}
234243

235-
#[cfg(feature = "std")]
236-
pub use self::if_std::*;
244+
#[cfg(feature = "alloc")]
245+
pub use self::if_alloc::*;
237246

238247
#[cfg(feature = "either")]
239248
use either::Either;

futures-util/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ Common utilities and extension traits for the futures-rs library.
1515
name = "futures_util"
1616

1717
[features]
18-
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"]
18+
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"]
1919
default = ["std", "futures-core-preview/either", "futures-sink-preview/either"]
2020
compat = ["std", "futures_01"]
2121
io-compat = ["compat", "tokio-io"]
2222
bench = []
23-
nightly = []
23+
nightly = ["futures-core-preview/nightly", "futures-sink-preview/nightly"]
2424
cfg-target-has-atomic = []
25+
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc"]
2526

2627
[dependencies]
2728
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.13", default-features = false }

futures-util/src/future/abortable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::task::AtomicWaker;
22
use futures_core::future::Future;
33
use futures_core::task::{Waker, Poll};
44
use pin_utils::unsafe_pinned;
5-
use std::pin::Pin;
6-
use std::sync::Arc;
7-
use std::sync::atomic::{AtomicBool, Ordering};
5+
use core::pin::Pin;
6+
use core::sync::atomic::{AtomicBool, Ordering};
7+
use alloc::sync::Arc;
88

99
/// A future which can be remotely short-circuited using an `AbortHandle`.
1010
#[derive(Debug, Clone)]

futures-util/src/future/join_all.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Definition of the `JoinAll` combinator, waiting for all of a list of futures
22
//! to finish.
33
4-
use std::fmt;
5-
use std::future::Future;
6-
use std::iter::FromIterator;
7-
use std::mem;
8-
use std::pin::Pin;
9-
use std::prelude::v1::*;
10-
use std::task::Poll;
4+
use core::fmt;
5+
use core::future::Future;
6+
use core::iter::FromIterator;
7+
use core::mem;
8+
use core::pin::Pin;
9+
use core::task::{Poll, Waker};
10+
use alloc::prelude::v1::*;
1111

1212
#[derive(Debug)]
1313
enum ElemState<F>
@@ -127,10 +127,7 @@ where
127127
{
128128
type Output = Vec<F::Output>;
129129

130-
fn poll(
131-
mut self: Pin<&mut Self>,
132-
waker: &::std::task::Waker,
133-
) -> Poll<Self::Output> {
130+
fn poll(mut self: Pin<&mut Self>, waker: &Waker) -> Poll<Self::Output> {
134131
let mut all_done = true;
135132

136133
for mut elem in iter_pin_mut(self.elems.as_mut()) {

futures-util/src/future/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use core::pin::Pin;
77
use futures_core::future::Future;
88
use futures_core::stream::Stream;
99
use futures_core::task::{Waker, Poll};
10+
#[cfg(feature = "alloc")]
11+
use alloc::boxed::Box;
1012

1113
// re-export for `select!`
1214
#[doc(hidden)]
@@ -67,9 +69,9 @@ pub use self::unit_error::UnitError;
6769
mod chain;
6870
pub(crate) use self::chain::Chain;
6971

70-
#[cfg(feature = "std")]
72+
#[cfg(feature = "alloc")]
7173
mod abortable;
72-
#[cfg(feature = "std")]
74+
#[cfg(feature = "alloc")]
7375
pub use self::abortable::{abortable, Abortable, AbortHandle, AbortRegistration, Aborted};
7476

7577
#[cfg(feature = "std")]
@@ -82,10 +84,9 @@ mod remote_handle;
8284
#[cfg(feature = "std")]
8385
pub use self::remote_handle::{Remote, RemoteHandle};
8486

85-
#[cfg(feature = "std")]
87+
#[cfg(feature = "alloc")]
8688
mod join_all;
87-
88-
#[cfg(feature = "std")]
89+
#[cfg(feature = "alloc")]
8990
pub use self::join_all::{join_all, JoinAll};
9091

9192
// #[cfg(feature = "std")]
@@ -653,7 +654,7 @@ pub trait FutureExt: Future {
653654
}
654655

655656
/// Wrap the future in a Box, pinning it.
656-
#[cfg(feature = "std")]
657+
#[cfg(feature = "alloc")]
657658
fn boxed(self) -> Pin<Box<Self>>
658659
where Self: Sized
659660
{

futures-util/src/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//! and the `AsyncRead` and `AsyncWrite` traits.
33
44
#![feature(futures_api)]
5-
#![cfg_attr(feature = "std", feature(async_await, await_macro, box_into_pin))]
5+
#![cfg_attr(feature = "alloc", feature(box_into_pin))]
6+
#![cfg_attr(feature = "std", feature(async_await, await_macro))]
67
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
8+
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc, alloc_prelude))]
79

810
#![cfg_attr(not(feature = "std"), no_std)]
911
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
@@ -13,6 +15,14 @@
1315
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
1416
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");
1517

18+
#[cfg(all(feature = "alloc", not(any(feature = "std", feature = "nightly"))))]
19+
compile_error!("The `alloc` feature without `std` requires the `nightly` feature active to explicitly opt-in to unstable features");
20+
21+
#[cfg(all(feature = "alloc", not(feature = "std")))]
22+
extern crate alloc;
23+
#[cfg(feature = "std")]
24+
extern crate std as alloc;
25+
1626
#[macro_use]
1727
mod macros;
1828

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

94-
#[cfg(feature = "std")]
104+
#[cfg(feature = "alloc")]
95105
pub mod lock;

0 commit comments

Comments
 (0)