@@ -694,8 +694,8 @@ impl RoomEventCacheInner {
694
694
let new_chunk = room_events
695
695
. replace_gap_at ( sync_events, gap_identifier)
696
696
// 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`" ) ;
699
699
700
700
new_chunk. first_position ( )
701
701
} ;
@@ -704,9 +704,9 @@ impl RoomEventCacheInner {
704
704
if let Some ( prev_token_gap) = prev_token {
705
705
room_events
706
706
. 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`" ) ;
710
710
}
711
711
712
712
trace ! ( "replaced gap with new events from backpagination" ) ;
@@ -715,20 +715,30 @@ impl RoomEventCacheInner {
715
715
//let _ = self.sender.send(RoomEventCacheUpdate::Prepend { events });
716
716
717
717
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.
721
721
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) ;
723
723
724
724
match first_item_position {
725
725
// Is there a first item? Insert at this position.
726
- Some ( item_position ) => {
726
+ Some ( first_item_position ) => {
727
727
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`" ) ;
729
734
}
730
735
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`" ) ;
732
742
}
733
743
734
744
// There is no first item. Let's simply push.
@@ -1031,8 +1041,7 @@ mod tests {
1031
1041
1032
1042
event_cache. subscribe ( ) . unwrap ( ) ;
1033
1043
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 ( ) ;
1036
1045
let room_event_cache = room_event_cache. unwrap ( ) ;
1037
1046
1038
1047
let expected_token = PaginationToken ( "old" . to_owned ( ) ) ;
0 commit comments