Skip to content

[Merged by Bors] - Increment last event count on next instead of iter #2382

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
48 changes: 14 additions & 34 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ impl<T> Default for ManualEventReader<T> {

impl<T> ManualEventReader<T> {
/// See [`EventReader::iter`]
pub fn iter<'a>(&mut self, events: &'a Events<T>) -> impl DoubleEndedIterator<Item = &'a T> {
pub fn iter<'a>(&'a mut self, events: &'a Events<T>) -> impl DoubleEndedIterator<Item = &'a T> {
internal_event_reader(&mut self.last_event_count, events).map(|(e, _)| e)
}

/// See [`EventReader::iter_with_id`]
pub fn iter_with_id<'a>(
&mut self,
&'a mut self,
events: &'a Events<T>,
) -> impl DoubleEndedIterator<Item = (&'a T, EventId<T>)> {
internal_event_reader(&mut self.last_event_count, events)
Expand All @@ -204,7 +204,7 @@ impl<T> ManualEventReader<T> {
/// Like [`iter_with_id`](EventReader::iter_with_id) except not emitting any traces for read
/// messages.
fn internal_event_reader<'a, T>(
last_event_count: &mut usize,
last_event_count: &'a mut usize,
events: &'a Events<T>,
) -> impl DoubleEndedIterator<Item = (&'a T, EventId<T>)> {
// if the reader has seen some of the events in a buffer, find the proper index offset.
Expand All @@ -219,37 +219,17 @@ fn internal_event_reader<'a, T>(
} else {
0
};
*last_event_count = events.event_count;
match events.state {
State::A => events
.events_b
.get(b_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id)
.chain(
events
.events_a
.get(a_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id),
),
State::B => events
.events_a
.get(a_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id)
.chain(
events
.events_b
.get(b_index..)
.unwrap_or_else(|| &[])
.iter()
.map(map_instance_event_with_id),
),
}
let a = events.events_a.get(a_index..).unwrap_or_else(|| &[]);
let b = events.events_b.get(b_index..).unwrap_or_else(|| &[]);
let unread_count = a.len() + b.len();
*last_event_count = events.event_count - unread_count;
let iterator = match events.state {
State::A => b.iter().chain(a.iter()),
State::B => a.iter().chain(b.iter()),
};
iterator
.map(map_instance_event_with_id)
.inspect(move |(_, id)| *last_event_count = (id.id + 1).max(*last_event_count))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the function returns a DoubleEndedIterator, will this logic also work when calling .next_back?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if calling next_back, all events in the iterator will be considered consumed if called from a new iterator, but they will still be available in the current iterator.

I think this is the most logical way to do it 👍

}

impl<'a, T: Component> EventReader<'a, T> {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pub fn scene_spawner_system(world: &mut World) {
.unwrap();

let mut updated_spawned_scenes = Vec::new();
let scene_spawner = &mut *scene_spawner;
for event in scene_spawner
.scene_asset_event_reader
.iter(&scene_asset_events)
Expand Down