Skip to main content

Spawner

Trait Spawner 

Source
pub trait Spawner {
    // Required method
    fn spawn<F>(&self, task: F)
       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.

§Examples

use std::future::Future;
use async_deferred::{Deferred, Spawner};

struct ThreadSpawner;

impl Spawner for ThreadSpawner {
    fn spawn<F>(&self, task: F)
    where
        F: Future<Output = ()> + Send + 'static,
    {
        std::thread::spawn(move || futures_executor::block_on(task));
    }
}

let mut deferred = Deferred::start_on(&ThreadSpawner, async { 42 });
assert_eq!(futures_executor::block_on(deferred.join()), Ok(&42));

Required Methods§

Source

fn spawn<F>(&self, task: F)
where F: Future<Output = ()> + Send + 'static,

Runs task in the background.

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 Executor<'static>

Available on crate feature smol only.

Spawns on this smol executor. The task only makes progress while the executor is run.

Source§

fn spawn<F>(&self, task: F)
where F: Future<Output = ()> + Send + 'static,

Source§

impl Spawner for Handle

Available on crate feature tokio only.

Spawns on this Tokio runtime. Works from outside the runtime.

Source§

fn spawn<F>(&self, task: F)
where F: Future<Output = ()> + Send + 'static,

Implementors§

Source§

impl Spawner for Smol

Available on crate feature smol only.
Source§

impl Spawner for Tokio

Available on crate feature tokio only.