Skip to content

Commit

Permalink
Add Yielder trait
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Apr 23, 2024
1 parent 3932f12 commit 71c498c
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 16 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ members = [
resolver = "2"

[workspace.package]
version = "0.3.2"
edition = "2021"
license = "MIT/Apache-2.0"
rust-version = "1.75.0"
Expand All @@ -20,8 +19,8 @@ keywords = ["async", "runtime", "agnostic", "trait"]
[workspace.dependencies]
async-io = "2"

futures-util = { version = "=0.3.29", default-features = false }
futures-channel = { version = "=0.3.29" }
futures-util = { version = "0.3", default-features = false }
futures-channel = "0.3"
pin-project-lite = "0.2"
agnostic-lite = { path = "./lite", version = "0.3" }

Expand Down
2 changes: 1 addition & 1 deletion agnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ hickory-resolver = { version = "0.24", default-features = false, optional = true
[dependencies]
agnostic-lite = { workspace = true, features = ["time"] }
async-trait = "0.1"
futures-util = { version = "=0.3.29" }
futures-util = "0.3"

tokio = { version = "1", default-features = false, optional = true }
tokio-stream = { version = "0.1", default-features = false, optional = true, features = [
Expand Down
14 changes: 11 additions & 3 deletions lite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "agnostic-lite"
version = "0.3.12"
version = "0.3.13"
edition.workspace = true
license.workspace = true
rust-version.workspace = true
Expand Down Expand Up @@ -53,6 +53,12 @@ wasm = [

test = ["time"]

[target.'cfg(target_family = "wasm")'.dependencies]
wasm = { workspace = true, features = ["wasm-bindgen"], optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
wasm = { workspace = true, optional = true }

[dependencies]
atomic-time = { version = "0.1", optional = true }
futures-util.workspace = true
Expand All @@ -66,9 +72,11 @@ tokio = { version = "1", optional = true, default-features = false, features = [
async-io = { workspace = true, optional = true }
async-std = { workspace = true, optional = true, features = ["default", "unstable"] }
smol = { workspace = true, optional = true }
wasm = { workspace = true, optional = true }
paste = { version = "1", optional = true }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3"

[dev-dependencies]
futures = { version = "=0.3.29", features = ["executor"] }
futures = { version = "0.3", features = ["executor"] }
tokio = { workspace = true, features = ["full"] }
8 changes: 7 additions & 1 deletion lite/src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ pub use after::*;
#[cfg(feature = "time")]
use std::time::{Duration, Instant};

use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner};
use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner, Yielder};

impl<F> super::Detach for ::async_std::task::JoinHandle<F> {}

/// A [`AsyncSpawner`] that uses the [`async-std`](async_std) runtime.
#[derive(Debug, Clone, Copy)]
pub struct AsyncStdSpawner;

impl Yielder for AsyncStdSpawner {
async fn yield_now() {
::async_std::task::yield_now().await
}
}

impl AsyncSpawner for AsyncStdSpawner {
type JoinHandle<F> = ::async_std::task::JoinHandle<F> where F: Send + 'static;

Expand Down
6 changes: 6 additions & 0 deletions lite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ pub mod async_io;
mod spawner;
pub use spawner::*;

/// Yielder hints the runtime to execution back
pub trait Yielder {
/// Yields execution back to the runtime.
fn yield_now() -> impl Future<Output = ()>;
}

/// Runtime trait
pub trait RuntimeLite: Sized + Unpin + Copy + Send + Sync + 'static {
/// The spawner type for this runtime
Expand Down
8 changes: 7 additions & 1 deletion lite/src/smol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use after::*;

#[cfg(feature = "time")]
use crate::async_io::*;
use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner};
use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner, Yielder};
#[cfg(feature = "time")]
use std::time::{Duration, Instant};

Expand All @@ -21,6 +21,12 @@ impl<T> super::Detach for ::smol::Task<T> {
#[derive(Debug, Clone, Copy)]
pub struct SmolSpawner;

impl Yielder for SmolSpawner {
async fn yield_now() {
::smol::future::yield_now().await
}
}

impl AsyncSpawner for SmolSpawner {
type JoinHandle<F> = ::smol::Task<F> where F: Send + 'static;

Expand Down
8 changes: 5 additions & 3 deletions lite/src/spawner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::future::Future;

use crate::Yielder;

/// Detaches the task related to the join handle to let it keep running in the background.
pub trait Detach: Sized {
/// Detaches the task to let it keep running in the background.
Expand All @@ -12,7 +14,7 @@ pub trait Detach: Sized {
impl<T> Detach for std::thread::JoinHandle<T> {}

/// A spawner trait for spawning futures.
pub trait AsyncSpawner: Copy + Send + Sync + 'static {
pub trait AsyncSpawner: Yielder + Copy + Send + Sync + 'static {
/// The handle returned by the spawner when a future is spawned.
type JoinHandle<F>: Detach + Future + Send + Sync + 'static
where
Expand All @@ -35,7 +37,7 @@ pub trait AsyncSpawner: Copy + Send + Sync + 'static {
}

/// A spawner trait for spawning futures.
pub trait AsyncLocalSpawner: Copy + 'static {
pub trait AsyncLocalSpawner: Yielder + Copy + 'static {
/// The handle returned by the spawner when a future is spawned.
type JoinHandle<F>: Detach + Future + 'static
where
Expand All @@ -58,7 +60,7 @@ pub trait AsyncLocalSpawner: Copy + 'static {
}

/// A spawner trait for spawning blocking.
pub trait AsyncBlockingSpawner: Copy + 'static {
pub trait AsyncBlockingSpawner: Yielder + Copy + 'static {
/// The join handle type for blocking tasks
type JoinHandle<R>: Detach
where
Expand Down
8 changes: 7 additions & 1 deletion lite/src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@ pub use after::*;

use core::future::Future;

use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner};
use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner, Yielder};

/// A [`AsyncSpawner`] that uses the [`tokio`] runtime.
#[derive(Debug, Clone, Copy)]
pub struct TokioSpawner;

impl Yielder for TokioSpawner {
async fn yield_now() {
::tokio::task::yield_now().await
}
}

impl AsyncSpawner for TokioSpawner {
type JoinHandle<F> = tokio::task::JoinHandle<F> where
F: Send + 'static;
Expand Down
8 changes: 7 additions & 1 deletion lite/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::time::{Duration, Instant};

use wasm::channel::*;

use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner};
use crate::{AsyncBlockingSpawner, AsyncLocalSpawner, AsyncSpawner, Yielder};

impl<T> super::Detach for WasmJoinHandle<T> {}

Expand Down Expand Up @@ -134,6 +134,12 @@ impl AsyncBlockingSpawner for WasmSpawner {
}
}

impl Yielder for WasmSpawner {
async fn yield_now() {
YieldNow(false).await
}
}

/// Future for the [`yield_now`](RuntimeLite::yield_now) function.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
Expand Down
36 changes: 36 additions & 0 deletions lite/src/wasm/after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,39 @@ impl AsyncLocalAfterSpawner for WasmSpawner {
spawn_after!(spawn_local, sleep_local_until(AsyncLocalSleep) -> (instant, future))
}
}

#[cfg(all(test, target_arch = "wasm32"))]
mod tests {
use super::*;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
async fn test_after_handle() {
crate::tests::spawn_after_unittest::<WasmRuntime>().await;
}

#[wasm_bindgen_test]
async fn test_after_drop() {
crate::tests::spawn_after_drop_unittest::<WasmRuntime>().await;
}

#[wasm_bindgen_test]
async fn test_after_cancel() {
crate::tests::spawn_after_cancel_unittest::<WasmRuntime>().await;
}

#[wasm_bindgen_test]
async fn test_after_abort() {
crate::tests::spawn_after_abort_unittest::<WasmRuntime>().await;
}

#[wasm_bindgen_test]
async fn test_after_reset_to_pass() {
crate::tests::spawn_after_reset_to_pass_unittest::<WasmRuntime>().await;
}

#[wasm_bindgen_test]
async fn test_after_reset_to_future() {
crate::tests::spawn_after_reset_to_future_unittest::<WasmRuntime>().await;
}
}
5 changes: 3 additions & 2 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "feature-extension-for-wasm-bindgen-futures"
version = "0.2.0"
version = "0.2.1"
edition.workspace = true
license.workspace = true
rust-version.workspace = true
Expand All @@ -10,8 +10,9 @@ description = "Feature extension for wasm-bindgen-futures"

[features]
default = []
time = ["futures-timer/wasm-bindgen"]
time = ["futures-timer"]
channel = ["futures-channel"]
wasm-bindgen = ["futures-timer?/wasm-bindgen"]

[dependencies]
futures-timer = { version = "3", optional = true }
Expand Down

0 comments on commit 71c498c

Please sign in to comment.