Skip to content

Commit bf88bea

Browse files
committed
!feedback
1 parent 4fe2302 commit bf88bea

File tree

1 file changed

+23
-14
lines changed
  • crates/matrix-sdk/src/event_cache

1 file changed

+23
-14
lines changed

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ impl RoomEventCacheInner {
694694
let new_chunk = room_events
695695
.replace_gap_at(sync_events, gap_identifier)
696696
// SAFETY: we are sure that `gap_identifier` represents a valid
697-
// `ChunkIdentifier` for a gap.
698-
.unwrap();
697+
// `ChunkIdentifier` for a `Gap` chunk.
698+
.expect("The `gap_identifier` must represent a `Gap`");
699699

700700
new_chunk.first_position()
701701
};
@@ -704,9 +704,9 @@ impl RoomEventCacheInner {
704704
if let Some(prev_token_gap) = prev_token {
705705
room_events
706706
.insert_gap_at(prev_token_gap, new_position)
707-
// SAFETY: we are sure that `gap_identifier` represents a valid
708-
// `ChunkIdentifier` for a gap.
709-
.unwrap();
707+
// SAFETY: we are sure that `new_position` represents a valid
708+
// `ChunkIdentifier` for an `Item` chunk.
709+
.expect("The `new_position` must represent an `Item`");
710710
}
711711

712712
trace!("replaced gap with new events from backpagination");
@@ -715,20 +715,30 @@ impl RoomEventCacheInner {
715715
//let _ = self.sender.send(RoomEventCacheUpdate::Prepend { events });
716716

717717
Ok(BackPaginationOutcome::Success { events, reached_start })
718-
}
719-
// There is no `token`/gap identifier. Let's assume we must prepend the new events.
720-
else {
718+
} else {
719+
// There is no `token`/gap identifier. Let's assume we must prepend the new
720+
// events.
721721
let first_item_position =
722-
room_events.events().nth(0).map(|(item_position, _)| item_position);
722+
room_events.events().next().map(|(item_position, _)| item_position);
723723

724724
match first_item_position {
725725
// Is there a first item? Insert at this position.
726-
Some(item_position) => {
726+
Some(first_item_position) => {
727727
if let Some(prev_token_gap) = prev_token {
728-
room_events.insert_gap_at(prev_token_gap, item_position).unwrap();
728+
room_events
729+
.insert_gap_at(prev_token_gap, first_item_position)
730+
// SAFETY: The `first_item_position` can only be an `Item` chunk, it's
731+
// an invariant of `LinkedChunk`. Also, it can only represent a valid
732+
// `ChunkIdentifier` as the data structure isn't modified yet.
733+
.expect("The `first_item_position` must represent a valid `Item`");
729734
}
730735

731-
room_events.insert_events_at(sync_events, item_position).unwrap();
736+
room_events
737+
.insert_events_at(sync_events, first_item_position)
738+
// SAFETY: The `first_item_position` can only be an `Item` chunk, it's
739+
// an invariant of `LinkedChunk`. The chunk it points to has not been
740+
// removed.
741+
.expect("The `first_item_position` must represent an `Item`");
732742
}
733743

734744
// There is no first item. Let's simply push.
@@ -1031,8 +1041,7 @@ mod tests {
10311041

10321042
event_cache.subscribe().unwrap();
10331043

1034-
let (room_event_cache, _drop_handles) =
1035-
client.event_cache().for_room(room_id).await.unwrap();
1044+
let (room_event_cache, _drop_handles) = event_cache.for_room(room_id).await.unwrap();
10361045
let room_event_cache = room_event_cache.unwrap();
10371046

10381047
let expected_token = PaginationToken("old".to_owned());

0 commit comments

Comments
 (0)