Skip to main content

spawn

Function spawn 

Source
pub fn spawn<F>(future: F) -> JoinHandle<F::Output> 
where F: Future + 'static, F::Output: 'static,
Available on Linux, or Windows, or AArch64 and macOS only.
Expand description

Spawns future onto the current runtime thread and returns a JoinHandle.

The future runs concurrently with other tasks on this thread. Awaiting the returned handle yields Result<T, JoinError>: Ok with the output, or Err(JoinError::Aborted) if the task was aborted. Dropping the handle detaches the task; it keeps running to completion.

The future is !Send and never migrates off this thread. It is first scheduled as a microtask; its first poll happens when the runtime drains the microtask queue. Subsequent wakeups are also scheduled as microtasks.

§Panics

Panics if the current thread’s runtime state or driver cannot be initialized.

§Examples

use std::rc::Rc;
use std::cell::Cell;

let out = Rc::new(Cell::new(0u32));
let sink = Rc::clone(&out);
runite::spawn(async move {
    let handle = runite::spawn(async { 21u32 });
    let value = handle.await.expect("task should not be aborted");
    sink.set(value * 2);
});
runite::run();
assert_eq!(out.get(), 42);