Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added peeking to EventReader #13809

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2bd8efd
Created an EventMutator for when you want to mutate an event.
BobG1983 Jun 11, 2024
7b990c2
Merge branch 'bevyengine:main' into eventmutator
BobG1983 Jun 11, 2024
7b27f90
Changed tests for par_read to match behavior for now.
BobG1983 Jun 11, 2024
6c49683
Fixed par_read for mutator
BobG1983 Jun 13, 2024
0013e86
Delete crates/bevy_ecs/.vscode directory
BobG1983 Jun 13, 2024
9454390
Merge branch 'bevyengine:main' into eventmutator
BobG1983 Jun 13, 2024
c6dcad7
Fix typo
BobG1983 Jun 13, 2024
472398d
Merge branch 'main' into eventmutator
BobG1983 Jun 17, 2024
670bfae
Updated event example
BobG1983 Jun 17, 2024
bc56cd5
Updated comments
BobG1983 Jun 17, 2024
c9a659f
Improved comment on chain
BobG1983 Jun 17, 2024
2f75c96
Fixing some typos
BobG1983 Jun 17, 2024
caf4935
Update event.rs
BobG1983 Jun 17, 2024
146eee8
Update event.rs
BobG1983 Jun 17, 2024
8793e2a
Update event.rs
BobG1983 Jun 17, 2024
e1d327e
Tidied up multi_threaded feature support
BobG1983 Jun 18, 2024
6c265fd
Fixed formatting of use statements
BobG1983 Jun 18, 2024
f927c43
Fixed cfg for ParIter
BobG1983 Jun 18, 2024
9061ce3
Apply suggestions from code review
BobG1983 Jun 21, 2024
6802742
Updating comment in example.
BobG1983 Jun 21, 2024
de0f0a8
Created EventCursor, replacing ManualEventReader, marked ManualEventR…
BobG1983 Jun 21, 2024
becd0c3
Updated engine and examples to use new EventCursor
BobG1983 Jun 21, 2024
29c4174
Fixed typo in test
BobG1983 Jun 21, 2024
3ace983
Fixed more typos in test
BobG1983 Jun 21, 2024
8a88642
Merge branch 'bevyengine:main' into eventmutator
BobG1983 Jun 24, 2024
862ac98
Merge branch 'main' into eventmutator
BobG1983 Jun 25, 2024
bd8c614
Fixing formatting and clippy breaks, remind me not to handle diffs on…
BobG1983 Jun 25, 2024
f9dbaa4
Merge branch 'main' into eventmutator
BobG1983 Jul 5, 2024
8c52d95
fixed merge conflict
BobG1983 Jul 5, 2024
f414749
Added peek iterators and tests for peek iterators to event cursor
BobG1983 Jun 24, 2024
437f7c1
Added peeking to EventMutator
BobG1983 Jun 24, 2024
bfe9dd9
removing unused into_iter
BobG1983 Jun 24, 2024
d6c4d8b
fixed clippy lint
BobG1983 Jul 5, 2024
ca182dd
fixing lint again?
BobG1983 Jul 5, 2024
3f08f39
Okay, I misunderstood the lint, fixed finally
BobG1983 Jul 5, 2024
c959a99
Merge branch 'main' into peekable_eventreader
BobG1983 Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
pub use bevy_derive::AppLabel;
use bevy_ecs::{
event::{event_update_system, ManualEventReader},
event::{event_update_system, EventCursor},
intern::Interned,
prelude::*,
schedule::{ScheduleBuildSettings, ScheduleLabel},
Expand Down Expand Up @@ -818,7 +818,7 @@ impl App {
/// This should be called after every [`update()`](App::update) otherwise you risk
/// dropping possible [`AppExit`] events.
pub fn should_exit(&self) -> Option<AppExit> {
let mut reader = ManualEventReader::default();
let mut reader = EventCursor::default();

let events = self.world().get_resource::<Events<AppExit>>()?;
let mut events = reader.read(events);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ mod tests {
use bevy_core::TaskPoolPlugin;
use bevy_ecs::prelude::*;
use bevy_ecs::{
event::ManualEventReader,
event::EventCursor,
schedule::{LogLevel, ScheduleBuildSettings},
};
use bevy_log::LogPlugin;
Expand Down Expand Up @@ -1300,7 +1300,7 @@ mod tests {
gate_opener.open(b_path);
gate_opener.open(c_path);

let mut reader = ManualEventReader::default();
let mut reader = EventCursor::default();
run_app_until(&mut app, |world| {
let events = world.resource::<Events<AssetEvent<LoadedFolder>>>();
let asset_server = world.resource::<AssetServer>();
Expand Down
36 changes: 29 additions & 7 deletions crates/bevy_ecs/src/event/collections.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate as bevy_ecs;
use bevy_ecs::{
event::{Event, EventId, EventInstance, ManualEventReader},
event::{Event, EventCursor, EventId, EventInstance},
system::Resource,
};
#[cfg(feature = "bevy_reflect")]
Expand Down Expand Up @@ -153,15 +153,37 @@ impl<E: Event> Events<E> {
self.send(Default::default())
}

/// Gets a new [`ManualEventReader`]. This will include all events already in the event buffers.
pub fn get_reader(&self) -> ManualEventReader<E> {
ManualEventReader::default()
/// Gets a new [`EventCursor`]. This will include all events already in the event buffers.
pub fn get_cursor(&self) -> EventCursor<E> {
EventCursor::default()
}

/// Gets a new [`ManualEventReader`]. This will ignore all events already in the event buffers.
/// Gets a new [`EventCursor`]. This will ignore all events already in the event buffers.
/// It will read all future events.
pub fn get_reader_current(&self) -> ManualEventReader<E> {
ManualEventReader {
pub fn get_cursor_current(&self) -> EventCursor<E> {
EventCursor {
last_event_count: self.event_count,
..Default::default()
}
}

#[deprecated(
since = "0.14.0",
note = "`get_reader` has been deprecated. Please use `get_cursor` instead."
)]
/// Gets a new [`EventCursor`]. This will include all events already in the event buffers.
pub fn get_reader(&self) -> EventCursor<E> {
EventCursor::default()
}

#[deprecated(
since = "0.14.0",
note = "`get_reader_current` has been replaced. Please use `get_cursor_current` instead."
)]
/// Gets a new [`EventCursor`]. This will ignore all events already in the event buffers.
/// It will read all future events.
pub fn get_reader_current(&self) -> EventCursor<E> {
EventCursor {
last_event_count: self.event_count,
..Default::default()
}
Expand Down
167 changes: 167 additions & 0 deletions crates/bevy_ecs/src/event/event_cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
use crate as bevy_ecs;
use bevy_ecs::event::{
Event, EventMutIterator, EventMutIteratorWithId, EventPeekIterator, EventPeekIteratorWithId,
EventReadIterator, EventReadIteratorWithId, Events,
};
#[cfg(feature = "multi_threaded")]
use bevy_ecs::event::{EventMutParIter, EventPeekParIter, EventReadParIter};
use std::marker::PhantomData;

// Deprecated in favor of `EventCursor`, there is no nice way to deprecate this
// because generic constraints are not allowed in type aliases, so this will always
// 'dead code'. Hence the `#[allow(dead_code)]`.
#[deprecated(
since = "0.14.0",
note = "`ManualEventReader` has been replaced. Please use `EventCursor` instead."
)]
#[doc(alias = "EventCursor")]
#[allow(dead_code)]
pub type ManualEventReader<E> = EventCursor<E>;

/// Stores the state for an [`EventReader`] or [`EventMutator`].
///
/// Access to the [`Events<E>`] resource is required to read any incoming events.
///
/// In almost all cases, you should just use an [`EventReader`] or [`EventMutator`],
/// which will automatically manage the state for you.
///
/// However, this type can be useful if you need to manually track events,
/// such as when you're attempting to send and receive events of the same type in the same system.
///
/// # Example
///
/// ```
/// use bevy_ecs::prelude::*;
/// use bevy_ecs::event::{Event, Events, EventCursor};
///
/// #[derive(Event, Clone, Debug)]
/// struct MyEvent;
///
/// /// A system that both sends and receives events using a [`Local`] [`EventCursor`].
/// fn send_and_receive_events(
/// // The `Local` `SystemParam` stores state inside the system itself, rather than in the world.
/// // `EventCursor<T>` is the internal state of `EventMutator<T>`, which tracks which events have been seen.
/// mut local_event_reader: Local<EventCursor<MyEvent>>,
/// // We can access the `Events` resource mutably, allowing us to both read and write its contents.
/// mut events: ResMut<Events<MyEvent>>,
/// ) {
/// // We must collect the events to resend, because we can't mutate events while we're iterating over the events.
/// let mut events_to_resend = Vec::new();
///
/// for event in local_event_reader.read(&mut events) {
/// events_to_resend.push(event.clone());
/// }
///
/// for event in events_to_resend {
/// events.send(MyEvent);
/// }
/// }
///
/// # bevy_ecs::system::assert_is_system(send_and_receive_events);
/// ```
#[derive(Debug)]
pub struct EventCursor<E: Event> {
pub(super) last_event_count: usize,
pub(super) _marker: PhantomData<E>,
}

impl<E: Event> Default for EventCursor<E> {
fn default() -> Self {
EventCursor {
last_event_count: 0,
_marker: Default::default(),
}
}
}

impl<E: Event> Clone for EventCursor<E> {
fn clone(&self) -> Self {
EventCursor {
last_event_count: self.last_event_count,
_marker: PhantomData,
}
}
}

#[allow(clippy::len_without_is_empty)] // Check fails since the is_empty implementation has a signature other than `(&self) -> bool`
impl<E: Event> EventCursor<E> {
/// See [`EventReader::read`]
pub fn read<'a>(&'a mut self, events: &'a Events<E>) -> EventReadIterator<'a, E> {
self.read_with_id(events).without_id()
}

/// See [`EventReader::peek`]
pub fn peek<'a>(&'a self, events: &'a Events<E>) -> EventPeekIterator<'a, E> {
self.peek_with_id(events).without_id()
}

/// See [`EventMutator::read`]
pub fn read_mut<'a>(&'a mut self, events: &'a mut Events<E>) -> EventMutIterator<'a, E> {
self.read_mut_with_id(events).without_id()
}

/// See [`EventReader::read_with_id`]
pub fn read_with_id<'a>(&'a mut self, events: &'a Events<E>) -> EventReadIteratorWithId<'a, E> {
EventReadIteratorWithId::new(self, events)
}

/// See [`EventReader::peek_with_id`]
pub fn peek_with_id<'a>(&'a self, events: &'a Events<E>) -> EventPeekIteratorWithId<'a, E> {
EventPeekIteratorWithId::new(self, events)
}

/// See [`EventMutator::read_with_id`]
pub fn read_mut_with_id<'a>(
&'a mut self,
events: &'a mut Events<E>,
) -> EventMutIteratorWithId<'a, E> {
EventMutIteratorWithId::new(self, events)
}

/// See [`EventReader::par_read`]
#[cfg(feature = "multi_threaded")]
pub fn par_read<'a>(&'a mut self, events: &'a Events<E>) -> EventReadParIter<'a, E> {
EventReadParIter::new(self, events)
}

/// See [`EventMutator::par_read`]
#[cfg(feature = "multi_threaded")]
pub fn par_read_mut<'a>(&'a mut self, events: &'a mut Events<E>) -> EventMutParIter<'a, E> {
EventMutParIter::new(self, events)
}

/// See [`EventReader::par_peek`]
#[cfg(feature = "multi_threaded")]
pub fn par_peek<'a>(&'a self, events: &'a Events<E>) -> EventPeekParIter<'a, E> {
EventPeekParIter::new(self, events)
}

/// See [`EventReader::len`]
pub fn len(&self, events: &Events<E>) -> usize {
// The number of events in this reader is the difference between the most recent event
// and the last event seen by it. This will be at most the number of events contained
// with the events (any others have already been dropped)
// TODO: Warn when there are dropped events, or return e.g. a `Result<usize, (usize, usize)>`
events
.event_count
.saturating_sub(self.last_event_count)
.min(events.len())
}

/// Amount of events we missed.
pub fn missed_events(&self, events: &Events<E>) -> usize {
events
.oldest_event_count()
.saturating_sub(self.last_event_count)
}

/// See [`EventReader::is_empty()`]
pub fn is_empty(&self, events: &Events<E>) -> bool {
self.len(events) == 0
}

/// See [`EventReader::clear()`]
pub fn clear(&mut self, events: &Events<E>) {
self.last_event_count = events.event_count;
}
}
Loading