Skip to content

Commit

Permalink
Add Send bound for JoinHandle::Output
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed Mar 26, 2024
1 parent c9e9267 commit 7ed7ceb
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 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
9 changes: 9 additions & 0 deletions lite/src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@ impl AsyncSpawner for AsyncStdSpawner {
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
<<Self as AsyncSpawner>::JoinHandle<F> as Future>::Output: Send,
{
::async_std::task::spawn(future)
}

fn spawn_detach<F>(future: F)
where
F::Output: Send + 'static,
F: Future + Send + 'static,
{
drop(::async_std::task::spawn(future));
}
}

impl AsyncLocalSpawner for AsyncStdSpawner {
Expand Down
25 changes: 22 additions & 3 deletions lite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub trait RuntimeLite: Sized + Unpin + Copy + Send + Sync + 'static {
where
F::Output: Send + 'static,
F: Future + Send + 'static,
<<Self::Spawner as AsyncSpawner>::JoinHandle<F> as Future>::Output: Send,
{
<Self::Spawner as AsyncSpawner>::spawn(future)
}
Expand Down Expand Up @@ -383,13 +384,21 @@ pub trait RuntimeLite: Sized + Unpin + Copy + Send + Sync + 'static {
#[cfg(any(test, feature = "test"))]
#[cfg_attr(docsrs, doc(cfg(any(test, feature = "test"))))]
pub mod tests {
use core::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
#[cfg(feature = "time")]
use std::{
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
time::Duration,
};

#[cfg(feature = "time")]
use super::{AfterHandle, RuntimeLite};

/// Unit test for the [`RuntimeLite::spawn_after`] function
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub async fn spawn_after_unittest<R: RuntimeLite>() {
let ctr = Arc::new(AtomicUsize::new(1));
let ctr1 = ctr.clone();
Expand All @@ -407,6 +416,8 @@ pub mod tests {
/// Unit test for the [`RuntimeLite::spawn_after`] function
///
/// The task will be canceled before it completes
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub async fn spawn_after_cancel_unittest<R: RuntimeLite>() {
let ctr = Arc::new(AtomicUsize::new(1));
let ctr1 = ctr.clone();
Expand All @@ -425,6 +436,8 @@ pub mod tests {
/// Unit test for the [`RuntimeLite::spawn_after`] function
///
/// The [`AfterHandle`] will be dropped immediately after it is created
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub async fn spawn_after_drop_unittest<R: RuntimeLite>() {
let ctr = Arc::new(AtomicUsize::new(1));
let ctr1 = ctr.clone();
Expand All @@ -442,6 +455,8 @@ pub mod tests {
/// Unit test for the [`RuntimeLite::spawn_after`] function
///
/// The [`AfterHandle`] will be abort after it is created, and the task will not be executed.
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub async fn spawn_after_abort_unittest<R: RuntimeLite>() {
let ctr = Arc::new(AtomicUsize::new(1));
let ctr1 = ctr.clone();
Expand All @@ -460,6 +475,8 @@ pub mod tests {
/// Unit test for the [`RuntimeLite::spawn_after`] function
///
/// The [`AfterHandle`] will be reset to passed than the original duration after it is created, and the task will be executed after the reset duration.
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub async fn spawn_after_reset_to_pass_unittest<R: RuntimeLite>() {
let ctr = Arc::new(AtomicUsize::new(1));
let ctr1 = ctr.clone();
Expand All @@ -478,6 +495,8 @@ pub mod tests {
/// Unit test for the [`RuntimeLite::spawn_after`] function
///
/// The [`AfterHandle`] will be reset to future than the original duration after it is created, and the task will be executed after the reset duration.
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub async fn spawn_after_reset_to_future_unittest<R: RuntimeLite>() {
let ctr = Arc::new(AtomicUsize::new(1));
let ctr1 = ctr.clone();
Expand Down
3 changes: 2 additions & 1 deletion lite/src/smol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ impl AsyncSpawner for SmolSpawner {
fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
F: Future + Send + 'static,
<<Self as AsyncSpawner>::JoinHandle<F> as Future>::Output: Send,
{
::smol::spawn(future)
}
Expand Down
10 changes: 4 additions & 6 deletions lite/src/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ pub trait AsyncSpawner: Copy + Send + Sync + 'static {
fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
where
F::Output: Send + 'static,
F: Future + Send + 'static;
F: Future + Send + 'static,
<<Self as AsyncSpawner>::JoinHandle<F> as Future>::Output: Send;

/// Spawn a future and detach it.
fn spawn_detach<F>(future: F)
where
F::Output: Send + 'static,
F: Future + Send + 'static,
{
core::mem::drop(Self::spawn(future));
}
F: Future + Send + 'static;
}

/// A spawner trait for spawning futures.
Expand Down Expand Up @@ -60,7 +58,7 @@ pub trait AsyncLocalSpawner: Copy + 'static {
/// A spawner trait for spawning blocking.
pub trait AsyncBlockingSpawner: Copy + 'static {
/// The join handle type for blocking tasks
type JoinHandle<R>: Detach
type JoinHandle<R>: Detach + Send
where
R: Send + 'static;

Expand Down
9 changes: 9 additions & 0 deletions lite/src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ impl AsyncSpawner for TokioSpawner {
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
<<Self as AsyncSpawner>::JoinHandle<F> as Future>::Output: Send,
{
::tokio::task::spawn(future)
}

fn spawn_detach<F>(future: F)
where
F::Output: Send + 'static,
F: Future + Send + 'static,
{
drop(::tokio::task::spawn(future));
}
}

impl AsyncLocalSpawner for TokioSpawner {
Expand Down
9 changes: 9 additions & 0 deletions lite/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ impl AsyncSpawner for WasmSpawner {
where
F::Output: Send + 'static,
F: core::future::Future + Send + 'static,
<<Self as AsyncSpawner>::JoinHandle<F> as Future>::Output: Send,
{
<Self as super::AsyncLocalSpawner>::spawn_local(future)
}

fn spawn_detach<F>(future: F)
where
F::Output: Send + 'static,
F: Future + Send + 'static,
{
<Self as super::AsyncLocalSpawner>::spawn_local_detach(future)
}
}

impl AsyncLocalSpawner for WasmSpawner {
Expand Down

0 comments on commit 7ed7ceb

Please sign in to comment.