throttle-timer 1.0.0

Throttle events and record event stats with a simple library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::time::Duration;
use throttle_timer::ThrottleTimer;

fn main() {
    let mut break_timer = ThrottleTimer::new(Duration::from_secs(10_u64), &"Break");
    let mut val = 0_u8;
    // timers always run when no previous runs
    assert!(break_timer.run(&mut || val += 1));

    for _ in 0..100 {
        // timer will not run as 10 secs has not passed
        // do run will return false
        assert!(!break_timer.run(&mut || val += 1));
    }
    break_timer.print_stats();
    assert_eq!(break_timer.total_calls(), &1);
    assert_eq!(val, 1_u8);
}