Crate ttl_queue

Source
Expand description

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

Structs§

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