From 8b1abbb51cb9dea923e05320287d83fcc123fe1b Mon Sep 17 00:00:00 2001 From: NathanW Date: Mon, 17 May 2021 20:49:43 -0600 Subject: [PATCH 1/2] Fix Events:: bug Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/event.rs | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/crates/bevy_ecs/src/event.rs b/crates/bevy_ecs/src/event.rs index 889d9d22e8b4c..9d10fea2e8f6a 100644 --- a/crates/bevy_ecs/src/event.rs +++ b/crates/bevy_ecs/src/event.rs @@ -328,14 +328,25 @@ impl Events { events.update(); } + #[inline] + fn increment_start_event_count(&mut self) { + let count = self.event_count + 1; + self.a_start_event_count = count; + self.b_start_event_count = count; + } + /// Removes all events. + #[inline] pub fn clear(&mut self) { + self.increment_start_event_count(); self.events_a.clear(); self.events_b.clear(); } /// Creates a draining iterator that removes all events. pub fn drain(&mut self) -> impl Iterator + '_ { + self.increment_start_event_count(); + let map = |i: EventInstance| i.event; match self.state { State::A => self @@ -480,4 +491,40 @@ mod tests { ) -> Vec { reader.iter(events).cloned().collect::>() } + + #[derive(PartialEq, Eq, Debug)] + struct E(usize); + + fn events_clear_and_read_impl(clear_func: impl FnOnce(&mut Events)) { + let mut events = Events::::default(); + let mut reader = events.get_reader(); + + assert!(reader.iter(&events).next().is_none()); + + events.send(E(0)); + assert_eq!(*reader.iter(&events).next().unwrap(), E(0)); + assert_eq!(reader.iter(&events).next(), None); + + events.send(E(1)); + clear_func(&mut events); + assert!(reader.iter(&events).next().is_none()); + + events.send(E(2)); + events.update(); + events.send(E(3)); + + assert!(reader.iter(&events).eq([E(2), E(3)].iter())); + } + + #[test] + fn test_events_clear_and_read() { + events_clear_and_read_impl(|events| events.clear()); + } + + #[test] + fn test_events_drain_and_read() { + events_clear_and_read_impl(|events| { + assert!(events.drain().eq(vec![E(0), E(1)].into_iter())); + }); + } } From f64a1170164b20d6c12f4228d53118282da71fb4 Mon Sep 17 00:00:00 2001 From: NathanW Date: Tue, 18 May 2021 19:55:03 -0600 Subject: [PATCH 2/2] fix reseting the start_event_count on clear/drain --- crates/bevy_ecs/src/event.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/event.rs b/crates/bevy_ecs/src/event.rs index 9d10fea2e8f6a..36113eb39dde4 100644 --- a/crates/bevy_ecs/src/event.rs +++ b/crates/bevy_ecs/src/event.rs @@ -329,23 +329,22 @@ impl Events { } #[inline] - fn increment_start_event_count(&mut self) { - let count = self.event_count + 1; - self.a_start_event_count = count; - self.b_start_event_count = count; + fn reset_start_event_count(&mut self) { + self.a_start_event_count = self.event_count; + self.b_start_event_count = self.event_count; } /// Removes all events. #[inline] pub fn clear(&mut self) { - self.increment_start_event_count(); + self.reset_start_event_count(); self.events_a.clear(); self.events_b.clear(); } /// Creates a draining iterator that removes all events. pub fn drain(&mut self) -> impl Iterator + '_ { - self.increment_start_event_count(); + self.reset_start_event_count(); let map = |i: EventInstance| i.event; match self.state {