From 955c48eca375ae3c62f5049e8ab8574e6c0cb6a1 Mon Sep 17 00:00:00 2001 From: NathanW Date: Tue, 18 May 2021 16:27:45 -0600 Subject: [PATCH] implement Events::extend using impl std::iter::Extend --- crates/bevy_ecs/src/event.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ecs/src/event.rs b/crates/bevy_ecs/src/event.rs index 8f88fe8f3c82d5..9cf9c320138872 100644 --- a/crates/bevy_ecs/src/event.rs +++ b/crates/bevy_ecs/src/event.rs @@ -351,12 +351,27 @@ impl Events { } } - pub fn extend(&mut self, events: I) + /// Iterates over events that happened since the last "update" call. + /// WARNING: You probably don't want to use this call. In most cases you should use an + /// `EventReader`. You should only use this if you know you only need to consume events + /// between the last `update()` call and your call to `iter_current_update_events`. + /// If events happen outside that window, they will not be handled. For example, any events that + /// happen after this call and before the next `update()` call will be dropped. + pub fn iter_current_update_events(&self) -> impl DoubleEndedIterator { + match self.state { + State::A => self.events_a.iter().map(map_instance_event), + State::B => self.events_b.iter().map(map_instance_event), + } + } +} + +impl std::iter::Extend for Events { + fn extend(&mut self, iter: I) where - I: Iterator, + I: IntoIterator, { let mut event_count = self.event_count; - let events = events.map(|event| { + let events = iter.into_iter().map(|event| { let event_id = EventId { id: event_count, _marker: PhantomData, @@ -496,4 +511,15 @@ mod tests { ) -> Vec { reader.iter(events).cloned().collect::>() } + + #[test] + fn test_events_extent_impl() { + let mut events = Events::::default(); + let mut reader = events.get_reader(); + + events.extend(vec![TestEvent { i: 0 }, TestEvent { i: 1 }]); + assert!(reader + .iter(&events) + .eq([TestEvent { i: 0 }, TestEvent { i: 1 }].iter())); + } }