Skip to main content

EventReader

Struct EventReader 

Source
pub struct EventReader<'a, E: Event> { /* private fields */ }
Expand description

Read-only accessor for consuming events from an EventQueue.

EventReader provides a way for systems to read events without consuming them immediately. It tracks a read index to ensure events are not read multiple times within the same frame by the same reader.

§Multiple Readers

Multiple EventReader instances can exist for the same queue (shared borrow). Each reader maintains its own read position, allowing different systems to read the same events independently.

§Read Tracking

The reader tracks which events have been read via an internal index. When read() is called, it returns only unread events and advances the index. This prevents double-processing of events within a single system.

§Example

use goud_engine::core::event::{EventQueue, EventReader};

#[derive(Debug, Clone)]
struct DamageEvent { amount: u32 }

let mut queue: EventQueue<DamageEvent> = EventQueue::new();
queue.send(DamageEvent { amount: 10 });
queue.send(DamageEvent { amount: 25 });
queue.swap_buffers();

// Create a reader
let mut reader = EventReader::new(&queue);

// First read gets both events
let events: Vec<_> = reader.read().collect();
assert_eq!(events.len(), 2);

// Second read gets nothing (already read)
let events: Vec<_> = reader.read().collect();
assert!(events.is_empty());

Implementations§

Source§

impl<'a, E: Event> EventReader<'a, E>

Source

pub fn new(queue: &'a EventQueue<E>) -> Self

Creates a new EventReader for the given queue.

The reader starts at index 0, meaning it will read all available events in the read buffer on the first call to read().

§Example
use goud_engine::core::event::{EventQueue, EventReader};

struct MyEvent { data: i32 }

let queue: EventQueue<MyEvent> = EventQueue::new();
let reader = EventReader::new(&queue);
assert!(reader.is_empty());
Source

pub fn read(&mut self) -> EventReaderIter<'_, 'a, E>

Returns an iterator over unread events.

Each call to read() returns only events that haven’t been read by this reader instance yet. The read index is advanced after iteration.

§Example
use goud_engine::core::event::{EventQueue, EventReader};

#[derive(Debug)]
struct ScoreEvent { points: i32 }

let mut queue: EventQueue<ScoreEvent> = EventQueue::new();
queue.send(ScoreEvent { points: 100 });
queue.send(ScoreEvent { points: 50 });
queue.swap_buffers();

let mut reader = EventReader::new(&queue);

// Read all events
for event in reader.read() {
    println!("Score: {}", event.points);
}
Source

pub fn is_empty(&self) -> bool

Returns true if there are no unread events.

This checks if all events in the read buffer have been consumed by this reader.

§Example
use goud_engine::core::event::{EventQueue, EventReader};

struct Event;

let mut queue: EventQueue<Event> = EventQueue::new();
queue.send(Event);
queue.swap_buffers();

let mut reader = EventReader::new(&queue);
assert!(!reader.is_empty());

// Consume all events
let _ = reader.read().count();
assert!(reader.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of unread events.

§Example
use goud_engine::core::event::{EventQueue, EventReader};

struct Event;

let mut queue: EventQueue<Event> = EventQueue::new();
queue.send(Event);
queue.send(Event);
queue.send(Event);
queue.swap_buffers();

let mut reader = EventReader::new(&queue);
assert_eq!(reader.len(), 3);

// Read one event
let _ = reader.read().next();
assert_eq!(reader.len(), 2);
Source

pub fn clear(&mut self)

Clears the reader’s position, allowing events to be re-read.

This resets the read index to 0. The next call to read() will return all events in the read buffer again.

§Example
use goud_engine::core::event::{EventQueue, EventReader};

#[derive(Debug, Clone)]
struct Event { id: u32 }

let mut queue: EventQueue<Event> = EventQueue::new();
queue.send(Event { id: 1 });
queue.swap_buffers();

let mut reader = EventReader::new(&queue);

// Read all events
let count1 = reader.read().count();
assert_eq!(count1, 1);

// Nothing left to read
let count2 = reader.read().count();
assert_eq!(count2, 0);

// Reset and read again
reader.clear();
let count3 = reader.read().count();
assert_eq!(count3, 1);

Auto Trait Implementations§

§

impl<'a, E> Freeze for EventReader<'a, E>

§

impl<'a, E> RefUnwindSafe for EventReader<'a, E>
where E: RefUnwindSafe,

§

impl<'a, E> Send for EventReader<'a, E>

§

impl<'a, E> Sync for EventReader<'a, E>

§

impl<'a, E> Unpin for EventReader<'a, E>

§

impl<'a, E> UnsafeUnpin for EventReader<'a, E>

§

impl<'a, E> UnwindSafe for EventReader<'a, E>
where E: RefUnwindSafe,

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<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

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, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> Resource for T
where T: Send + Sync + 'static,