Skip to main content

EventChannel

Struct EventChannel 

Source
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, and update() 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 reading events_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: u64

Implementations§

Source§

impl<T> EventChannel<T>

Source

pub fn new() -> Self

Source

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.

Source

pub fn sequence(&self) -> u64

The sequence number of the most recently sent event. Record this as your cursor after consuming events_since.

Source

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.

Source

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.

Source

pub fn read(&self) -> impl Iterator<Item = &T>

Returns an iterator over every buffered event, oldest first.

Source

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.

Source

pub fn peek(&self) -> Option<&T>

Returns a reference to the oldest buffered event, if any.

Source

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.

Source

pub fn update(&mut self)

Expires events that have now been visible for two frames. Call once per frame; step() on a generated world does this for every channel.

Source

pub fn clear(&mut self)

Immediately drops every buffered event, advancing past them.

Source

pub fn len(&self) -> usize

Returns the number of buffered events.

Source

pub fn is_empty(&self) -> bool

Returns true if no events are buffered.

Trait Implementations§

Source§

impl<T: Clone> Clone for EventChannel<T>

Source§

fn clone(&self) -> EventChannel<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Default for EventChannel<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto 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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.