pub trait Spawner {
// Required method
fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
where F: Future<Output = ()> + Send + 'static;
}Expand description
Runs a task in the background on an async runtime.
Implement this to use Deferred with any runtime. Implementations
for Tokio and smol are available behind the tokio (default) and smol features.
The runtime must keep polling task until it finishes. If the runtime stops first,
it must drop task, so the Deferred reports State::Cancelled
instead of staying pending. If the runtime can’t take the task, for example because its
task pool is full, return a SpawnError.
§Examples
use std::future::Future;
use async_deferred::{Deferred, SpawnError, Spawner};
struct ThreadSpawner;
impl Spawner for ThreadSpawner {
fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
where
F: Future<Output = ()> + Send + 'static,
{
std::thread::spawn(move || futures_executor::block_on(task));
Ok(())
}
}
let mut deferred = Deferred::start_on(&ThreadSpawner, async { 42 }).unwrap();
assert_eq!(futures_executor::block_on(deferred.join()), Ok(&42));Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl Spawner for Handle
Available on crate feature tokio only.Spawns on this Tokio runtime. Works from outside the runtime.
impl Spawner for Handle
Available on crate feature
tokio only.Spawns on this Tokio runtime. Works from outside the runtime.
Source§impl<'a> Spawner for Executor<'a>
Available on crate feature smol only.Spawns on this smol executor. The task only makes progress while the executor is run.
impl<'a> Spawner for Executor<'a>
Available on crate feature
smol only.Spawns on this smol executor. The task only makes progress while the executor is run.
Source§impl<S: Spawner + ?Sized> Spawner for Arc<S>
Available on target_has_atomic=ptr and crate feature alloc only.
impl<S: Spawner + ?Sized> Spawner for Arc<S>
Available on
target_has_atomic=ptr and crate feature alloc only.