Borrow instead of consuming in EventReader::clear (#6851)

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>
This commit is contained in:
Vladyslav Batyrenko 2022-12-05 23:07:20 +00:00
parent 77c59c22ab
commit b337ed63ad
2 changed files with 4 additions and 4 deletions

View file

@ -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
@ -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();
}
}
@ -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

View file

@ -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>,
) {