events: iter_current_update_events

This commit is contained in:
Carter Anderson 2020-06-16 22:20:08 -07:00
parent e855995145
commit 1dd81587dd

View File

@ -78,7 +78,7 @@ impl<T> Default for Events<T> {
} }
} }
fn map_event_instance<T>(event_instance: &EventInstance<T>) -> &T { fn map_instance_event<T>(event_instance: &EventInstance<T>) -> &T {
&event_instance.event &event_instance.event
} }
@ -119,28 +119,28 @@ impl<T> EventReader<T> {
.get(b_index..) .get(b_index..)
.unwrap_or_else(|| &[]) .unwrap_or_else(|| &[])
.iter() .iter()
.map(map_event_instance) .map(map_instance_event)
.chain( .chain(
events events
.events_a .events_a
.get(a_index..) .get(a_index..)
.unwrap_or_else(|| &[]) .unwrap_or_else(|| &[])
.iter() .iter()
.map(map_event_instance), .map(map_instance_event),
), ),
State::B => events State::B => events
.events_a .events_a
.get(a_index..) .get(a_index..)
.unwrap_or_else(|| &[]) .unwrap_or_else(|| &[])
.iter() .iter()
.map(map_event_instance) .map(map_instance_event)
.chain( .chain(
events events
.events_b .events_b
.get(b_index..) .get(b_index..)
.unwrap_or_else(|| &[]) .unwrap_or_else(|| &[])
.iter() .iter()
.map(map_event_instance), .map(map_instance_event),
), ),
} }
} }
@ -252,6 +252,18 @@ impl<T> Events<T> {
self.send(event); self.send(event);
} }
} }
/// 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),
}
}
} }
#[cfg(test)] #[cfg(test)]