Struct EventQueue

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

A bounded queue with explicit overflow and reusable backing storage.

Implementations§

§

impl<T> EventQueue<T>

pub fn new(capacity: usize, overflow: Overflow) -> EventQueue<T>

Creates an empty queue. Storage grows lazily as events are accepted.

pub fn send(&mut self, value: T) -> Result<Option<T>, T>

Returns the evicted value, or the rejected input as Err. Zero capacity always rejects.

pub fn try_send_batch( &mut self, batch: &mut Vec<T>, ) -> Result<usize, EventBatchError>

Moves every event in batch to the queue’s tail in vector order, or none.

Returns the accepted event count and empties batch on success, retaining its allocation for reuse. An empty batch succeeds with Ok(0), including on a full or zero-capacity queue, and does not change metrics.

If batch.len() exceeds Self::remaining_capacity, returns the requested and available counts in EventBatchError. The queue and batch retain all their events in their original order, without allocating or dropping events. Rejection increases metrics().rejected by the entire batch size; success increases metrics().accepted by that size and updates the high water mark. Counters saturate. Batches never evict, even under Overflow::EvictOldest; Self::send keeps its configured policy.

No payload is cloned and no temporary event storage is allocated. Queue storage may grow on success, with space reserved before any events move. As with Self::send, allocation failure is not a capacity-error result.

use ferravex_engine::events::{EventBatchError, EventQueue, Overflow};

let mut events = EventQueue::new(2, Overflow::EvictOldest);
events.send(String::from("previous")).unwrap();
let mut batch = vec![String::from("hit"), String::from("sound")];
assert_eq!(events.try_send_batch(&mut batch), Err(EventBatchError {
    requested: 2,
    remaining_capacity: 1,
}));
assert_eq!(batch.len(), 2); // The caller still owns both events.
assert_eq!(events.pop().as_deref(), Some("previous"));
assert_eq!(events.try_send_batch(&mut batch), Ok(2));
assert!(batch.is_empty());
assert!(events.iter().map(String::as_str).eq(["hit", "sound"]));
assert_eq!(events.metrics().accepted, 3);
assert_eq!(events.metrics().rejected, 2);
assert_eq!(events.metrics().evicted, 0);

pub fn pop(&mut self) -> Option<T>

Removes the oldest queued event.

pub fn iter(&self) -> Iter<'_, T>

Borrows events in send order without allocating or consuming them.

pub fn drain(&mut self) -> Drain<'_, T>

Removes all events in send order without allocating or cloning.

Dropping the iterator discards any unyielded events. The queue keeps its backing storage for future sends. Use Self::drain_up_to to leave a tail queued for later processing.

pub fn drain_up_to(&mut self, limit: usize) -> Drain<'_, T>

Removes up to limit oldest events without allocating or cloning.

Dropping the iterator discards unyielded events within the selected prefix; events beyond that prefix remain queued in send order. A zero limit leaves all events queued.

pub fn len(&self) -> usize

Number of queued events.

pub fn is_empty(&self) -> bool

Whether no events are queued.

pub fn capacity(&self) -> usize

Maximum retained event count, independent of backing storage capacity.

pub fn remaining_capacity(&self) -> usize

Number of sends that fit before the overflow policy takes effect.

pub fn is_full(&self) -> bool

Whether a send will invoke the overflow policy. True at zero capacity.

pub fn dropped(&self) -> u64

Total rejected and evicted events since the last metrics reset, saturating at u64::MAX. Explicit pops, drains and clears are excluded.

pub fn metrics(&self) -> EventQueueMetrics

Returns a copy of the counters without changing queued events.

pub fn reset_metrics(&mut self) -> EventQueueMetrics

Returns the previous metrics and starts a new observation interval.

Queued events remain intact. Counters become zero; the new high-water mark starts at the current queue length.

pub fn clear(&mut self)

Discards queued events while retaining backing storage and metrics.

Auto Trait Implementations§

§

impl<T> Freeze for EventQueue<T>

§

impl<T> RefUnwindSafe for EventQueue<T>
where T: RefUnwindSafe,

§

impl<T> Send for EventQueue<T>
where T: Send,

§

impl<T> Sync for EventQueue<T>
where T: Sync,

§

impl<T> Unpin for EventQueue<T>
where T: Unpin,

§

impl<T> UnwindSafe for EventQueue<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.