pub struct Spawner { /* private fields */ }Expand description
The capability to schedule detached work on one executor.
Acquired once with Spawner::current; afterwards Spawner::spawn needs no runtime
on the calling thread (the native spawner pins the executor it was acquired on).
Invariant: a spawner does not keep its executor alive. Once a native runtime has shut
down, work handed to its spawner is dropped unpolled — see Spawner::spawn.
Implementations§
Source§impl Spawner
impl Spawner
Sourcepub fn current() -> Result<Self, RuntimeUnavailable>
pub fn current() -> Result<Self, RuntimeUnavailable>
The executor current on this thread.
Post: Err iff native code runs outside a Tokio runtime.
Sourcepub fn spawn<F>(&self, future: F)
pub fn spawn<F>(&self, future: F)
Run future detached on this spawner’s executor.
Post: the executor owns future and runs it to completion while it is alive. If the
executor has shut down, future is dropped unpolled and spawn cannot report it;
work whose loss must be observed goes through Spawner::run_detached, which reports
it as Abandoned.
Sourcepub fn run_detached<F, T>(
&self,
future: F,
) -> impl Future<Output = Result<T, Abandoned>> + MaybeSend + 'static
pub fn run_detached<F, T>( &self, future: F, ) -> impl Future<Output = Result<T, Abandoned>> + MaybeSend + 'static
Start future on this spawner’s executor now and return a future of its output.
Law: the executor, not the returned future, owns the work — dropping the returned future abandons only the wait, and the work still runs to completion.