#timer #async #wasm

no-std async-timer

Timers for Rust async story

34 releases ()

1.0.0-beta.7 Dec 23, 2020
1.0.0-beta.5 Jul 27, 2020
0.7.4 Jul 27, 2020
0.7.3 Feb 19, 2020
0.2.10 Jul 15, 2019

#8 in WebAssembly

Download history 464/week @ 2020-11-07 530/week @ 2020-11-14 483/week @ 2020-11-21 552/week @ 2020-11-28 613/week @ 2020-12-05 503/week @ 2020-12-12 424/week @ 2020-12-19 340/week @ 2020-12-26 612/week @ 2021-01-02 447/week @ 2021-01-09 625/week @ 2021-01-16 745/week @ 2021-01-23 881/week @ 2021-01-30 898/week @ 2021-02-06 699/week @ 2021-02-13 770/week @ 2021-02-20

2,351 downloads per month
Used in 10 crates (8 directly)

BSL-1.0 license

61KB
1.5K SLoC

async-timer

Rust Crates.io Documentation dependency status

Timer facilities for Rust's async story

Minimal Rust version: 1.36

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)
  • stream - Enables Stream implementation for Interval

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–1MB
~19K SLoC