Skip to main content

TtlQueue

Struct TtlQueue 

Source
pub struct TtlQueue<T> { /* private fields */ }
Expand description

A bounded FIFO queue where each item carries an expiry timestamp.

Expired items are evicted lazily on push, pop, and drain. When the queue is at capacity, the oldest item is dropped on push.

§Examples

use ma_core::TtlQueue;

let mut q = TtlQueue::new(8);
let now = 100;
q.push(now, now + 60, "expires in 60s");
q.push(now, 0, "never expires");

// Both present at t=100
assert_eq!(q.drain(now).len(), 2);

Implementations§

Source§

impl<T> TtlQueue<T>

Source

pub fn new(capacity: usize) -> Self

Create a new queue with the given maximum capacity.

§Panics

Panics if capacity is 0.

Source

pub fn push(&mut self, now: u64, expires_at: u64, item: T)

Push an item with an absolute expiry timestamp (Unix epoch seconds).

Pass expires_at = 0 for items that never expire. If the queue is full after eviction, the oldest item is silently dropped.

Source

pub fn pop(&mut self, now: u64) -> Option<T>

Pop the oldest non-expired item, evicting any expired head entries first.

Source

pub fn peek(&mut self, now: u64) -> Option<&T>

Peek at the oldest non-expired item without removing it.

Source

pub fn drain(&mut self, now: u64) -> Vec<T>

Drain all non-expired items, returning them in FIFO order.

Source

pub fn len(&self) -> usize

Number of items currently in the queue (including not-yet-evicted expired items).

Source

pub fn is_empty(&self) -> bool

Whether the queue is empty.

Trait Implementations§

Source§

impl<T: Clone> Clone for TtlQueue<T>

Source§

fn clone(&self) -> TtlQueue<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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> 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> UnsafeUnpin for TtlQueue<T>

§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more