ttl-queue

Crates.iottl-queue
lib.rsttl-queue
version0.2.0
sourcesrc
created_at2023-08-02 19:14:49.726148
updated_at2023-08-02 20:22:18.909119
descriptionA queue that drops its content after a given amount of time.
homepagehttps://github.com/sunsided/ttl-queue
repositoryhttps://github.com/sunsided/ttl-queue.git
max_upload_size
id933004
size33,021
Markus Mayer (sunsided)

documentation

README

Timed Queue

A queue that drops its content after a given amount of time.

Example

To implement an FPS counter, you could use the following technique:

use std::thread;
use std::time::Duration;
use ttl_queue::TtlQueue;

fn main() {
    let mut fps_counter = TtlQueue::new(Duration::from_secs_f64(1.0));

    for i in 0..100 {
        // Register a new frame and return the number of frames observed
        // within the last second.
        let fps = fps_counter.refresh_and_push_back(());
        debug_assert!(fps >= 1);

        // Sleep 10 ms to achieve a ~100 Hz frequency.
        thread::sleep(Duration::from_millis(10));
    }

    let fps = fps_counter.refresh();
    debug_assert!(fps >= 95 && fps <= 105);
}
Commit count: 13

cargo fmt