Skip to main content

Events

Struct Events 

Source
pub struct Events<T: 'static> { /* private fields */ }
Expand description

Double-buffered typed event queue.

Events are written to current during the tick they are sent. At the start of the next Schedule::run(), World::update_events() swaps the buffers: current becomes previous and the old previous is cleared. Systems that use EventReader<T> iterate over previous, so they always read events from the previous tick.

Tick N:   EventWriter sends → current
tick N+1: update_events() → current becomes previous, cleared current
          EventReader reads previous (events from tick N)
Tick N+2: update_events() → previous is cleared

Register an event type with [World::add_event::<T>()] before using EventWriter<T> or EventReader<T> in systems.

Implementations§

Source§

impl<T: 'static> Events<T>

Source

pub fn new() -> Self

Create an empty double buffer.

Source

pub fn send(&mut self, event: T)

Send an event. It will be readable by EventReader on the next tick.

Source

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

Iterate over events sent during the previous tick.

Source

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

Iterate over events in the current (not yet swapped) buffer.

For render extraction that runs after a tick completes: events sent during tick N live in current until the next update().

Source

pub fn current_len(&self) -> usize

Number of events in the current (not yet swapped) buffer.

Used by render event extractors with offset tracking to avoid re-reading events that were already captured in a prior flush.

Source

pub fn update(&mut self)

Advance the double buffer.

The previous buffer is cleared. The current buffer becomes the new previous buffer. Called automatically by World::update_events() at the start of each Schedule::run().

Source

pub fn len(&self) -> usize

Number of readable events (in the previous buffer).

Source

pub fn is_empty(&self) -> bool

Returns true if there are no readable events.

Trait Implementations§

Source§

impl<T: 'static> Default for Events<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for Events<T>
where Vec<T>: Freeze,

§

impl<T> RefUnwindSafe for Events<T>
where Vec<T>: RefUnwindSafe,

§

impl<T> Send for Events<T>
where Vec<T>: Send,

§

impl<T> Sync for Events<T>
where Vec<T>: Sync,

§

impl<T> Unpin for Events<T>
where Vec<T>: Unpin,

§

impl<T> UnsafeUnpin for Events<T>
where Vec<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for Events<T>
where Vec<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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.