Struct FrameEvents
pub struct FrameEvents<T> { /* private fields */ }Expand description
Double-buffered events with explicit publication and pending-buffer backpressure.
Implementations§
§impl<T> FrameEvents<T>
impl<T> FrameEvents<T>
pub fn new(capacity: usize) -> FrameEvents<T>
pub fn new(capacity: usize) -> FrameEvents<T>
Creates empty buffers, each bounded by capacity. Storage grows lazily.
pub fn send(&mut self, event: T) -> Result<(), T>
pub fn send(&mut self, event: T) -> Result<(), T>
Queues an event for the next advance, returning it unchanged if pending is full. Zero capacity always rejects. Visible events do not consume pending capacity.
pub fn read(&self) -> &[T]
pub fn read(&self) -> &[T]
Borrows published events in send order; repeated reads see the same events.
pub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Borrows published events in send order without allocating or consuming them.
pub fn reader(&self) -> FrameEventReader
pub fn reader(&self) -> FrameEventReader
Creates a cursor starting at the oldest currently visible event.
Earlier history is excluded from its diagnostics. The first reader for a buffer allocates one shared identity token; later readers and cursor operations allocate no storage. The buffer keeps no list of readers.
use ferravex_engine::frame_events::FrameEvents;
let mut events = FrameEvents::new(2);
let mut reader = events.reader();
events.send("hit").unwrap();
assert!(events.read_with(&mut reader).unwrap().events.is_empty());
events.advance();
assert_eq!(events.read_with(&mut reader).unwrap().events, ["hit"]);
assert!(events.read_with(&mut reader).unwrap().events.is_empty());
assert_eq!(events.read(), ["hit"]); // Other listeners still see it.pub fn reader_status(
&self,
reader: &FrameEventReader,
) -> Result<FrameReaderStatus, FrameReaderError>
pub fn reader_status( &self, reader: &FrameEventReader, ) -> Result<FrameReaderStatus, FrameReaderError>
Inspects a cursor without changing its position or acknowledging loss.
An empty buffer can still report missed events. Repeated inspections preserve the report until a cursor read or reset acknowledges it.
use ferravex_engine::frame_events::FrameEvents;
let mut events = FrameEvents::new(1);
let mut reader = events.reader();
events.send(7).unwrap();
events.advance();
events.advance(); // Expire 7 and publish an empty frame.
let status = events.reader_status(&reader).unwrap();
assert_eq!((status.frames_behind, status.missed_frames), (2, 1));
assert_eq!((status.unread_events, status.missed_events), (0, 1));
assert_eq!(events.read_with(&mut reader).unwrap().status, status);
assert_eq!(events.reader_status(&reader).unwrap().missed_events, 0);pub fn read_with<'a>(
&'a self,
reader: &mut FrameEventReader,
) -> Result<FrameEventRead<'a, T>, FrameReaderError>
pub fn read_with<'a>( &'a self, reader: &mut FrameEventReader, ) -> Result<FrameEventRead<'a, T>, FrameReaderError>
Borrows and acknowledges all currently available unread events for one cursor.
Loss is returned alongside the events, then acknowledged, including for
empty reads. Pending events and other readers are unaffected. See
Self::reader for an example and Self::read_up_to for a read budget.
pub fn read_up_to<'a>(
&'a self,
reader: &mut FrameEventReader,
limit: usize,
) -> Result<FrameEventRead<'a, T>, FrameReaderError>
pub fn read_up_to<'a>( &'a self, reader: &mut FrameEventReader, limit: usize, ) -> Result<FrameEventRead<'a, T>, FrameReaderError>
Borrows and acknowledges up to limit unread events in send order.
The returned status precedes the read. Even a zero limit acknowledges
reported loss, but leaves available events unread. Use Self::reader_status
for an inspection that never changes the cursor. The returned slice is
acknowledged immediately; processing only part of it does not rewind.
use ferravex_engine::frame_events::FrameEvents;
let mut events = FrameEvents::new(2);
events.send(1).unwrap();
events.send(2).unwrap();
events.advance();
let mut reader = events.reader();
let batch = events.read_up_to(&mut reader, 1).unwrap();
assert_eq!(batch.events, [1]);
assert_eq!(batch.status.unread_events, 2);
// Destructive drains remove events for every reader. Already-read
// events do not count as missed for this cursor.
drop(events.drain_up_to(1));
assert_eq!(events.read_with(&mut reader).unwrap().events, [2]);pub fn reset_reader(
&self,
reader: &mut FrameEventReader,
position: FrameReaderPosition,
) -> Result<FrameReaderStatus, FrameReaderError>
pub fn reset_reader( &self, reader: &mut FrameEventReader, position: FrameReaderPosition, ) -> Result<FrameReaderStatus, FrameReaderError>
Returns the previous status and resets a cursor to a retained-data boundary.
Both destinations acknowledge prior loss without changing events, pending data, other cursors or metrics. Expired/drained/cleared events cannot be recovered. A cursor from another buffer is rejected without mutation.
use ferravex_engine::frame_events::{FrameEvents, FrameReaderPosition};
let mut events = FrameEvents::new(1);
events.send(1).unwrap();
events.advance();
let mut reader = events.reader();
let skipped = events.reset_reader(&mut reader, FrameReaderPosition::Latest).unwrap();
assert_eq!(skipped.unread_events, 1);
assert!(events.read_with(&mut reader).unwrap().events.is_empty());
events.reset_reader(&mut reader, FrameReaderPosition::Oldest).unwrap();
assert_eq!(events.read_with(&mut reader).unwrap().events, [1]);
events.send(2).unwrap();
events.advance();
assert_eq!(events.read_with(&mut reader).unwrap().events, [2]);pub fn drain(&mut self) -> Drain<'_, T>
pub fn drain(&mut self) -> Drain<'_, T>
Removes published events in send order without allocating or cloning.
Dropping the iterator discards unyielded visible events. Pending events remain queued for the next advance, and both buffers retain their storage. Cursors are not moved: any removed events they have not read are reported as missed by their next status/read. Already-read events are not missed.
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 visible events without allocating or cloning.
Dropping the iterator discards the unyielded selected prefix. Unselected visible events remain available until drained, cleared or expired by the next advance. Pending events are unaffected; a zero limit removes nothing. Cursor positions remain valid after prefix removal; unread removed events are reported as missed, including selected events discarded on iterator drop.
pub fn pending_len(&self) -> usize
pub fn pending_len(&self) -> usize
Number of events waiting for the next advance.
pub fn remaining_capacity(&self) -> usize
pub fn remaining_capacity(&self) -> usize
Number of additional sends that fit before the next advance.
pub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Whether pending is full and sends will be rejected. True at zero capacity.
pub fn advance(&mut self)
pub fn advance(&mut self)
Expires the current visible events and publishes pending events in send order. The buffers are swapped and reused without allocating.
§Panics
Panics before changing either buffer if the lifetime advance count or
published event count would exceed u64::MAX. Cursor sequences never
wrap or saturate into a misleading empty read; metrics still saturate.
pub fn metrics(&self) -> FrameEventsMetrics
pub fn metrics(&self) -> FrameEventsMetrics
Returns a copy of the metrics without changing either buffer.
pub fn reset_metrics(&mut self) -> FrameEventsMetrics
pub fn reset_metrics(&mut self) -> FrameEventsMetrics
Returns the previous metrics and starts a new observation interval.
Neither buffer changes. Counters become zero; the pending high-water mark starts at the current pending length. Subsequent advances still count any visible events they expire, even if those events predate the reset.
pub fn clear(&mut self)
pub fn clear(&mut self)
Discards both buffers while retaining their backing storage and metrics. Readers report unread visible events as missed; unpublished pending events do not count. Frame/event sequences and reader identities are preserved.