might_sleep 0.2.0

limit cpu usage by estimating sleep time
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use might_sleep::prelude::*;

mod usage {
    use std::time::Duration;

    pub const LOW: Duration = Duration::from_millis(100);
    pub const NORMAL: Duration = Duration::from_millis(50);
}

fn main() {
    let mut cpu_limiter = CpuLimiter::new(usage::LOW);

    cpu_limiter.duration = usage::NORMAL;
    loop {
        println!("should be called every 50 ms");
        cpu_limiter.might_sleep();
    }
}