Skip to content

Commit

Permalink
support no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Mar 10, 2024
1 parent 4b7aec5 commit 7ba1731
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 221 deletions.
15 changes: 7 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@ homepage = "https://github.com/al8n/wg"
repository = "https://github.com/al8n/wg.git"
documentation = "https://docs.rs/wg/"
readme = "README.md"
version = "0.7.4"
version = "0.7.5"
license = "MIT OR Apache-2.0"
keywords = ["waitgroup", "async", "sync", "notify", "wake"]
categories = ["asynchronous", "concurrency", "data-structures"]
edition = "2021"

[features]
default = []
full = ["triomphe", "parking_lot"]
default = ["std", "parking_lot", "triomphe"]
std = ["triomphe?/default", "event-listener?/default", "futures-core?/default", "tokio?/rt"]
triomphe = ["dep:triomphe"]
parking_lot = ["dep:parking_lot"]

future = ["event-listener", "pin-project-lite"]

tokio = ["dep:tokio", "futures-core", "pin-project-lite"]

[dependencies]
parking_lot = { version = "0.12", optional = true }
triomphe = { version = "0.1", optional = true }
event-listener = { version = "5", optional = true }
triomphe = { version = "0.1", optional = true, default-features = false }
event-listener = { version = "5", optional = true, default-features = false }
pin-project-lite = { version = "0.2", optional = true }

tokio = { version = "1", default-features = false, optional = true, features = ["sync", "rt"] }
futures-core = { version = "0.3", optional = true }
tokio = { version = "1", optional = true, default-features = false, features = ["sync"] }
futures-core = { version = "0.3", default-features = false, optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["full"] }
Expand Down
23 changes: 15 additions & 8 deletions src/future.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use super::*;
use event_listener::{Event, EventListener};

use std::{
use core::{
pin::Pin,
sync::atomic::{AtomicUsize, Ordering},
task::{Context, Poll},
};

#[cfg(feature = "std")]
use std::sync::Arc;

#[cfg(not(feature = "std"))]
use alloc::sync::Arc;

#[derive(Debug)]
struct AsyncInner {
counter: AtomicUsize,
Expand Down Expand Up @@ -89,8 +94,8 @@ impl Clone for AsyncWaitGroup {
}
}

impl std::fmt::Debug for AsyncWaitGroup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for AsyncWaitGroup {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("AsyncWaitGroup")
.field("counter", &self.inner.counter)
.finish()
Expand Down Expand Up @@ -199,7 +204,7 @@ impl AsyncWaitGroup {
WaitGroupFuture {
inner: self,
notified: self.inner.event.listen(),
_pin: std::marker::PhantomPinned,
_pin: core::marker::PhantomPinned,
}
}

Expand Down Expand Up @@ -233,9 +238,11 @@ impl AsyncWaitGroup {
/// wg.block_wait(spawner);
/// # })
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn block_wait<S>(&self, spawner: S)
where
S: FnOnce(Pin<Box<dyn std::future::Future<Output = ()> + Send + 'static>>),
S: FnOnce(Pin<Box<dyn core::future::Future<Output = ()> + Send + 'static>>),
{
let this = self.clone();
let (tx, rx) = std::sync::mpsc::channel();
Expand All @@ -258,11 +265,11 @@ pin_project_lite::pin_project! {
#[pin]
notified: EventListener,
#[pin]
_pin: std::marker::PhantomPinned,
_pin: core::marker::PhantomPinned,
}
}

impl<'a> std::future::Future for WaitGroupFuture<'a> {
impl<'a> core::future::Future for WaitGroupFuture<'a> {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand Down
Loading

0 comments on commit 7ba1731

Please sign in to comment.