[][src]Function async_task::spawn_local

pub fn spawn_local<F, R, S, T>(
    future: F,
    schedule: S,
    tag: T
) -> (Task<T>, JoinHandle<R, T>) where
    F: Future<Output = R> + 'static,
    R: 'static,
    S: Fn(Task<T>) + Send + Sync + 'static,
    T: Send + Sync + 'static, 

Creates a new local task.

This constructor returns a Task reference that runs the future and a JoinHandle that awaits its result.

When run, the task polls future. When woken up, it gets scheduled for running by the schedule function. Argument tag is an arbitrary piece of data stored inside the task.

The schedule function should not attempt to run the task nor to drop it. Instead, it should push the task into some kind of queue so that it can be processed later.

Unlike spawn, this function does not require the future to implement Send. If the Task reference is run or dropped on a thread it was not created on, a panic will occur.


use crossbeam::channel;

// The future inside the task.
let future = async {
    println!("Hello, world!");

// If the task gets woken up, it will be sent into this channel.
let (s, r) = channel::unbounded();
let schedule = move |task| s.send(task).unwrap();

// Create a task with the future and the schedule function.
let (task, handle) = async_task::spawn_local(future, schedule, ());