EventStore.iter()

This commit is contained in:
Mike Dilger 2024-04-09 08:58:35 +12:00
parent ce18610918
commit a4233dbc45

View File

@ -130,6 +130,38 @@ impl EventStore {
}
}
}
pub fn iter(&self) -> EventStoreIter<'_> {
EventStoreIter {
store: self,
offset: mmap_append::HEADER_SIZE,
}
}
}
pub struct EventStoreIter<'a> {
store: &'a EventStore,
offset: usize,
}
impl<'a> Iterator for EventStoreIter<'a> {
type Item = Result<Event<'a>, Error>;
fn next(&mut self) -> Option<Self::Item> {
match self.store.get_event_by_offset(self.offset) {
Err(e) => {
if matches!(e.inner, ChorusError::EndOfInput) {
None
} else {
Some(Err(e))
}
},
Ok(event) => {
self.offset += event.length();
Some(Ok(event))
}
}
}
}
#[cfg(test)]