Skip to content

Commit

Permalink
Merge pull request #163 from alexmoon/no-std
Browse files Browse the repository at this point in the history
Add no-std support
  • Loading branch information
yoshuawuyts authored Mar 11, 2024
2 parents 04e3292 + c6fd447 commit d6f70c7
Show file tree
Hide file tree
Showing 64 changed files with 523 additions and 247 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ jobs:
command: check
args: --all --bins --examples

- name: check no-std
uses: actions-rs/cargo@v1
with:
command: check
args: --all --no-default-features

- name: check alloc
uses: actions-rs/cargo@v1
with:
command: check
args: --all --no-default-features --features alloc

- name: tests
uses: actions-rs/cargo@v1
with:
Expand Down
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ harness = false
name = "compare"
harness = false

[features]
default = ["std"]
std = ["alloc"]
alloc = ["bitvec/alloc", "dep:slab", "dep:smallvec"]

[dependencies]
bitvec = { version = "1.0.1", default-features = false, features = ["alloc"] }
futures-core = "0.3"
bitvec = { version = "1.0.1", default-features = false }
futures-core = { version = "0.3", default-features = false }
pin-project = "1.0.8"
slab = "0.4.8"
smallvec = "1.11.0"
slab = { version = "0.4.8", optional = true }
smallvec = { version = "1.11.0", optional = true }

[dev-dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
Expand Down
2 changes: 1 addition & 1 deletion benches/utils/countdown_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn streams_array<const N: usize>() -> [CountdownStream; N] {
let wakers = Rc::new(RefCell::new(BinaryHeap::new()));
let completed = Rc::new(Cell::new(0));
let mut streams =
std::array::from_fn(|n| CountdownStream::new(n, N, wakers.clone(), completed.clone()));
core::array::from_fn(|n| CountdownStream::new(n, N, wakers.clone(), completed.clone()));
shuffle(&mut streams);
streams
}
Expand Down
30 changes: 12 additions & 18 deletions src/future/future_group.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use alloc::collections::BTreeSet;
use core::fmt::{self, Debug};
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::stream::Stream;
use futures_core::Future;
use slab::Slab;
use std::collections::BTreeSet;
use std::fmt::{self, Debug};
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::task::{Context, Poll};

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

Expand Down Expand Up @@ -237,7 +237,7 @@ impl<F: Future> FutureGroup<F> {

// Set the corresponding state
self.states[index].set_pending();
let mut readiness = self.wakers.readiness().lock().unwrap();
let mut readiness = self.wakers.readiness();
readiness.set_ready(index);

key
Expand Down Expand Up @@ -273,7 +273,7 @@ impl<F: Future> FutureGroup<F> {
impl<F: Future> FutureGroup<F> {
fn poll_next_inner(
self: Pin<&mut Self>,
cx: &std::task::Context<'_>,
cx: &Context<'_>,
) -> Poll<Option<(Key, <F as Future>::Output)>> {
let mut this = self.project();

Expand All @@ -283,7 +283,7 @@ impl<F: Future> FutureGroup<F> {
}

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

// Lock readiness so we can use it again
readiness = this.wakers.readiness().lock().unwrap();
readiness = this.wakers.readiness();
}
}

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

fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.poll_next_inner(cx) {
Poll::Ready(Some((_key, item))) => Poll::Ready(Some(item)),
Poll::Ready(None) => Poll::Ready(None),
Expand Down Expand Up @@ -396,10 +393,7 @@ impl<F: Future> DerefMut for Keyed<F> {
impl<F: Future> Stream for Keyed<F> {
type Item = (Key, <F as Future>::Output);

fn poll_next(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
this.group.as_mut().poll_next_inner(cx)
}
Expand All @@ -408,8 +402,8 @@ impl<F: Future> Stream for Keyed<F> {
#[cfg(test)]
mod test {
use super::FutureGroup;
use core::future;
use futures_lite::prelude::*;
use std::future;

#[test]
fn smoke() {
Expand Down
2 changes: 1 addition & 1 deletion src/future/futures_ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::future::Join;
use crate::future::Race;
use core::future::IntoFuture;
use futures_core::Future;
use std::future::IntoFuture;

use super::join::tuple::Join2;
use super::race::tuple::Race2;
Expand Down
18 changes: 10 additions & 8 deletions src/future/join/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::utils::{FutureArray, OutputArray, PollArray, WakerArray};
use core::fmt;
use core::future::{Future, IntoFuture};
use core::mem::ManuallyDrop;
use core::ops::DerefMut;
use core::pin::Pin;
use core::task::{Context, Poll};
use std::ops::DerefMut;

use pin_project::{pin_project, pinned_drop};

Expand Down Expand Up @@ -93,7 +93,7 @@ where
"Futures must not be polled after completing"
);

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

// Lock readiness so we can use it again
readiness = this.wakers.readiness().lock().unwrap();
readiness = this.wakers.readiness();
}
}

Expand Down Expand Up @@ -178,12 +178,8 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::utils::DummyWaker;

use std::future;
use std::future::Future;
use std::sync::Arc;
use std::task::Context;
use core::future;

#[test]
fn smoke() {
Expand All @@ -203,7 +199,13 @@ mod test {
}

#[test]
#[cfg(feature = "alloc")]
fn debug() {
use crate::utils::DummyWaker;
use alloc::format;
use alloc::sync::Arc;
use core::task::Context;

let mut fut = [future::ready("hello"), future::ready("world")].join();
assert_eq!(format!("{:?}", fut), "[Pending, Pending]");
let mut fut = Pin::new(&mut fut);
Expand Down
1 change: 1 addition & 0 deletions src/future/join/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::future::Future;

pub(crate) mod array;
pub(crate) mod tuple;
#[cfg(feature = "alloc")]
pub(crate) mod vec;

/// Wait for all futures to complete.
Expand Down
14 changes: 7 additions & 7 deletions src/future/join/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::utils::{PollArray, WakerArray};

use core::fmt::{self, Debug};
use core::future::{Future, IntoFuture};
use core::mem::MaybeUninit;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::ops::DerefMut;
use core::pin::Pin;
use core::task::{Context, Poll};
use std::mem::ManuallyDrop;
use std::ops::DerefMut;

use pin_project::{pin_project, pinned_drop};

Expand Down Expand Up @@ -136,7 +135,7 @@ macro_rules! impl_join_tuple {
};
($mod_name:ident $StructName:ident $($F:ident)+) => {
mod $mod_name {
use std::mem::ManuallyDrop;
use core::mem::ManuallyDrop;

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

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

let mut readiness = this.wakers.readiness().lock().unwrap();
let mut readiness = this.wakers.readiness();
readiness.set_waker(cx.waker());

for index in 0..LEN {
Expand Down Expand Up @@ -234,7 +233,7 @@ macro_rules! impl_join_tuple {

return Poll::Ready(out);
}
readiness = this.wakers.readiness().lock().unwrap();
readiness = this.wakers.readiness();
}

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

#[test]
#[allow(clippy::unit_cmp)]
Expand Down Expand Up @@ -332,6 +331,7 @@ mod test {
}

#[test]
#[cfg(feature = "std")]
fn does_not_leak_memory() {
use core::cell::RefCell;
use futures_lite::future::pending;
Expand Down
20 changes: 11 additions & 9 deletions src/future/join/vec.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::Join as JoinTrait;
use crate::utils::{FutureVec, OutputVec, PollVec, WakerVec};

use alloc::vec::Vec;
use core::fmt;
use core::future::{Future, IntoFuture};
use core::mem::ManuallyDrop;
use core::ops::DerefMut;
use core::pin::Pin;
use core::task::{Context, Poll};
use std::mem::ManuallyDrop;
use std::ops::DerefMut;
use std::vec::Vec;

use pin_project::{pin_project, pinned_drop};

Expand Down Expand Up @@ -85,7 +85,7 @@ where
"Futures must not be polled after completing"
);

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

// Lock readiness so we can use it again
readiness = this.wakers.readiness().lock().unwrap();
readiness = this.wakers.readiness();
}
}

Expand Down Expand Up @@ -174,10 +174,12 @@ mod test {
use super::*;
use crate::utils::DummyWaker;

use std::future;
use std::future::Future;
use std::sync::Arc;
use std::task::Context;
use alloc::format;
use alloc::sync::Arc;
use alloc::vec;
use core::future;
use core::future::Future;
use core::task::Context;

#[test]
fn smoke() {
Expand Down
2 changes: 2 additions & 0 deletions src/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
//! complete, or return an `Err` if *no* futures complete successfully.
//!
#[doc(inline)]
#[cfg(feature = "alloc")]
pub use future_group::FutureGroup;
pub use futures_ext::FutureExt;
pub use join::Join;
Expand All @@ -77,6 +78,7 @@ pub use race_ok::RaceOk;
pub use try_join::TryJoin;

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

mod futures_ext;
Expand Down
2 changes: 1 addition & 1 deletion src/future/race/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ where
#[cfg(test)]
mod test {
use super::*;
use std::future;
use core::future;

// NOTE: we should probably poll in random order.
#[test]
Expand Down
1 change: 1 addition & 0 deletions src/future/race/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use core::future::Future;

pub(crate) mod array;
pub(crate) mod tuple;
#[cfg(feature = "alloc")]
pub(crate) mod vec;

/// Wait for the first future to complete.
Expand Down
2 changes: 1 addition & 1 deletion src/future/race/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl_race_tuple! { Race12 A B C D E F G H I J K L }
#[cfg(test)]
mod test {
use super::*;
use std::future;
use core::future;

#[test]
fn race_1() {
Expand Down
4 changes: 3 additions & 1 deletion src/future/race/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::utils::{self, Indexer};

use super::Race as RaceTrait;

use alloc::vec::Vec;
use core::fmt;
use core::future::{Future, IntoFuture};
use core::pin::Pin;
Expand Down Expand Up @@ -81,7 +82,8 @@ where
#[cfg(test)]
mod test {
use super::*;
use std::future;
use alloc::vec;
use core::future;

// NOTE: we should probably poll in random order.
#[test]
Expand Down
Loading

0 comments on commit d6f70c7

Please sign in to comment.