Skip to content

Commit a8e522c

Browse files
committed
feat(sdk): Implement LinkedChunk::chunks.
This patch implements the `LinkedChunk::chunks` method that returns a forward iterator over the chunks.
1 parent 9dcab4e commit a8e522c

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,15 @@ impl<Item, Gap, const CAP: usize> LinkedChunk<Item, Gap, CAP> {
367367
/// It iterates from the last to the first chunk.
368368
pub fn rchunks(&self) -> LinkedChunkIterBackward<'_, Item, Gap, CAP> {
369369
self.rchunks_from(self.latest_chunk().identifier())
370-
.expect("`iter_chunks_from` cannot fail because at least one empty chunk must exist")
370+
.expect("`rchunks_from` cannot fail because at least one empty chunk must exist")
371+
}
372+
373+
/// Iterate over the chunks, forward.
374+
///
375+
/// It iterates from the first to the last chunk.
376+
pub fn chunks(&self) -> LinkedChunkIter<'_, Item, Gap, CAP> {
377+
self.chunks_from(ChunkIdentifierGenerator::FIRST_IDENTIFIER)
378+
.expect("`chunks_from` cannot fail because at least one empty chunk must exist")
371379
}
372380

373381
/// Iterate over the chunks, starting from `identifier`, backward.
@@ -403,7 +411,7 @@ impl<Item, Gap, const CAP: usize> LinkedChunk<Item, Gap, CAP> {
403411
/// It iterates from the last to the first item.
404412
pub fn ritems(&self) -> impl Iterator<Item = (ItemPosition, &Item)> {
405413
self.ritems_from(self.latest_chunk().identifier().to_last_item_position())
406-
.expect("`iter_items_from` cannot fail because at least one empty chunk must exist")
414+
.expect("`ritems_from` cannot fail because at least one empty chunk must exist")
407415
}
408416

409417
/// Iterate over the items, forward.
@@ -1117,6 +1125,40 @@ mod tests {
11171125
assert_matches!(iterator.next(), None);
11181126
}
11191127

1128+
#[test]
1129+
fn test_chunks() {
1130+
let mut linked_chunk = LinkedChunk::<char, (), 2>::new();
1131+
linked_chunk.push_items_back(['a', 'b']);
1132+
linked_chunk.push_gap_back(());
1133+
linked_chunk.push_items_back(['c', 'd', 'e']);
1134+
1135+
let mut iterator = linked_chunk.chunks();
1136+
1137+
assert_matches!(
1138+
iterator.next(),
1139+
Some(Chunk { identifier: ChunkIdentifier(0), content: ChunkContent::Items(items), .. }) => {
1140+
assert_eq!(items, &['a', 'b']);
1141+
}
1142+
);
1143+
assert_matches!(
1144+
iterator.next(),
1145+
Some(Chunk { identifier: ChunkIdentifier(1), content: ChunkContent::Gap(..), .. })
1146+
);
1147+
assert_matches!(
1148+
iterator.next(),
1149+
Some(Chunk { identifier: ChunkIdentifier(2), content: ChunkContent::Items(items), .. }) => {
1150+
assert_eq!(items, &['c', 'd']);
1151+
}
1152+
);
1153+
assert_matches!(
1154+
iterator.next(),
1155+
Some(Chunk { identifier: ChunkIdentifier(3), content: ChunkContent::Items(items), .. }) => {
1156+
assert_eq!(items, &['e']);
1157+
}
1158+
);
1159+
assert_matches!(iterator.next(), None);
1160+
}
1161+
11201162
#[test]
11211163
fn test_rchunks_from() -> Result<(), LinkedChunkError> {
11221164
let mut linked_chunk = LinkedChunk::<char, (), 2>::new();

0 commit comments

Comments
 (0)