Skip to content

Commit 03eb373

Browse files
committed
!again
1 parent 1ed23db commit 03eb373

File tree

1 file changed

+57
-3
lines changed
  • crates/matrix-sdk/src/event_cache

1 file changed

+57
-3
lines changed

crates/matrix-sdk/src/event_cache/store.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ pub mod experimental {
166166
pub fn iter_events<'a>(&'a self) -> impl Iterator<Item = (ItemPosition, &'a Event)> {
167167
self.chunks.iter_items()
168168
}
169+
170+
/// Iterate over the events, starting from `position`.
171+
pub fn iter_events_from<'a>(
172+
&'a self,
173+
position: ItemPosition,
174+
) -> Result<impl Iterator<Item = (ItemPosition, &'a Event)>, LinkedChunkError> {
175+
self.chunks.iter_items_from(position)
176+
}
169177
}
170178

171179
#[derive(Debug)]
@@ -387,8 +395,9 @@ pub mod experimental {
387395
///
388396
/// It iterates from the last to the first chunk.
389397
fn iter_chunks<'a>(&'a self) -> LinkedChunkIter<'a, T, C> {
390-
self.iter_chunks_from(0)
391-
.expect("`iter_chunks_from` cannot fail because at least one chunk must exist")
398+
self.iter_chunks_from(0).expect(
399+
"`iter_chunks_from` cannot fail because at least one empty chunk must exist",
400+
)
392401
}
393402

394403
/// Iterate over the chunks, starting from `position`.
@@ -409,7 +418,16 @@ pub mod experimental {
409418
///
410419
/// It iterates from the last the first item.
411420
fn iter_items<'a>(&'a self) -> impl Iterator<Item = (ItemPosition, &'a T)> {
412-
self.iter_chunks()
421+
self.iter_items_from(ItemPosition(0, 0))
422+
.expect("`iter_items_from` cannot fail because at least one empty chunk must exist")
423+
}
424+
425+
fn iter_items_from<'a>(
426+
&'a self,
427+
position: ItemPosition,
428+
) -> Result<impl Iterator<Item = (ItemPosition, &'a T)>, LinkedChunkError> {
429+
Ok(self
430+
.iter_chunks_from(position.chunk_index())?
413431
.filter_map(|(chunk_index, chunk)| match &chunk.content {
414432
ChunkContent::Gap => None,
415433
ChunkContent::Items(items) => {
@@ -419,6 +437,7 @@ pub mod experimental {
419437
}
420438
})
421439
.flatten()
440+
.skip(position.item_index()))
422441
}
423442
}
424443

@@ -878,6 +897,41 @@ pub mod experimental {
878897
Ok(())
879898
}
880899

900+
#[test]
901+
fn test_iter_items() {
902+
let mut events = Events::<char, 2>::new();
903+
events.push_events(['a', 'b']);
904+
events.push_gap();
905+
events.push_events(['c', 'd', 'e']);
906+
907+
let mut iterator = events.iter_events();
908+
909+
assert_matches!(iterator.next(), Some((ItemPosition(0, 0), 'e')));
910+
assert_matches!(iterator.next(), Some((ItemPosition(1, 0), 'd')));
911+
assert_matches!(iterator.next(), Some((ItemPosition(1, 1), 'c')));
912+
assert_matches!(iterator.next(), Some((ItemPosition(3, 0), 'b')));
913+
assert_matches!(iterator.next(), Some((ItemPosition(3, 1), 'a')));
914+
assert_matches!(iterator.next(), None);
915+
}
916+
917+
#[test]
918+
fn test_iter_items_from() -> Result<(), LinkedChunkError> {
919+
let mut events = Events::<char, 2>::new();
920+
events.push_events(['a', 'b']);
921+
events.push_gap();
922+
events.push_events(['c', 'd', 'e']);
923+
924+
let mut iterator =
925+
events.iter_events_from(events.event_position(|event| *event == 'c').unwrap())?;
926+
927+
assert_matches!(iterator.next(), Some((ItemPosition(1, 1), 'c')));
928+
assert_matches!(iterator.next(), Some((ItemPosition(3, 0), 'b')));
929+
assert_matches!(iterator.next(), Some((ItemPosition(3, 1), 'a')));
930+
assert_matches!(iterator.next(), None);
931+
932+
Ok(())
933+
}
934+
881935
#[test]
882936
fn test_insert_items_at() -> Result<(), LinkedChunkError> {
883937
let mut events = Events::<char, 3>::new();

0 commit comments

Comments
 (0)