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>

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>

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]

Borrows published events in send order; repeated reads see the same events.

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

Borrows published events in send order without allocating or consuming them.

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>

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>

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>

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>

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>

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>

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 len(&self) -> usize

Number of visible events; pending events are excluded.

pub fn is_empty(&self) -> bool

Whether the visible buffer is empty, regardless of pending events.

pub fn pending_len(&self) -> usize

Number of events waiting for the next advance.

pub fn capacity(&self) -> usize

Maximum event count in each of the visible and pending buffers.

pub fn remaining_capacity(&self) -> usize

Number of additional sends that fit before the next advance.

pub fn is_full(&self) -> bool

Whether pending is full and sends will be rejected. True at zero capacity.

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

Returns a copy of the metrics without changing either buffer.

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)

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.

Auto Trait Implementations§

§

impl<T> !Freeze for FrameEvents<T>

§

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

§

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

§

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

§

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

§

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