#timer #async #events

no-std async-timer

Timers for Rust async story

42 releases

1.0.0-beta.15 Sep 29, 2024
1.0.0-beta.14 Jun 25, 2024
1.0.0-beta.13 Mar 23, 2024
1.0.0-beta.11 Oct 27, 2023
0.2.10 Jul 15, 2019

#107 in WebAssembly

Download history 4941/week @ 2024-07-16 3059/week @ 2024-07-23 3455/week @ 2024-07-30 1542/week @ 2024-08-06 1628/week @ 2024-08-13 1750/week @ 2024-08-20 1933/week @ 2024-08-27 1688/week @ 2024-09-03 1436/week @ 2024-09-10 1808/week @ 2024-09-17 2958/week @ 2024-09-24 1896/week @ 2024-10-01 3153/week @ 2024-10-08 3348/week @ 2024-10-15 3069/week @ 2024-10-22 3263/week @ 2024-10-29

13,179 downloads per month
Used in 28 crates (15 directly)

BSL-1.0 license

65KB
1.5K SLoC

async-timer

Rust Crates.io Documentation dependency status

Timer facilities for Rust's async story

Accuracy

Regular timers that do not rely on async event loop tend to be on par with user space timers like in tokio. If that's not suitable for you you should enable event loop based timers which in most cases give you the most accurate timers possible on unix platforms (See features.)

Features

  • tokio1 - Enables event loop based timers using tokio, providing higher resolution timers on unix platforms.
  • c_wrapper - Uses C shim to create bindings to platform API, which may be more reliable than libc.
  • std - Enables usage of std types (e.g. Error)

Examples

Timed

async fn job() {
}

async fn do_job() {
    let work = unsafe {
        async_timer::Timed::platform_new_unchecked(job(), core::time::Duration::from_secs(1))
    };

    match work.await {
        Ok(_) => println!("I'm done!"),
        //You can retry by polling `expired`
        Err(expired) => println!("Job expired: {}", expired),
    }
}

Interval

async fn job() {
}

async fn do_a_while() {
    let mut times: u8 = 0;
    let mut interval = async_timer::Interval::platform_new(core::time::Duration::from_secs(1));

    while times < 5 {
        job().await;
        interval.wait().await;
        times += 1;
    }
}

Dependencies

~0–8MB
~63K SLoC