Skip to content

Commit ec3b7c2

Browse files
committed
Add no_std + alloc support
1 parent ebbc6be commit ec3b7c2

File tree

28 files changed

+184
-76
lines changed

28 files changed

+184
-76
lines changed

.travis.yml

+8
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:

futures-core/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ documentation = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alph
1010
description = """
1111
The core traits and types in for the `futures` library.
1212
"""
13+
build = "build.rs"
1314

1415
[lib]
1516
name = "futures_core"
@@ -18,9 +19,11 @@ name = "futures_core"
1819
default = ["std"]
1920
std = ["either/use_std"]
2021
nightly = []
22+
alloc = ["alloc-shim/alloc"]
2123

2224
[dependencies]
2325
either = { version = "1.4", default-features = false, optional = true }
26+
alloc-shim = { version = "0.2.1", optional = true }
2427

2528
[dev-dependencies]
2629
futures-preview = { path = "../futures", version = "=0.3.0-alpha.12" }

futures-core/build.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=build.rs");
5+
6+
if env::var_os("CARGO_FEATURE_STD").is_some() || env::var_os("CARGO_FEATURE_ALLOC").is_some() {
7+
println!("cargo:rustc-cfg=alloc");
8+
}
9+
}

futures-core/src/future/future_obj.rs

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

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

193194
unsafe impl<'a, T, F> UnsafeFutureObj<'a, T> for Box<F>

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(alloc)]
37+
mod if_alloc {
3838
use super::*;
39+
use std::boxed::Box;
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

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![feature(futures_api)]
44
#![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))]
5+
#![cfg_attr(feature = "alloc", feature(alloc))]
56

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

@@ -10,6 +11,12 @@
1011

1112
#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.12/futures_core")]
1213

14+
#[cfg(all(feature = "alloc", not(feature = "nightly")))]
15+
compile_error!("The `alloc` feature requires the `nightly` feature active to explicitly opt-in to unstable features");
16+
17+
#[cfg(all(feature = "alloc", not(feature = "std")))]
18+
extern crate alloc as std;
19+
1320
pub mod future;
1421
#[doc(hidden)] pub use self::future::{Future, FusedFuture, TryFuture};
1522

futures-core/src/stream/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl<S, T, E> TryStream for S
145145
}
146146
}
147147

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

@@ -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

futures-core/src/stream/stream_obj.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::Stream;
22
use crate::task::{LocalWaker, Poll};
33
use core::fmt;
44
use core::marker::PhantomData;
5-
use core::mem;
65
use core::pin::Pin;
76

87
/// A custom trait object for polling streams, roughly akin to
@@ -188,10 +187,11 @@ where
188187
unsafe fn drop(_ptr: *mut ()) {}
189188
}
190189

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

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

futures-core/src/task/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ pub mod __internal;
66
pub use self::spawn::{Spawn, LocalSpawn, SpawnError};
77

88
pub use core::task::{Poll, Waker, LocalWaker, UnsafeWake};
9-
#[cfg(feature = "std")]
9+
#[cfg(alloc)]
1010
pub use std::task::{Wake, local_waker, local_waker_from_nonlocal};

futures-sink/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ documentation = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alph
1010
description = """
1111
The asynchronous `Sink` trait for the futures-rs library.
1212
"""
13+
build = "build.rs"
1314

1415
[lib]
1516
name = "futures_sink"
1617

1718
[features]
1819
std = ["either/use_std", "futures-core-preview/std", "futures-channel-preview/std"]
1920
default = ["std"]
21+
nightly = ["futures-core-preview/nightly"]
22+
alloc = ["futures-core-preview/alloc", "alloc-shim/alloc"]
2023

2124
[dependencies]
2225
either = { version = "1.4", default-features = false, optional = true }
2326
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.12", default-features = false }
2427
futures-channel-preview = { path = "../futures-channel", version = "=0.3.0-alpha.12", default-features = false }
28+
alloc-shim = { version = "0.2.1", optional = true }

futures-sink/build.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=build.rs");
5+
6+
if env::var_os("CARGO_FEATURE_STD").is_some() || env::var_os("CARGO_FEATURE_ALLOC").is_some() {
7+
println!("cargo:rustc-cfg=alloc");
8+
}
9+
}

futures-sink/src/lib.rs

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

1010
#![feature(futures_api)]
11+
#![cfg_attr(feature = "alloc", feature(alloc))]
12+
13+
#[cfg(all(feature = "alloc", not(feature = "nightly")))]
14+
compile_error!("The `alloc` feature 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 as std;
1118

1219
use futures_core::task::{LocalWaker, Poll};
1320
use core::pin::Pin;
@@ -155,8 +162,8 @@ impl<'a, S: ?Sized + Sink> Sink for Pin<&'a mut S> {
155162
#[cfg(feature = "std")]
156163
mod channel_impls;
157164

158-
#[cfg(feature = "std")]
159-
mod if_std {
165+
#[cfg(alloc)]
166+
mod if_alloc {
160167
use super::*;
161168

162169
/// The error type for `Vec` and `VecDequeue` when used as `Sink`s.
@@ -232,8 +239,8 @@ mod if_std {
232239
}
233240
}
234241

235-
#[cfg(feature = "std")]
236-
pub use self::if_std::*;
242+
#[cfg(alloc)]
243+
pub use self::if_alloc::*;
237244

238245
#[cfg(feature = "either")]
239246
use either::Either;

futures-util/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ documentation = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alph
1010
description = """
1111
Common utilities and extension traits for the futures-rs library.
1212
"""
13+
build = "build.rs"
1314

1415
[lib]
1516
name = "futures_util"
@@ -20,7 +21,8 @@ default = ["std", "futures-core-preview/either", "futures-sink-preview/either"]
2021
compat = ["std", "futures_01"]
2122
io-compat = ["compat", "tokio-io"]
2223
bench = []
23-
nightly = []
24+
nightly = ["futures-core-preview/nightly", "futures-sink-preview/nightly"]
25+
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "alloc-shim/alloc"]
2426

2527
[dependencies]
2628
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.12", default-features = false }
@@ -37,6 +39,7 @@ slab = { version = "0.4", optional = true }
3739
futures_01 = { version = "0.1.25", optional = true, package = "futures" }
3840
tokio-io = { version = "0.1.9", optional = true }
3941
pin-utils = "0.1.0-alpha.4"
42+
alloc-shim = { version = "0.2.1", optional = true }
4043

4144
[dev-dependencies]
4245
futures-preview = { path = "../futures", version = "=0.3.0-alpha.12" }

futures-util/build.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=build.rs");
5+
6+
if env::var_os("CARGO_FEATURE_STD").is_some() || env::var_os("CARGO_FEATURE_ALLOC").is_some() {
7+
println!("cargo:rustc-cfg=alloc");
8+
}
9+
}

futures-util/src/future/mod.rs

+7-5
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::{LocalWaker, Poll};
10+
#[cfg(alloc)]
11+
use std::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(alloc)]
7173
mod abortable;
72-
#[cfg(feature = "std")]
74+
#[cfg(alloc)]
7375
pub use self::abortable::{abortable, Abortable, AbortHandle, AbortRegistration, Aborted};
7476

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

85-
#[cfg(feature = "std")]
87+
#[cfg(alloc)]
8688
mod join_all;
8789

88-
#[cfg(feature = "std")]
90+
#[cfg(alloc)]
8991
pub use self::join_all::{join_all, JoinAll};
9092

9193
// #[cfg(feature = "std")]
@@ -653,7 +655,7 @@ pub trait FutureExt: Future {
653655
}
654656

655657
/// Wrap the future in a Box, pinning it.
656-
#[cfg(feature = "std")]
658+
#[cfg(alloc)]
657659
fn boxed(self) -> Pin<Box<Self>>
658660
where Self: Sized
659661
{

futures-util/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
#![feature(futures_api, box_into_pin)]
55
#![cfg_attr(feature = "std", feature(async_await, await_macro))]
66
#![cfg_attr(feature = "nightly", feature(cfg_target_has_atomic))]
7+
#![cfg_attr(feature = "alloc", feature(alloc))]
78

89
#![cfg_attr(not(feature = "std"), no_std)]
910
#![warn(missing_docs, missing_debug_implementations)]
1011
#![deny(bare_trait_objects)]
1112

1213
#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.12/futures_util")]
1314

15+
#[cfg(all(feature = "alloc", not(feature = "nightly")))]
16+
compile_error!("The `alloc` feature requires the `nightly` feature active to explicitly opt-in to unstable features");
17+
18+
#[cfg(all(feature = "alloc", not(feature = "std")))]
19+
extern crate alloc as std;
20+
1421
#[macro_use]
1522
mod macros;
1623

@@ -89,5 +96,5 @@ pub mod io;
8996
#[cfg(feature = "std")]
9097
#[doc(hidden)] pub use crate::io::{AsyncReadExt, AsyncWriteExt};
9198

92-
#[cfg(feature = "std")]
99+
#[cfg(alloc)]
93100
pub mod lock;

futures-util/src/lock/bilock.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
use futures_core::future::Future;
55
use futures_core::task::{LocalWaker, Poll, Waker};
6+
#[cfg(feature = "std")]
67
use std::any::Any;
78
use std::boxed::Box;
89
use std::cell::UnsafeCell;
10+
#[cfg(feature = "std")]
911
use std::error::Error;
1012
use std::fmt;
1113
use std::mem;
@@ -210,6 +212,7 @@ impl<T> fmt::Display for ReuniteError<T> {
210212
}
211213
}
212214

215+
#[cfg(feature = "std")]
213216
impl<T: Any> Error for ReuniteError<T> {
214217
fn description(&self) -> &str {
215218
"tried to reunite two BiLocks that don't form a pair"

futures-util/src/lock/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Futures-powered synchronization primitives.
22
3+
#[cfg(feature = "std")]
34
mod mutex;
5+
#[cfg(feature = "std")]
46
pub use self::mutex::{Mutex, MutexLockFuture, MutexGuard};
57

68
mod bilock;

futures-util/src/sink/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ pub use self::with::With;
4141
mod with_flat_map;
4242
pub use self::with_flat_map::WithFlatMap;
4343

44-
#[cfg(feature = "std")]
44+
#[cfg(alloc)]
4545
mod buffer;
46-
#[cfg(feature = "std")]
46+
#[cfg(alloc)]
4747
pub use self::buffer::Buffer;
4848

4949
impl<T: ?Sized> SinkExt for T where T: Sink {}
@@ -156,7 +156,7 @@ pub trait SinkExt: Sink {
156156
///
157157
/// This method is only available when the `std` feature of this
158158
/// library is activated, and it is activated by default.
159-
#[cfg(feature = "std")]
159+
#[cfg(alloc)]
160160
fn buffer(self, capacity: usize) -> Buffer<Self>
161161
where Self: Sized,
162162
{

0 commit comments

Comments
 (0)