smol-rs/async-task

Why manual call of schedule is needed on runnable?

Closed this issue · 2 comments

let schedule = |runnable| QUEUE.send(runnable).unwrap();
let (runnable, task) = async_task::spawn(async { 1 + 2 }, schedule);

// Schedule the task and await its output.
runnable.schedule(); <- this line
assert_eq!(smol::future::block_on(task), 3);

Frankly,it's not very ergonomic. Why not the future starting to be scheduled once it's awaited, just use Rust lazy future feature.

This is done so that you can manipulate the runnable before scheduling it. For instance, see these lines in async-executor:

https://github.com/smol-rs/async-executor/blob/a5ff8df7d94eb5f716132f6702d1d662a29034d6/src/lib.rs#L414-L419

Here we need to get the waker for the runnable before scheduling it. This would be impossible or at least very inefficient if the Runnable was only created when the future desired it.