Skip to content

Commit

Permalink
implement Events::extend using impl std::iter::Extend
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSWard committed May 18, 2021
1 parent 514b70f commit 955c48e
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions crates/bevy_ecs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,27 @@ impl<T: Component> Events<T> {
}
}

pub fn extend<I>(&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<Item = &T> {
match self.state {
State::A => self.events_a.iter().map(map_instance_event),
State::B => self.events_b.iter().map(map_instance_event),
}
}
}

impl<T> std::iter::Extend<T> for Events<T> {
fn extend<I>(&mut self, iter: I)
where
I: Iterator<Item = T>,
I: IntoIterator<Item = T>,
{
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,
Expand Down Expand Up @@ -496,4 +511,15 @@ mod tests {
) -> Vec<TestEvent> {
reader.iter(events).cloned().collect::<Vec<TestEvent>>()
}

#[test]
fn test_events_extent_impl() {
let mut events = Events::<TestEvent>::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()));
}
}

0 comments on commit 955c48e

Please sign in to comment.