Go to file
Taiki Endo 24adf39992 Always set #![no_std] to fix redundant import warning
```
error: the item `Box` is imported redundantly
 --> src/runnable.rs:9:5
  |
9 | use alloc::boxed::Box;
  |     ^^^^^^^^^^^^^^^^^
 --> /rustc/5119208fd78a77547c705d1695428c88d6791263/library/std/src/prelude/mod.rs:125:13
  |
  = note: the item `Box` is already defined here
  |
  = note: `-D unused-imports` implied by `-D warnings`
  = help: to override `-D warnings` add `#[allow(unused_imports)]`
```
2024-03-03 06:47:46 -08:00
.github ci: Use cargo-hack's --rust-version flag for msrv check 2024-01-07 13:20:09 +09:00
benches Docs and comments 2020-09-19 21:34:44 +02:00
examples Fix clippy::non_canonical_partial_ord_impl warning in example 2023-10-08 14:43:10 +09:00
src Always set #![no_std] to fix redundant import warning 2024-03-03 06:47:46 -08:00
tests Ignore dead_code warnings for tuple structs in tests 2024-01-07 13:20:09 +09:00
.gitignore Initial commit 2019-08-12 20:18:51 +02:00
CHANGELOG.md v4.7.0 2024-01-03 18:39:31 -08:00
Cargo.toml Update flaky_test requirement from 0.1 to 0.2 2024-01-22 10:55:06 +09:00
LICENSE-APACHE Initial commit 2019-08-12 20:18:51 +02:00
LICENSE-MIT Initial commit 2019-08-12 20:18:51 +02:00
README.md Update license badge to match Cargo.toml 2021-02-14 13:38:19 +09:00

README.md

async-task

Build License Cargo Documentation

Task abstraction for building executors.

To spawn a future onto an executor, we first need to allocate it on the heap and keep some state attached to it. The state indicates whether the future is ready for polling, waiting to be woken up, or completed. Such a stateful future is called a task.

All executors have a queue that holds scheduled tasks:

let (sender, receiver) = flume::unbounded();

A task is created using either spawn(), spawn_local(), or spawn_unchecked() which return a Runnable and a Task:

// A future that will be spawned.
let future = async { 1 + 2 };

// A function that schedules the task when it gets woken up.
let schedule = move |runnable| sender.send(runnable).unwrap();

// Construct a task.
let (runnable, task) = async_task::spawn(future, schedule);

// Push the task into the queue by invoking its schedule function.
runnable.schedule();

The Runnable is used to poll the task's future, and the Task is used to await its output.

Finally, we need a loop that takes scheduled tasks from the queue and runs them:

for runnable in receiver {
    runnable.run();
}

Method run() polls the task's future once. Then, the Runnable vanishes and only reappears when its Waker wakes the task, thus scheduling it to be run again.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.