pub struct EventChannel<T> {
pub events: Vec<T>,
pub base_sequence: u64,
pub previous_update_sequence: u64,
}Expand description
A sequence-numbered event channel for inter-system communication.
Events live in one flat Vec and every event gets a monotonically
increasing sequence number, the same cursor scheme the structural log
uses. Two consumption styles are supported:
- Frame-scoped:
read()sees every buffered event, andupdate()once per frame expires events after they have been visible for two frames, matching the old double-buffer lifetime. - Cursor-based, exactly-once: each consumer records
sequence()after readingevents_since(cursor). Multiple consumers each see every event exactly once, and a consumer that skips a frame catches up instead of double-processing or missing events.
§Examples
use freecs::EventChannel;
#[derive(Debug, Clone)]
struct DamageEvent { amount: i32 }
let mut channel = EventChannel::new();
channel.send(DamageEvent { amount: 10 });
assert_eq!(channel.len(), 1);
let mut cursor = 0;
let seen = channel.events_since(cursor);
assert_eq!(seen.len(), 1);
cursor = channel.sequence();
assert!(channel.events_since(cursor).is_empty(), "cursor consumers see each event once");
channel.update();
assert_eq!(channel.len(), 1, "event persists after first update");
channel.update();
assert_eq!(channel.len(), 0, "event expired after second update");Fields§
§events: Vec<T>§base_sequence: u64§previous_update_sequence: u64Implementations§
Source§impl<T> EventChannel<T>
impl<T> EventChannel<T>
pub fn new() -> Self
Sourcepub fn send(&mut self, event: T)
pub fn send(&mut self, event: T)
Sends an event. It stays readable until it expires two update()
calls later or a cursor consumer trims past it.
Sourcepub fn sequence(&self) -> u64
pub fn sequence(&self) -> u64
The sequence number of the most recently sent event. Record this as
your cursor after consuming events_since.
Sourcepub fn events_since(&self, cursor: u64) -> &[T]
pub fn events_since(&self, cursor: u64) -> &[T]
All buffered events sent after cursor, oldest first. A cursor older
than the buffer yields everything still buffered.
Sourcepub fn consume(&self, cursor: &mut u64) -> &[T]
pub fn consume(&self, cursor: &mut u64) -> &[T]
The exactly-once read: yields every event sent after cursor and
advances the cursor past them, so calling this every frame delivers
each event to this consumer exactly once. Each consumer owns one
u64 cursor; the buffer itself is untouched, so other consumers and
the two-frame expiry are unaffected. This is the spelling to reach
for by default; read() re-reads the whole buffer every call.
Sourcepub fn read(&self) -> impl Iterator<Item = &T>
pub fn read(&self) -> impl Iterator<Item = &T>
Returns an iterator over every buffered event, oldest first.
Sourcepub fn read_frame(&self) -> &[T]
pub fn read_frame(&self) -> &[T]
The events settled at the last update: the previous
frame’s complete set, frozen and broadcast to every reader without a
cursor. Events sent since that update (this frame’s sends) are
excluded, so the slice does not change as more events arrive during the
frame. A reader that calls this once per frame therefore sees each
frame’s events exactly once, one frame after they were sent, regardless
of where in the frame it reads and of interleaving with senders. This is
the broadcast, single-frame counterpart to the cursor-based
consume: use it when every reader must observe the
same frozen set for a frame, as a deferred event pipeline does.
Sourcepub fn trim(&mut self, up_to_sequence: u64)
pub fn trim(&mut self, up_to_sequence: u64)
Drops all events up to and including up_to_sequence. Call with the
minimum cursor across consumers to reclaim memory early.
Trait Implementations§
Source§impl<T: Clone> Clone for EventChannel<T>
impl<T: Clone> Clone for EventChannel<T>
Source§fn clone(&self) -> EventChannel<T>
fn clone(&self) -> EventChannel<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T> Freeze for EventChannel<T>
impl<T> RefUnwindSafe for EventChannel<T>where
T: RefUnwindSafe,
impl<T> Send for EventChannel<T>where
T: Send,
impl<T> Sync for EventChannel<T>where
T: Sync,
impl<T> Unpin for EventChannel<T>where
T: Unpin,
impl<T> UnsafeUnpin for EventChannel<T>
impl<T> UnwindSafe for EventChannel<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more