pub struct TtlQueue<T> { /* private fields */ }Expand description
A queue that drops its content after a given amount of time.
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..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);Implementations§
source§impl<T> TtlQueue<T>
impl<T> TtlQueue<T>
sourcepub fn with_capacity(ttl: Duration, capacity: usize) -> Self
pub fn with_capacity(ttl: Duration, capacity: usize) -> Self
Creates an empty TtlQueue for at least capacity elements.
sourcepub fn refresh_and_push_back(&mut self, element: T) -> usize
pub fn refresh_and_push_back(&mut self, element: T) -> usize
Pushes an element to the end of the queue and returns the number of items currently in the queue. This operation is O(N) at worst.
sourcepub fn pop_front(&mut self) -> Option<(Instant, T)>
pub fn pop_front(&mut self) -> Option<(Instant, T)>
Gets the element from the front of the queue if it exists, as well as the time instant at which it was added.
sourcepub fn peek_front(&mut self) -> Option<&(Instant, T)>
pub fn peek_front(&mut self) -> Option<&(Instant, T)>
Similar to pop_front but without removing the element.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Gets the number elements currently in the queue, including potentially expired elements.
This operation is O(1). In order to obtain an accurate count in O(N) (worst-case),
use refresh instead.
Trait Implementations§
Auto Trait Implementations§
impl<T> RefUnwindSafe for TtlQueue<T>where T: RefUnwindSafe,
impl<T> Send for TtlQueue<T>where T: Send,
impl<T> Sync for TtlQueue<T>where T: Sync,
impl<T> Unpin for TtlQueue<T>where T: Unpin,
impl<T> UnwindSafe for TtlQueue<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more