Skip to content

Commit

Permalink
Fix Events::drain bug
Browse files Browse the repository at this point in the history
Co-authored-by: Alice-Cecile alice-i-cecile@users.noreply.github.com
  • Loading branch information
NathanSWard committed May 18, 2021
1 parent d4ffa3f commit cb71e4d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ impl<T: Component> Events<T> {

/// Creates a draining iterator that removes all events.
pub fn drain(&mut self) -> impl Iterator<Item = T> + '_ {
self.a_start_event_count = 0;
self.b_start_event_count = 0;
self.event_count = 0;

let map = |i: EventInstance<T>| i.event;
match self.state {
State::A => self
Expand Down Expand Up @@ -480,4 +484,24 @@ mod tests {
) -> Vec<TestEvent> {
reader.iter(events).cloned().collect::<Vec<TestEvent>>()
}

#[test]
fn test_events_drain() {
struct E;
let mut events = Events::<E>::default();
let mut reader = events.get_reader();

assert!(reader.iter(&events).next().is_none());

events.send(E);
assert!(reader.iter(&events).next().is_some());
assert!(reader.iter(&events).next().is_none());

let _ = events.drain();
assert!(reader.iter(&events).next().is_none());

events.send(E);
assert!(reader.iter(&events).next().is_some());
assert!(reader.iter(&events).next().is_none());
}
}

0 comments on commit cb71e4d

Please sign in to comment.