Skip to content

Commit 280a4b7

Browse files
committed
feat(sdk): Add LinkedChunk::first_chunk.
This patch adds `LinkedChunk::first_chunk`, and exposes it to `EventLinkedChunk` to replace the use of 2 iterators in `RoomPagination` and `ThreadPagination`, and to remove 2 `unwrap`s.
1 parent 3fce422 commit 280a4b7

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

crates/matrix-sdk-common/src/linked_chunk/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,11 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
11101110
.skip(position.index()))
11111111
}
11121112

1113+
/// Return the first chunk.
1114+
pub fn first_chunk(&self) -> &Chunk<CAP, Item, Gap> {
1115+
self.links.first_chunk()
1116+
}
1117+
11131118
/// Get a mutable reference to the `LinkedChunk` updates, aka
11141119
/// [`ObservableUpdates`].
11151120
///

crates/matrix-sdk/src/event_cache/caches/event_linked_chunk.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ impl EventLinkedChunk {
177177
self.chunks.chunk_identifier(predicate)
178178
}
179179

180+
/// Return the first chunk.
181+
pub fn first_chunk(&self) -> &Chunk<DEFAULT_CHUNK_CAPACITY, Event, Gap> {
182+
self.chunks.first_chunk()
183+
}
184+
180185
/// Iterate over the chunks, forward.
181186
///
182187
/// The oldest chunk comes first.

crates/matrix-sdk/src/event_cache/caches/room/pagination.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ impl PaginatedCache for Arc<RoomEventCacheInner> {
170170
});
171171
}
172172

173-
let prev_first_chunk =
174-
state.room_linked_chunk().chunks().next().expect("a linked chunk is never empty");
173+
let prev_first_chunk = state.room_linked_chunk().first_chunk();
175174

176175
// The first chunk is not a gap, we can load its previous chunk.
177176
let linked_chunk_id = LinkedChunkId::Room(&state.state.room_id);

crates/matrix-sdk/src/event_cache/caches/thread/pagination.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ impl PaginatedCache for ThreadEventCacheWrapper {
112112
});
113113
}
114114

115-
let prev_first_chunk =
116-
state.thread_linked_chunk().chunks().next().expect("a linked chunk is never empty");
115+
let prev_first_chunk = state.thread_linked_chunk().first_chunk();
117116

118117
// The first chunk is not a gap, we can load its previous chunk.
119118
let linked_chunk_id = LinkedChunkId::Thread(&state.room_id, &state.thread_id);

0 commit comments

Comments
 (0)