Path: blob/main/crates/bevy_ecs/src/event/event_cursor.rs
6600 views
use bevy_ecs::event::{1BufferedEvent, EventIterator, EventIteratorWithId, EventMutIterator, EventMutIteratorWithId,2Events,3};4#[cfg(feature = "multi_threaded")]5use bevy_ecs::event::{EventMutParIter, EventParIter};6use core::marker::PhantomData;78/// Stores the state for an [`EventReader`] or [`EventMutator`].9///10/// Access to the [`Events<E>`] resource is required to read any incoming events.11///12/// In almost all cases, you should just use an [`EventReader`] or [`EventMutator`],13/// which will automatically manage the state for you.14///15/// However, this type can be useful if you need to manually track events,16/// such as when you're attempting to send and receive events of the same type in the same system.17///18/// # Example19///20/// ```21/// use bevy_ecs::prelude::*;22/// use bevy_ecs::event::{BufferedEvent, Events, EventCursor};23///24/// #[derive(BufferedEvent, Clone, Debug)]25/// struct MyEvent;26///27/// /// A system that both sends and receives events using a [`Local`] [`EventCursor`].28/// fn send_and_receive_events(29/// // The `Local` `SystemParam` stores state inside the system itself, rather than in the world.30/// // `EventCursor<T>` is the internal state of `EventMutator<T>`, which tracks which events have been seen.31/// mut local_event_reader: Local<EventCursor<MyEvent>>,32/// // We can access the `Events` resource mutably, allowing us to both read and write its contents.33/// mut events: ResMut<Events<MyEvent>>,34/// ) {35/// // We must collect the events to resend, because we can't mutate events while we're iterating over the events.36/// let mut events_to_resend = Vec::new();37///38/// for event in local_event_reader.read(&mut events) {39/// events_to_resend.push(event.clone());40/// }41///42/// for event in events_to_resend {43/// events.write(MyEvent);44/// }45/// }46///47/// # bevy_ecs::system::assert_is_system(send_and_receive_events);48/// ```49///50/// [`EventReader`]: super::EventReader51/// [`EventMutator`]: super::EventMutator52#[derive(Debug)]53pub struct EventCursor<E: BufferedEvent> {54pub(super) last_event_count: usize,55pub(super) _marker: PhantomData<E>,56}5758impl<E: BufferedEvent> Default for EventCursor<E> {59fn default() -> Self {60EventCursor {61last_event_count: 0,62_marker: Default::default(),63}64}65}6667impl<E: BufferedEvent> Clone for EventCursor<E> {68fn clone(&self) -> Self {69EventCursor {70last_event_count: self.last_event_count,71_marker: PhantomData,72}73}74}7576impl<E: BufferedEvent> EventCursor<E> {77/// See [`EventReader::read`](super::EventReader::read)78pub fn read<'a>(&'a mut self, events: &'a Events<E>) -> EventIterator<'a, E> {79self.read_with_id(events).without_id()80}8182/// See [`EventMutator::read`](super::EventMutator::read)83pub fn read_mut<'a>(&'a mut self, events: &'a mut Events<E>) -> EventMutIterator<'a, E> {84self.read_mut_with_id(events).without_id()85}8687/// See [`EventReader::read_with_id`](super::EventReader::read_with_id)88pub fn read_with_id<'a>(&'a mut self, events: &'a Events<E>) -> EventIteratorWithId<'a, E> {89EventIteratorWithId::new(self, events)90}9192/// See [`EventMutator::read_with_id`](super::EventMutator::read_with_id)93pub fn read_mut_with_id<'a>(94&'a mut self,95events: &'a mut Events<E>,96) -> EventMutIteratorWithId<'a, E> {97EventMutIteratorWithId::new(self, events)98}99100/// See [`EventReader::par_read`](super::EventReader::par_read)101#[cfg(feature = "multi_threaded")]102pub fn par_read<'a>(&'a mut self, events: &'a Events<E>) -> EventParIter<'a, E> {103EventParIter::new(self, events)104}105106/// See [`EventMutator::par_read`](super::EventMutator::par_read)107#[cfg(feature = "multi_threaded")]108pub fn par_read_mut<'a>(&'a mut self, events: &'a mut Events<E>) -> EventMutParIter<'a, E> {109EventMutParIter::new(self, events)110}111112/// See [`EventReader::len`](super::EventReader::len)113pub fn len(&self, events: &Events<E>) -> usize {114// The number of events in this reader is the difference between the most recent event115// and the last event seen by it. This will be at most the number of events contained116// with the events (any others have already been dropped)117// TODO: Warn when there are dropped events, or return e.g. a `Result<usize, (usize, usize)>`118events119.event_count120.saturating_sub(self.last_event_count)121.min(events.len())122}123124/// Amount of events we missed.125pub fn missed_events(&self, events: &Events<E>) -> usize {126events127.oldest_event_count()128.saturating_sub(self.last_event_count)129}130131/// See [`EventReader::is_empty()`](super::EventReader::is_empty)132pub fn is_empty(&self, events: &Events<E>) -> bool {133self.len(events) == 0134}135136/// See [`EventReader::clear()`](super::EventReader::clear)137pub fn clear(&mut self, events: &Events<E>) {138self.last_event_count = events.event_count;139}140}141142143