Skip to content

Commit

Permalink
Borrow instead of consuming in EventReader::clear (bevyengine#6851)
Browse files Browse the repository at this point in the history
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader. 

## Migration Guide

`EventReader::clear` now takes a mutable reference instead of consuming the event reader. This means that `clear` now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

```rust
// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}
``` 

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
  • Loading branch information
2 people authored and ItsDoot committed Feb 1, 2023
1 parent 7f44f54 commit 182f207
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl<'w, 's, E: Event> EventReader<'w, 's, E> {
/// #
/// struct CollisionEvent;
///
/// fn play_collision_sound(events: EventReader<CollisionEvent>) {
/// fn play_collision_sound(mut events: EventReader<CollisionEvent>) {
/// if !events.is_empty() {
/// events.clear();
/// // Play a sound
Expand All @@ -246,7 +246,7 @@ impl<'w, 's, E: Event> EventReader<'w, 's, E> {
/// In those situations you generally want to consume those events to make sure they don't appear in the next frame.
///
/// For more information see [`EventReader::is_empty()`].
pub fn clear(mut self) {
pub fn clear(&mut self) {
self.iter().last();
}
}
Expand Down Expand Up @@ -817,7 +817,7 @@ mod tests {
events.send(TestEvent { i: 0 });
world.insert_resource(events);

let mut reader = IntoSystem::into_system(|events: EventReader<TestEvent>| -> bool {
let mut reader = IntoSystem::into_system(|mut events: EventReader<TestEvent>| -> bool {
if !events.is_empty() {
events.clear();
false
Expand Down
2 changes: 1 addition & 1 deletion examples/games/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ fn check_for_collisions(
}

fn play_collision_sound(
collision_events: EventReader<CollisionEvent>,
mut collision_events: EventReader<CollisionEvent>,
audio: Res<Audio>,
sound: Res<CollisionSound>,
) {
Expand Down

0 comments on commit 182f207

Please sign in to comment.