2 releases

0.1.1 Apr 16, 2020
0.1.0 Apr 16, 2020

#123 in #net

MIT license

140KB
2.5K SLoC

kayrx-timer


lib.rs:

Time tracking

This module provides a number of types for executing code after a set period of time.

  • Delay is a future that does no work and completes at a specific Instant in time.

  • Interval is a stream yielding a value at a fixed period. It is initialized with a Duration and repeatedly yields each time the duration elapses.

  • Timeout: Wraps a future or stream, setting an upper bound to the amount of time it is allowed to execute. If the future or stream does not complete in time, then it is canceled and an error is returned.

  • DelayQueue: A queue where items are returned once the requested delay has expired.

These types are sufficient for handling a large number of scenarios involving time.

These types must be used from within the context of the Runtime.

Examples

Wait 100ms and print "Hello World!"

use kayrx_timer::delay_for;
use std::time::Duration;
use kayrx_karx;

fn main() {
    kayrx_karx::exec(async {
        delay_for(Duration::from_millis(100)).await;
        println!("100 ms have elapsed");
    });
}

Require that an operation takes no more than 300ms. Note that this uses the timeout function on the FutureExt trait. This trait is included in the prelude.

use kayrx_timer::{timeout, Duration};

async fn long_future() {
    // do work here
}

let res = timeout(Duration::from_secs(1), long_future()).await;

if res.is_err() {
    println!("operation timed out");
}

Dependencies

~135KB