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

Lines changed: 8 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:

futures-core/Cargo.toml

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)