Skip to content

Commit f2ae06d

Browse files
taiki-ecramertj
authored andcommitted
Add no_std + alloc environment support
1 parent 4eb1fbf commit f2ae06d

39 files changed

+246
-174
lines changed

.travis.yml

Lines changed: 16 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 16 additions & 7 deletions
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

Lines changed: 3 additions & 2 deletions
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 }

0 commit comments

Comments
 (0)