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>
impl<T> EventQueue<T>
pub fn new(capacity: usize, overflow: Overflow) -> 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>
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>
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 drain(&mut self) -> Drain<'_, T>
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>
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 capacity(&self) -> usize
pub fn capacity(&self) -> usize
Maximum retained event count, independent of backing storage capacity.
pub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Number of sends that fit before the overflow policy takes effect.
pub fn dropped(&self) -> u64
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
pub fn metrics(&self) -> EventQueueMetrics
Returns a copy of the counters without changing queued events.
pub fn reset_metrics(&mut self) -> EventQueueMetrics
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)
pub fn clear(&mut self)
Discards queued events while retaining backing storage and metrics.