Struct 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..=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));

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.

Source

pub fn iter(&self) -> impl Iterator<Item = &(Instant, T)>

Returns an iterator to the data.

Source

pub fn avg_delta(&self) -> Duration

Returns the average duration between two events.

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
Source§

impl<T> IntoIterator for TtlQueue<T>

Source§

type Item = (Instant, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<TtlQueue<T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TtlQueue<T>

§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.