From cead90253ea43a921e50a71091ea3a33735ff8d0 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Tue, 28 Apr 2026 11:04:06 +0200 Subject: [PATCH] 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. --- crates/matrix-sdk-common/src/linked_chunk/mod.rs | 5 +++++ .../matrix-sdk/src/event_cache/caches/event_linked_chunk.rs | 5 +++++ crates/matrix-sdk/src/event_cache/caches/room/pagination.rs | 3 +-- .../matrix-sdk/src/event_cache/caches/thread/pagination.rs | 3 +-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/matrix-sdk-common/src/linked_chunk/mod.rs b/crates/matrix-sdk-common/src/linked_chunk/mod.rs index 31347ba66..b6115b327 100644 --- a/crates/matrix-sdk-common/src/linked_chunk/mod.rs +++ b/crates/matrix-sdk-common/src/linked_chunk/mod.rs @@ -1110,6 +1110,11 @@ impl LinkedChunk { .skip(position.index())) } + /// Return the first chunk. + pub fn first_chunk(&self) -> &Chunk { + self.links.first_chunk() + } + /// Get a mutable reference to the `LinkedChunk` updates, aka /// [`ObservableUpdates`]. /// diff --git a/crates/matrix-sdk/src/event_cache/caches/event_linked_chunk.rs b/crates/matrix-sdk/src/event_cache/caches/event_linked_chunk.rs index f4265dd8d..edfe17d4d 100644 --- a/crates/matrix-sdk/src/event_cache/caches/event_linked_chunk.rs +++ b/crates/matrix-sdk/src/event_cache/caches/event_linked_chunk.rs @@ -177,6 +177,11 @@ impl EventLinkedChunk { self.chunks.chunk_identifier(predicate) } + /// Return the first chunk. + pub fn first_chunk(&self) -> &Chunk { + self.chunks.first_chunk() + } + /// Iterate over the chunks, forward. /// /// The oldest chunk comes first. diff --git a/crates/matrix-sdk/src/event_cache/caches/room/pagination.rs b/crates/matrix-sdk/src/event_cache/caches/room/pagination.rs index b99fe6c7d..66b1089bc 100644 --- a/crates/matrix-sdk/src/event_cache/caches/room/pagination.rs +++ b/crates/matrix-sdk/src/event_cache/caches/room/pagination.rs @@ -170,8 +170,7 @@ impl PaginatedCache for Arc { }); } - let prev_first_chunk = - state.room_linked_chunk().chunks().next().expect("a linked chunk is never empty"); + let prev_first_chunk = state.room_linked_chunk().first_chunk(); // The first chunk is not a gap, we can load its previous chunk. let linked_chunk_id = LinkedChunkId::Room(&state.state.room_id); diff --git a/crates/matrix-sdk/src/event_cache/caches/thread/pagination.rs b/crates/matrix-sdk/src/event_cache/caches/thread/pagination.rs index e8223db4e..7f28e81f1 100644 --- a/crates/matrix-sdk/src/event_cache/caches/thread/pagination.rs +++ b/crates/matrix-sdk/src/event_cache/caches/thread/pagination.rs @@ -112,8 +112,7 @@ impl PaginatedCache for ThreadEventCacheWrapper { }); } - let prev_first_chunk = - state.thread_linked_chunk().chunks().next().expect("a linked chunk is never empty"); + let prev_first_chunk = state.thread_linked_chunk().first_chunk(); // The first chunk is not a gap, we can load its previous chunk. let linked_chunk_id = LinkedChunkId::Thread(&state.room_id, &state.thread_id);