async-deferred 0.4.0

A lightweight utility for spawning async tasks with fire-and-forget semantics and deferred result retrieval.
Documentation

async-deferred

CI crates.io docs.rs License no_std

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 Spawner trait
  • no_std: works on embedded executors such as embassy, with a heap allocator or, with StaticDeferred, without one

The result type only needs to be Send + 'static, the same as tokio::spawn.

Examples

Non-blocking Result Checking

use async_deferred::Deferred;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let mut deferred = Deferred::start(async {
        sleep(Duration::from_millis(100)).await;
        "Hello, World!"
    });

    // Check if ready without blocking
    match deferred.try_get() {
        Some(result) => println!("Result ready: {result}"),
        None => println!("Still computing..."),
    }
}

With Completion Callbacks

use async_deferred::Deferred;

#[tokio::main]
async fn main() {
    let mut deferred = Deferred::start_with_callback(
        async { 6 * 7 },
        |result| println!("Computation finished with {result}"),
    );

    deferred.join().await.unwrap();
}

Await for a result

use async_deferred::Deferred;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    // Start a computation immediately
    let mut deferred = Deferred::start(async {
        sleep(Duration::from_millis(100)).await;
        42
    });

    // Do other work while computation runs
    println!("Doing other work...");

    // Get the result when ready
    let result = deferred.join().await;
    assert_eq!(result, Ok(&42));

    // Or take ownership of the result: `.await` consumes the Deferred
    let deferred = Deferred::start(async { String::from("owned") });
    let value: String = deferred.await.unwrap();
    assert_eq!(value, "owned");
}

Error Handling

use async_deferred::{Deferred, Error};

#[tokio::main]
async fn main() {
    let mut deferred: Deferred<u32> = Deferred::start(async {
        panic!("Something went wrong!");
    });

    match deferred.join().await {
        Ok(result) => println!("Result: {result}"),
        Err(Error::Panicked(msg)) => println!("Task panicked: {msg}"),
        Err(err) => println!("No result: {err}"),
    }
}

Cancellation

use async_deferred::{Deferred, State};

#[tokio::main]
async fn main() {
    let mut deferred = Deferred::start(std::future::pending::<u32>());

    assert!(deferred.cancel());
    assert_eq!(deferred.state(), State::NotStarted);

    // The Deferred can be reused
    deferred.begin(async { 100 }).unwrap();
    assert_eq!(deferred.join().await, Ok(&100));
}

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:

async-deferred = { version = "0.4", default-features = false, features = ["smol"] }

smol

use async_deferred::{Deferred, Smol};

fn main() {
    let mut deferred = Deferred::start_on(&Smol, async { 42 }).unwrap();
    assert_eq!(smol::block_on(deferred.join()), Ok(&42));
}

Your own runtime

Any runtime works by implementing Spawner:

use std::future::Future;
use async_deferred::{Deferred, SpawnError, Spawner};

struct MyRuntime;

impl Spawner for MyRuntime {
    fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
    where
        F: Future<Output = ()> + Send + 'static,
    {
        // Hand `task` to your runtime here, or return a `SpawnError` if it can't take it.
        std::thread::spawn(move || futures_executor::block_on(task));
        Ok(())
    }
}

let mut deferred = Deferred::start_on(&MyRuntime, async { 42 }).unwrap();

For single-threaded executors and futures that are not Send, implement LocalSpawner and use the *_local_on methods.

Embassy (no_std)

async-deferred = { version = "0.4", default-features = false, features = ["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 async_deferred::{embassy_spawner, Deferred};
use embassy_time::{Duration, Instant, Timer};

// Lets up to 4 `Deferred` tasks run at the same time.
embassy_spawner!(DeferredSpawner, pool_size = 4);

#[embassy_executor::main]
async fn main(spawner: embassy_executor::Spawner) {
    // ... set up the heap allocator and the time driver ...
    let spawner = DeferredSpawner(spawner);

    let mut measurement = Deferred::start_local_on(&spawner, read_sensor()).unwrap();
    let mut started = Instant::now();

    loop {
        // The main loop's own work.
        Timer::after(Duration::from_millis(100)).await;

        // Check for the result without waiting.
        if let Some(value) = measurement.take() {
            defmt::info!("temperature: {}", value);
        } else if started.elapsed() > Duration::from_millis(500) {
            // The sensor hung: give up, and wait until its task has freed its slot.
            measurement.cancel_and_wait().await;
        } else {
            continue;
        }

        // `take` and `cancel` reset the `Deferred`, so it can start the next measurement.
        if measurement.begin_local_on(&spawner, read_sensor()).is_err() {
            defmt::error!("could not start a measurement");
        }
        started = Instant::now();
    }
}

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)

async-deferred = { version = "0.4", default-features = false, features = ["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 async_deferred::{StaticDeferred, Ticket};

static MEASUREMENT: StaticDeferred<u32> = StaticDeferred::new();

#[embassy_executor::task]
async fn measurement_task(ticket: Ticket<'static, u32>) {
    ticket.run(read_sensor()).await;
}

#[embassy_executor::main]
async fn main(spawner: embassy_executor::Spawner) {
    // If the task can't be spawned, the ticket is dropped and `MEASUREMENT` resets.
    if let Ok(ticket) = MEASUREMENT.begin() {
        if let Ok(token) = measurement_task(ticket) {
            spawner.spawn(token);
        }
    }

    // ... do other work ...

    // Check for the result without waiting.
    if let Some(value) = MEASUREMENT.take() {
        defmt::info!("temperature: {}", value);
    }
}

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.