Skip to content

Commit 9481a2c

Browse files
committed
Optimise EventReader::clear() and improve documentation (#7471)
# Objective Clearing the reader doesn't require iterating the events. Updating the `last_event_count` of the reader is enough. I rewrote part of the documentation as some of it was incorrect or harder to understand than necessary. ## Changelog Added `ManualEventReader::clear()` Co-authored-by: devil-ira <justthecooldude@gmail.com>
1 parent 00ff8ad commit 9481a2c

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

crates/bevy_ecs/src/event.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,15 @@ impl<'w, 's, E: Event> EventReader<'w, 's, E> {
207207
self.reader.len(&self.events)
208208
}
209209

210-
/// Determines if no events are available to be read without consuming any.
211-
/// If you need to consume the iterator you can use [`EventReader::clear`].
210+
/// Returns `true` if there are no events available to read.
212211
///
213212
/// # Example
214213
///
215-
/// The following example shows a common pattern of this function in conjunction with `clear`
216-
/// to avoid leaking events to the next schedule iteration while also checking if it was emitted.
214+
/// The following example shows a useful pattern where some behaviour is triggered if new events are available.
215+
/// [`EventReader::clear()`] is used so the same events don't re-trigger the behaviour the next time the system runs.
217216
///
218217
/// ```
219218
/// # use bevy_ecs::prelude::*;
220-
/// #
221219
/// struct CollisionEvent;
222220
///
223221
/// fn play_collision_sound(mut events: EventReader<CollisionEvent>) {
@@ -229,19 +227,17 @@ impl<'w, 's, E: Event> EventReader<'w, 's, E> {
229227
/// # bevy_ecs::system::assert_is_system(play_collision_sound);
230228
/// ```
231229
pub fn is_empty(&self) -> bool {
232-
self.len() == 0
230+
self.reader.is_empty(&self.events)
233231
}
234232

235-
/// Consumes the iterator.
233+
/// Consumes all available events.
236234
///
237-
/// This means all currently available events will be removed before the next frame.
238-
/// This is useful when multiple events are sent in a single frame and you want
239-
/// to react to one or more events without needing to know how many were sent.
240-
/// In those situations you generally want to consume those events to make sure they don't appear in the next frame.
235+
/// This means these events will not appear in calls to [`EventReader::iter()`] or
236+
/// [`EventReader::iter_with_id()`] and [`EventReader::is_empty()`] will return `true`.
241237
///
242-
/// For more information see [`EventReader::is_empty()`].
238+
/// For usage, see [`EventReader::is_empty()`].
243239
pub fn clear(&mut self) {
244-
self.iter().last();
240+
self.reader.clear(&self.events);
245241
}
246242
}
247243

@@ -362,10 +358,15 @@ impl<E: Event> ManualEventReader<E> {
362358
.saturating_sub(self.last_event_count)
363359
}
364360

365-
/// See [`EventReader::is_empty`]
361+
/// See [`EventReader::is_empty()`]
366362
pub fn is_empty(&self, events: &Events<E>) -> bool {
367363
self.len(events) == 0
368364
}
365+
366+
/// See [`EventReader::clear()`]
367+
pub fn clear(&mut self, events: &Events<E>) {
368+
self.last_event_count = events.event_count;
369+
}
369370
}
370371

371372
pub struct ManualEventIterator<'a, E: Event> {

0 commit comments

Comments
 (0)