Function swanling::util::ms_timer_expired[][src]

pub fn ms_timer_expired(started: Instant, elapsed: usize) -> bool
Expand description

Determine if a timer expired, with millisecond granularity.

If the timer was started more than run_time milliseconds ago return true, otherwise return false.

This function accepts started as a std::time::Instant. It expects run_time in milliseconds.

Example

use swanling::util;

let started = std::time::Instant::now();
let mut counter = 0;
loop {
    // Track how many times this loop runs.
    counter += 1;

    // Sleep for a quarter of a second.
    std::thread::sleep(std::time::Duration::from_millis(100));

    // Do stuff ...

    // Loop until the timer expires, then break.
    if util::ms_timer_expired(started, 750) {
        break
    }
}

// It took 8 loops for the timer to expire. (Total time in the loop was 800 ms).
assert_eq!(counter, 8);