Skip to content

Commit c6fd447

Browse files
committed
Add no-std support
1 parent 04e3292 commit c6fd447

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+523
-247
lines changed

.github/workflows/ci.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ jobs:
3434
command: check
3535
args: --all --bins --examples
3636

37+
- name: check no-std
38+
uses: actions-rs/cargo@v1
39+
with:
40+
command: check
41+
args: --all --no-default-features
42+
43+
- name: check alloc
44+
uses: actions-rs/cargo@v1
45+
with:
46+
command: check
47+
args: --all --no-default-features --features alloc
48+
3749
- name: tests
3850
uses: actions-rs/cargo@v1
3951
with:

Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ harness = false
2727
name = "compare"
2828
harness = false
2929

30+
[features]
31+
default = ["std"]
32+
std = ["alloc"]
33+
alloc = ["bitvec/alloc", "dep:slab", "dep:smallvec"]
34+
3035
[dependencies]
31-
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
32-
futures-core = "0.3"
36+
bitvec = { version = "1.0.1", default-features = false }
37+
futures-core = { version = "0.3", default-features = false }
3338
pin-project = "1.0.8"
34-
slab = "0.4.8"
35-
smallvec = "1.11.0"
39+
slab = { version = "0.4.8", optional = true }
40+
smallvec = { version = "1.11.0", optional = true }
3641

3742
[dev-dependencies]
3843
async-std = { version = "1.12.0", features = ["attributes"] }

benches/utils/countdown_streams.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn streams_array<const N: usize>() -> [CountdownStream; N] {
4242
let wakers = Rc::new(RefCell::new(BinaryHeap::new()));
4343
let completed = Rc::new(Cell::new(0));
4444
let mut streams =
45-
std::array::from_fn(|n| CountdownStream::new(n, N, wakers.clone(), completed.clone()));
45+
core::array::from_fn(|n| CountdownStream::new(n, N, wakers.clone(), completed.clone()));
4646
shuffle(&mut streams);
4747
streams
4848
}

src/future/future_group.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use alloc::collections::BTreeSet;
2+
use core::fmt::{self, Debug};
3+
use core::ops::{Deref, DerefMut};
4+
use core::pin::Pin;
5+
use core::task::{Context, Poll};
16
use futures_core::stream::Stream;
27
use futures_core::Future;
38
use slab::Slab;
4-
use std::collections::BTreeSet;
5-
use std::fmt::{self, Debug};
6-
use std::ops::{Deref, DerefMut};
7-
use std::pin::Pin;
8-
use std::task::{Context, Poll};
99

1010
use crate::utils::{PollState, PollVec, WakerVec};
1111

@@ -237,7 +237,7 @@ impl<F: Future> FutureGroup<F> {
237237

238238
// Set the corresponding state
239239
self.states[index].set_pending();
240-
let mut readiness = self.wakers.readiness().lock().unwrap();
240+
let mut readiness = self.wakers.readiness();
241241
readiness.set_ready(index);
242242

243243
key
@@ -273,7 +273,7 @@ impl<F: Future> FutureGroup<F> {
273273
impl<F: Future> FutureGroup<F> {
274274
fn poll_next_inner(
275275
self: Pin<&mut Self>,
276-
cx: &std::task::Context<'_>,
276+
cx: &Context<'_>,
277277
) -> Poll<Option<(Key, <F as Future>::Output)>> {
278278
let mut this = self.project();
279279

@@ -283,7 +283,7 @@ impl<F: Future> FutureGroup<F> {
283283
}
284284

285285
// Set the top-level waker and check readiness
286-
let mut readiness = this.wakers.readiness().lock().unwrap();
286+
let mut readiness = this.wakers.readiness();
287287
readiness.set_waker(cx.waker());
288288
if !readiness.any_ready() {
289289
// Nothing is ready yet
@@ -326,7 +326,7 @@ impl<F: Future> FutureGroup<F> {
326326
};
327327

328328
// Lock readiness so we can use it again
329-
readiness = this.wakers.readiness().lock().unwrap();
329+
readiness = this.wakers.readiness();
330330
}
331331
}
332332

@@ -343,10 +343,7 @@ impl<F: Future> FutureGroup<F> {
343343
impl<F: Future> Stream for FutureGroup<F> {
344344
type Item = <F as Future>::Output;
345345

346-
fn poll_next(
347-
self: Pin<&mut Self>,
348-
cx: &mut std::task::Context<'_>,
349-
) -> Poll<Option<Self::Item>> {
346+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
350347
match self.poll_next_inner(cx) {
351348
Poll::Ready(Some((_key, item))) => Poll::Ready(Some(item)),
352349
Poll::Ready(None) => Poll::Ready(None),
@@ -396,10 +393,7 @@ impl<F: Future> DerefMut for Keyed<F> {
396393
impl<F: Future> Stream for Keyed<F> {
397394
type Item = (Key, <F as Future>::Output);
398395

399-
fn poll_next(
400-
self: Pin<&mut Self>,
401-
cx: &mut std::task::Context<'_>,
402-
) -> Poll<Option<Self::Item>> {
396+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
403397
let mut this = self.project();
404398
this.group.as_mut().poll_next_inner(cx)
405399
}
@@ -408,8 +402,8 @@ impl<F: Future> Stream for Keyed<F> {
408402
#[cfg(test)]
409403
mod test {
410404
use super::FutureGroup;
405+
use core::future;
411406
use futures_lite::prelude::*;
412-
use std::future;
413407

414408
#[test]
415409
fn smoke() {

src/future/futures_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::future::Join;
22
use crate::future::Race;
3+
use core::future::IntoFuture;
34
use futures_core::Future;
4-
use std::future::IntoFuture;
55

66
use super::join::tuple::Join2;
77
use super::race::tuple::Race2;

src/future/join/array.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::utils::{FutureArray, OutputArray, PollArray, WakerArray};
44
use core::fmt;
55
use core::future::{Future, IntoFuture};
66
use core::mem::ManuallyDrop;
7+
use core::ops::DerefMut;
78
use core::pin::Pin;
89
use core::task::{Context, Poll};
9-
use std::ops::DerefMut;
1010

1111
use pin_project::{pin_project, pinned_drop};
1212

@@ -93,7 +93,7 @@ where
9393
"Futures must not be polled after completing"
9494
);
9595

96-
let mut readiness = this.wakers.readiness().lock().unwrap();
96+
let mut readiness = this.wakers.readiness();
9797
readiness.set_waker(cx.waker());
9898
if *this.pending != 0 && !readiness.any_ready() {
9999
// Nothing is ready yet
@@ -125,7 +125,7 @@ where
125125
}
126126

127127
// Lock readiness so we can use it again
128-
readiness = this.wakers.readiness().lock().unwrap();
128+
readiness = this.wakers.readiness();
129129
}
130130
}
131131

@@ -178,12 +178,8 @@ where
178178
#[cfg(test)]
179179
mod test {
180180
use super::*;
181-
use crate::utils::DummyWaker;
182181

183-
use std::future;
184-
use std::future::Future;
185-
use std::sync::Arc;
186-
use std::task::Context;
182+
use core::future;
187183

188184
#[test]
189185
fn smoke() {
@@ -203,7 +199,13 @@ mod test {
203199
}
204200

205201
#[test]
202+
#[cfg(feature = "alloc")]
206203
fn debug() {
204+
use crate::utils::DummyWaker;
205+
use alloc::format;
206+
use alloc::sync::Arc;
207+
use core::task::Context;
208+
207209
let mut fut = [future::ready("hello"), future::ready("world")].join();
208210
assert_eq!(format!("{:?}", fut), "[Pending, Pending]");
209211
let mut fut = Pin::new(&mut fut);

src/future/join/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::future::Future;
22

33
pub(crate) mod array;
44
pub(crate) mod tuple;
5+
#[cfg(feature = "alloc")]
56
pub(crate) mod vec;
67

78
/// Wait for all futures to complete.

src/future/join/tuple.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use crate::utils::{PollArray, WakerArray};
33

44
use core::fmt::{self, Debug};
55
use core::future::{Future, IntoFuture};
6-
use core::mem::MaybeUninit;
6+
use core::mem::{ManuallyDrop, MaybeUninit};
7+
use core::ops::DerefMut;
78
use core::pin::Pin;
89
use core::task::{Context, Poll};
9-
use std::mem::ManuallyDrop;
10-
use std::ops::DerefMut;
1110

1211
use pin_project::{pin_project, pinned_drop};
1312

@@ -136,7 +135,7 @@ macro_rules! impl_join_tuple {
136135
};
137136
($mod_name:ident $StructName:ident $($F:ident)+) => {
138137
mod $mod_name {
139-
use std::mem::ManuallyDrop;
138+
use core::mem::ManuallyDrop;
140139

141140
#[pin_project::pin_project]
142141
pub(super) struct Futures<$($F,)+> {$(
@@ -199,7 +198,7 @@ macro_rules! impl_join_tuple {
199198

200199
let mut futures = this.futures.project();
201200

202-
let mut readiness = this.wakers.readiness().lock().unwrap();
201+
let mut readiness = this.wakers.readiness();
203202
readiness.set_waker(cx.waker());
204203

205204
for index in 0..LEN {
@@ -234,7 +233,7 @@ macro_rules! impl_join_tuple {
234233

235234
return Poll::Ready(out);
236235
}
237-
readiness = this.wakers.readiness().lock().unwrap();
236+
readiness = this.wakers.readiness();
238237
}
239238

240239
Poll::Pending
@@ -294,7 +293,7 @@ impl_join_tuple! { join12 Join12 A B C D E F G H I J K L }
294293
#[cfg(test)]
295294
mod test {
296295
use super::*;
297-
use std::future;
296+
use core::future;
298297

299298
#[test]
300299
#[allow(clippy::unit_cmp)]
@@ -332,6 +331,7 @@ mod test {
332331
}
333332

334333
#[test]
334+
#[cfg(feature = "std")]
335335
fn does_not_leak_memory() {
336336
use core::cell::RefCell;
337337
use futures_lite::future::pending;

src/future/join/vec.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use super::Join as JoinTrait;
22
use crate::utils::{FutureVec, OutputVec, PollVec, WakerVec};
33

4+
use alloc::vec::Vec;
45
use core::fmt;
56
use core::future::{Future, IntoFuture};
7+
use core::mem::ManuallyDrop;
8+
use core::ops::DerefMut;
69
use core::pin::Pin;
710
use core::task::{Context, Poll};
8-
use std::mem::ManuallyDrop;
9-
use std::ops::DerefMut;
10-
use std::vec::Vec;
1111

1212
use pin_project::{pin_project, pinned_drop};
1313

@@ -85,7 +85,7 @@ where
8585
"Futures must not be polled after completing"
8686
);
8787

88-
let mut readiness = this.wakers.readiness().lock().unwrap();
88+
let mut readiness = this.wakers.readiness();
8989
readiness.set_waker(cx.waker());
9090
if *this.pending != 0 && !readiness.any_ready() {
9191
// Nothing is ready yet
@@ -119,7 +119,7 @@ where
119119
}
120120

121121
// Lock readiness so we can use it again
122-
readiness = this.wakers.readiness().lock().unwrap();
122+
readiness = this.wakers.readiness();
123123
}
124124
}
125125

@@ -174,10 +174,12 @@ mod test {
174174
use super::*;
175175
use crate::utils::DummyWaker;
176176

177-
use std::future;
178-
use std::future::Future;
179-
use std::sync::Arc;
180-
use std::task::Context;
177+
use alloc::format;
178+
use alloc::sync::Arc;
179+
use alloc::vec;
180+
use core::future;
181+
use core::future::Future;
182+
use core::task::Context;
181183

182184
#[test]
183185
fn smoke() {

src/future/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
//! complete, or return an `Err` if *no* futures complete successfully.
7070
//!
7171
#[doc(inline)]
72+
#[cfg(feature = "alloc")]
7273
pub use future_group::FutureGroup;
7374
pub use futures_ext::FutureExt;
7475
pub use join::Join;
@@ -77,6 +78,7 @@ pub use race_ok::RaceOk;
7778
pub use try_join::TryJoin;
7879

7980
/// A growable group of futures which act as a single unit.
81+
#[cfg(feature = "alloc")]
8082
pub mod future_group;
8183

8284
mod futures_ext;

0 commit comments

Comments
 (0)