#fps-counter #queue #ttl #stack #content #drop #vec-deque #amount-of-time #default

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

#2414 in Data structures

Download history 530/week @ 2025-03-09 486/week @ 2025-03-16 630/week @ 2025-03-23 364/week @ 2025-03-30 556/week @ 2025-04-06 469/week @ 2025-04-13 514/week @ 2025-04-20 485/week @ 2025-04-27 741/week @ 2025-05-04 593/week @ 2025-05-11 416/week @ 2025-05-18 554/week @ 2025-05-25 462/week @ 2025-06-01 369/week @ 2025-06-08 394/week @ 2025-06-15 429/week @ 2025-06-22

1,664 downloads per month

EUPL-1.2

17KB
334 lines

Timed Queue

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

Crate Features

  • vecdeque - Uses a VecDeque as the underlying data structure. Enabled by default.
  • doublestack - Uses two stacks (Vec) as the underlying data structure. Mutually exclusive with vecdeque.
  • tokio - Uses tokio::time::Instant instead of std::time::Instant.

Example

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

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

for i in 0..=50 {
    // 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 ~20 ms to achieve a ~50 Hz frequency.
    thread::sleep(Duration::from_millis(19));
}

let fps = fps_counter.refresh();
debug_assert!(fps >= 45 && fps <= 55);

let delta = fps_counter.avg_delta();
debug_assert!(delta >= Duration::from_millis(19) && delta <= Duration::from_millis(21));

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