#queue #ttl #fps-counter

ttl-queue

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

2 unstable releases

0.2.0 Aug 2, 2023
0.1.0 Aug 2, 2023

#2253 in Data structures

Download history 1132/week @ 2024-12-08 696/week @ 2024-12-15 266/week @ 2024-12-22 560/week @ 2024-12-29 637/week @ 2025-01-05 433/week @ 2025-01-12 582/week @ 2025-01-19 535/week @ 2025-01-26 579/week @ 2025-02-02 527/week @ 2025-02-09 477/week @ 2025-02-16 454/week @ 2025-02-23 573/week @ 2025-03-02 530/week @ 2025-03-09 486/week @ 2025-03-16 620/week @ 2025-03-23

2,223 downloads per month

EUPL-1.2

17KB
334 lines

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);
}

Dependencies

~0–5.5MB
~19K SLoC