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>
impl<'a, E: Event> EventReader<'a, E>
Sourcepub fn new(queue: &'a EventQueue<E>) -> Self
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());Sourcepub fn read(&mut self) -> EventReaderIter<'_, 'a, E> ⓘ
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);
}Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn len(&self) -> usize
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);Sourcepub fn clear(&mut self)
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> 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<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
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