#async #async-task #run-time #dispatcher #task-scheduling #pluggable #scheduler

async-dispatcher

async runtime based on a pluggable dispatcher

3 releases

0.1.2 May 31, 2024
0.1.1 May 31, 2024
0.1.0 May 31, 2024

#1463 in Asynchronous

Download history 1524/week @ 2024-10-26 1475/week @ 2024-11-02 1615/week @ 2024-11-09 1736/week @ 2024-11-16 1602/week @ 2024-11-23 1731/week @ 2024-11-30 1503/week @ 2024-12-07 1430/week @ 2024-12-14 934/week @ 2024-12-21 1530/week @ 2024-12-28 2761/week @ 2025-01-04 2371/week @ 2025-01-11 1743/week @ 2025-01-18 1914/week @ 2025-01-25 2166/week @ 2025-02-01 2245/week @ 2025-02-08

8,310 downloads per month
Used in 5 crates (4 directly)

Apache-2.0

8KB
166 lines

Async Dispatcher

crates.io version docs CI

This crate allows async libraries to spawn tasks and set timers without being tied to a particular async runtime.

The core of this need comes from wanting to be able to use the native OS scheduler, as written about in Zed Decoded: Async Rust.

Libraries can spawn in a generic way:

use async_dispatcher::{spawn, sleep};

pub async my_library_function() {
    let task = spawn(async {
        sleep(Duration::from_secs(1)).await;
        println!("in a spawned task!");
    });

    // ...
}

Applications using those libraries can control how that work is dispatched by implementing the Dispatcher trait:

use async_dispatcher::{set_dispatcher, Dispatcher, Runnable};

struct MyAppDispatcher;

impl Dispatcher for MyAppDispatcher {
    fn dispatch(&self, runnable: Runnable) {
        // ...
    }

    fn dispatch_after(&self, duration: Duration, runnable: Runnable) {
        // ...
    }
}

fn main() {
    set_dispatcher(MyAppDispatcher);

    async_dispatcher::block_on(async move {
        my_library_function().await;
    });
}

Dependencies

~350–475KB