Skip to main content

AsyncLocalSpawner

Trait AsyncLocalSpawner 

Source
pub trait AsyncLocalSpawner:
    Yielder
    + Copy
    + 'static {
    type JoinHandle<O>: LocalJoinHandle<O> + 'static
       where O: 'static;

    // Required method
    fn spawn_local<F>(future: F) -> Self::JoinHandle<F::Output>
       where F::Output: 'static,
             F: Future + 'static;

    // Provided method
    fn spawn_local_detach<F>(future: F)
       where F::Output: 'static,
             F: Future + 'static { ... }
}
Expand description

A spawner trait for spawning futures.

§Local-executor context (contract note)

A local spawn needs a thread-local executor somebody drives, and not every runtime has an ambient one. An implementation may panic when the current thread has no local-executor context to target:

  • tokio: panics outside a LocalSet or LocalRuntime (tokio’s own documented spawn_local contract).
  • smol: always panics — smol has no ambient thread-local executor; drive a smol::LocalExecutor yourself and spawn onto it directly.
  • embassy: always panics — its global spawner is Send-only.

Required Associated Types§

Source

type JoinHandle<O>: LocalJoinHandle<O> + 'static where O: 'static

The handle returned by the spawner when a future is spawned.

Required Methods§

Source

fn spawn_local<F>(future: F) -> Self::JoinHandle<F::Output>
where F::Output: 'static, F: Future + 'static,

Spawn a future.

Provided Methods§

Source

fn spawn_local_detach<F>(future: F)
where F::Output: 'static, F: Future + 'static,

Spawn a future and detach it.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl AsyncLocalSpawner for EmbassySpawner

Available on crate feature embassy only.
Source§

type JoinHandle<F> = JoinHandle<F> where F: 'static

Source§

impl AsyncLocalSpawner for SmolSpawner

Available on crate feature smol only.

§Panics

Both local-spawn methods always panic: smol has no ambient thread-local executor to target, so a correct local spawn needs a smol::LocalExecutor the caller owns and drives — spawn onto it directly instead. (Before 0.7 these methods spawned onto a temporary LocalExecutor that dropped at the end of the statement, cancelling the task: a silent no-op for spawn_local_detach, and a “task polled after completion” panic on awaiting spawn_local’s handle. A loud, immediate panic replaces that silent loss.)

Source§

type JoinHandle<O> = JoinHandle<O> where O: 'static

Source§

impl AsyncLocalSpawner for TokioSpawner

Available on crate feature tokio only.
Source§

type JoinHandle<F> = JoinHandle<F> where F: 'static

Source§

impl AsyncLocalSpawner for WasmSpawner

Available on crate feature wasm only.
Source§

type JoinHandle<F> = JoinHandle<F> where F: 'static