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

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

#880 in Asynchronous

Download history 3136/week @ 2024-06-20 2745/week @ 2024-06-27 3337/week @ 2024-07-04 4460/week @ 2024-07-11 2385/week @ 2024-07-18 1962/week @ 2024-07-25 2507/week @ 2024-08-01 1559/week @ 2024-08-08 1793/week @ 2024-08-15 1997/week @ 2024-08-22 1911/week @ 2024-08-29 1651/week @ 2024-09-05 1641/week @ 2024-09-12 1468/week @ 2024-09-19 1574/week @ 2024-09-26 1210/week @ 2024-10-03

6,132 downloads per month
Used in 4 crates

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

~335–470KB