Struct ttl_queue::TtlQueue

source ·
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>

source

pub fn new(ttl: Duration) -> Self

Creates an empty TtlQueue with default capacity.

source

pub fn with_capacity(ttl: Duration, capacity: usize) -> Self

Creates an empty TtlQueue for at least capacity elements.

source

pub fn push_back(&mut self, element: T)

Pushes an element to the end of the queue.

source

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.

source

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.

source

pub fn peek_front(&mut self) -> Option<&(Instant, T)>

Similar to pop_front but without removing the element.

source

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.

source

pub fn is_empty(&self) -> bool

Returns true if the queue is definitely empty or false if the queue is possibly empty.

This operation is O(1). In order to obtain an accurate count in O(N) (worst-case), use refresh instead.

source

pub fn refresh(&mut self) -> usize

Refreshes the queue and returns the number of currently contained elements.

Trait Implementations§

source§

impl<T: Debug> Debug for TtlQueue<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.