async-deferred
A lightweight utility for fire-and-forget async computations in Rust. Start asynchronous tasks immediately and retrieve their results later without blocking. Works with any async runtime.
Features
- Fire-and-forget pattern: start computations without waiting for results
- Deferred retrieval: check results when convenient using non-blocking operations
- Panic handling: detect if the task or its callback panicked, and why
- Callback support: run code with the result as soon as the task finishes
- Cancellation: stop a running task and reuse the
Deferred - Any runtime: Tokio by default, smol behind a feature, or your own through the
Spawnertrait no_std: works on embedded executors such as embassy, with a heap allocator or, withStaticDeferred, without one
The result type only needs to be Send + 'static, the same as tokio::spawn.
Examples
Non-blocking Result Checking
use Deferred;
use Duration;
use sleep;
async
With Completion Callbacks
use Deferred;
async
Await for a result
use Deferred;
use Duration;
use sleep;
async
Error Handling
use ;
async
Cancellation
use ;
async
Runtimes
Tasks are started through a spawner, which hands them to an async runtime.
| Feature | Spawner | Notes |
|---|---|---|
tokio (default) |
Tokio, tokio::runtime::Handle, tokio::task::LocalSet (local) |
Enables the start and begin shortcuts used above |
smol |
Smol, smol::Executor, smol::LocalExecutor (local) |
|
std (default) |
Catches panics in the task and callback. Without it, the crate is no_std |
|
alloc (with std) |
Deferred, which keeps its task on the heap. Needs atomic compare-and-swap |
|
static-deferred |
StaticDeferred, which needs no heap |
To use another runtime without pulling in Tokio:
= { = "0.4", = false, = ["smol"] }
smol
use ;
Your own runtime
Any runtime works by implementing Spawner:
use Future;
use ;
;
let mut deferred = start_on.unwrap;
For single-threaded executors and futures that are not Send, implement LocalSpawner
and use the *_local_on methods.
Embassy (no_std)
= { = "0.4", = false, = ["alloc"] }
embassy_spawner! declares a spawner for embassy in one line. You also need a heap allocator,
such as esp-alloc or embedded-alloc, and a target with atomic compare-and-swap. On chips
without it, such as Cortex-M0 (RP2040) or the ESP32-C3, use StaticDeferred below.
use ;
use ;
// Lets up to 4 `Deferred` tasks run at the same time.
embassy_spawner!;
async
The futures can hold values that are not Send, such as Rc or peripheral drivers.
Starting a task while pool_size tasks are running returns a SpawnError instead of
panicking. A cancelled task frees its slot the next time the executor runs it: use
cancel_and_wait to restart right away in a full pool.
Without the std feature, panics are not caught, so TaskPanicked and CallbackPanicked
are never reported. On embedded targets a panic usually halts the device anyway.
A complete example for the ESP32-S3, with tests that run on the chip, is in
embassy-esp32s3/.
Without a heap (StaticDeferred)
= { = "0.4", = false, = ["static-deferred"] }
StaticDeferred keeps the result in a static instead of on the heap, so it works on targets
without an allocator and keeps memory use fixed. Each kind of job gets its own static and
its own task: begin returns a Ticket, which the task uses to run the job.
use ;
static MEASUREMENT: = new;
async
async
All methods take &self, so the static can be used from any task. It has state, take,
join, cancel and cancel_and_wait like Deferred, but no callbacks, and panics in the
job are not caught. It needs a
critical-section implementation, which embassy and the
HALs provide. On std, add critical-section = { version = "1", features = ["std"] } to your
dependencies; without one, linking fails with undefined symbol: _critical_section_1_0_acquire.
A complete example for the ESP32-S3, with tests that run on the chip, is in
embassy-esp32s3-no-heap/.
License
Licensed under the MIT license.