#timer #simpler #duration #limited #straight-forward #context #callback

simpler_timer

A very simple timer library with limited, but straight-forward functionality

3 unstable releases

0.2.0 Jul 14, 2020
0.1.1 Jul 14, 2020
0.1.0 Jul 13, 2020

#18 in #simpler

Download history 300/week @ 2023-12-05 297/week @ 2023-12-12 156/week @ 2023-12-19 143/week @ 2023-12-26 140/week @ 2024-01-02 181/week @ 2024-01-09 291/week @ 2024-01-16 225/week @ 2024-01-23 186/week @ 2024-01-30 222/week @ 2024-02-06 221/week @ 2024-02-13 336/week @ 2024-02-20 276/week @ 2024-02-27 301/week @ 2024-03-05 392/week @ 2024-03-12 238/week @ 2024-03-19

1,254 downloads per month
Used in oryx

MIT/Apache

6KB
60 lines

Simpler Timer

Crates.io Docs.rs

This library provides a very simple, poll based timer.

To use, include the following in Cargo.toml

[dependencies]
simpler_timer = "0.2.0"
use simpler_timer::Timer;
use std::time::Duration;

fn main() {
    let periodic = Timer::with_duration(Duration::from_millis(100));
    let timeout = Timer::with_duration(Duration::from_secs(2));
    

    loop {
        if periodic.expired() {
            println!("tick");
            periodic.reset();
        }

        if timeout.expired() {
            break;
        }
    }

    println!("total elapsed time: {}ms", timeout.elapsed().as_millis());
}

lib.rs:

simpler_timer

A simple timer mechanism to track arbitrary timeouts. It doesn't do anything fancy, e.g. no callbacks upon expiry, just give it a Duration and poll if the timer is expired. Timers can be reset and reused for periodic contexts, such as a simple time based control loop.

Example

use std::time::Duration;

// 100ms timer
let tick = Timer::with_duration(Duration::from_millis(100));
// 1 sec timer
let end = Timer::with_duration(Duration::from_secs(1));

loop {
    if tick.expired() {
        // do something interesting
        println!("tick");
        tick.reset();
    }

    if end.expired() { 
        // don't reset, let's get out of here
        break; 
    }
}

println!("total time: {}ms", end.elapsed().as_millis());

No runtime deps