[−][src]Function async_task_ffi::spawn_with
pub fn spawn_with<F, S, D>(
future: F,
schedule: S,
data: D
) -> (Runnable<D>, Task<F::Output>) where
F: Future + Send + 'static,
F::Output: Send + 'static,
S: Fn(Runnable<D>) + Send + Sync + 'static,
D: Send + 'static,
Creates a new task with associated data.
This function is the same as spawn()
, except it makes it possible to
attach arbitrary data to the task. This makes it possible to benefit from
the single allocation design of async_task_ffi
without having to write a
specialized implementation.
The data can be accessed using Runnable::data
or Runnable::data_mut
.
Examples
use async_task_ffi::Runnable; // The future inside the task. let future = async { println!("Hello, world!"); }; // A function that schedules the task when it gets woken up // and counts the amount of times it has been scheduled. let (s, r) = flume::unbounded(); let schedule = move |mut runnable: Runnable<usize>| { *runnable.data_mut() += 1; s.send(runnable).unwrap(); }; // Create a task with the future, the schedule function // and the initial data. let (runnable, task) = async_task_ffi::spawn_with(future, schedule, 0);