Skip to main content

AsyncSpawner

Trait AsyncSpawner 

Source
pub trait AsyncSpawner: Send + Sync {
    // Required methods
    fn spawn_supervised(
        &self,
        label: &'static str,
        task: BoxFuture<'static, ()>,
    ) -> Result<(), SpawnError>;
    fn spawn_blocking_erased(
        &self,
        label: &'static str,
        work: BoxedBlockingWork,
    ) -> Result<Receiver<ErasedBlockingResult>, SpawnError>;
    fn request_cancel(&self);
    fn is_cancelled(&self) -> bool;
    fn shutdown(&self) -> BoxFuture<'static, ()>;
}
Expand description

Object-safe host-provided async spawner.

spawn_blocking is exposed as an erased operation on the trait so the runtime can hold Arc<dyn AsyncSpawner>. Typed callers use the helper spawn_blocking wrapper below, which downcasts the boxed result back to the requested type.

Required Methods§

Source

fn spawn_supervised( &self, label: &'static str, task: BoxFuture<'static, ()>, ) -> Result<(), SpawnError>

Spawn a supervised async task.

Source

fn spawn_blocking_erased( &self, label: &'static str, work: BoxedBlockingWork, ) -> Result<Receiver<ErasedBlockingResult>, SpawnError>

Spawn a blocking task and return an erased result receiver.

Source

fn request_cancel(&self)

Broadcast cancellation to all supervised tasks.

Source

fn is_cancelled(&self) -> bool

Has cancellation already been requested?

Source

fn shutdown(&self) -> BoxFuture<'static, ()>

Request cancellation and wait for supervised tasks to terminate.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§