#timer #events #arceos

no-std timer_list

A list of timed events that will be triggered sequentially when the timer expires

1 unstable release

0.1.0 Jul 17, 2024

#923 in Data structures

Download history 1094/week @ 2024-10-13 786/week @ 2024-10-20 349/week @ 2024-10-27 69/week @ 2024-11-03 250/week @ 2024-11-10 544/week @ 2024-11-17 320/week @ 2024-11-24 422/week @ 2024-12-01 374/week @ 2024-12-08 422/week @ 2024-12-15 615/week @ 2024-12-22 634/week @ 2024-12-29 386/week @ 2025-01-05 199/week @ 2025-01-12 144/week @ 2025-01-19 30/week @ 2025-01-26

765 downloads per month

GPL-3.0-or-later OR Apache-2…

9KB
152 lines

timer_list

Crates.io Docs.rs CI

A list of timed events that will be triggered sequentially when the timer expires.

Examples

use timer_list::{TimerEvent, TimerEventFn, TimerList};
use std::time::{Duration, Instant};

let mut timer_list = TimerList::new();

// set a timer that will be triggered after 1 second
let start_time = Instant::now();
timer_list.set(Duration::from_secs(1), TimerEventFn::new(|now| {
    println!("timer event after {:?}", now);
}));

while !timer_list.is_empty() {
    // check if there is any event that is expired
    let now = Instant::now().duration_since(start_time);
    if let Some((deadline, event)) = timer_list.expire_one(now) {
        // trigger the event, will print "timer event after 1.00s"
        event.callback(now);
        break;
    }
    std::thread::sleep(Duration::from_millis(10)); // relax the CPU
}

No runtime deps