Skip to content

Commit f4034db

Browse files
committed
feat(sdk) LinkedChunkError is a real error now.
1 parent fcc8db0 commit f4034db

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#![allow(dead_code)]
1616

1717
use std::{
18+
convert::Infallible,
19+
error::Error,
1820
fmt,
1921
marker::PhantomData,
2022
ops::Not,
@@ -26,11 +28,18 @@ use std::{
2628
};
2729

2830
/// Errors of [`LinkedChunk`].
29-
#[derive(Debug)]
31+
#[derive(thiserror::Error, Debug)]
3032
pub enum LinkedChunkError {
33+
#[error("The chunk identifier is invalid: `{identifier:?}`")]
3134
InvalidChunkIdentifier { identifier: ChunkIdentifier },
35+
36+
#[error("The chunk is a gap: `{identifier:?}`")]
3237
ChunkIsAGap { identifier: ChunkIdentifier },
38+
39+
#[error("The chunk is an item: `{identifier:?}`")]
3340
ChunkIsItems { identifier: ChunkIdentifier },
41+
42+
#[error("The item index is invalid: `{index}`")]
3443
InvalidItemIndex { index: usize },
3544
}
3645

@@ -1120,7 +1129,7 @@ impl<Item, Gap> LinkedChunkListener<Item, Gap> for () {
11201129

11211130
#[cfg(test)]
11221131
mod tests {
1123-
use std::fmt::Debug;
1132+
use std::{convert::Infallible, fmt::Debug};
11241133

11251134
use assert_matches::assert_matches;
11261135

@@ -1256,7 +1265,7 @@ mod tests {
12561265
Item: Clone + Debug + Send + Sync,
12571266
Gap: Clone + Debug + Send + Sync,
12581267
{
1259-
type Error = ();
1268+
type Error = Infallible;
12601269

12611270
fn new_chunk_items(
12621271
&self,
@@ -1808,7 +1817,7 @@ mod tests {
18081817
let position_after_f =
18091818
Position(position_of_f.chunk_identifier(), position_of_f.index() + 1);
18101819

1811-
linked_chunk.insert_items_at(['p', 'q'], position_after_f).await?;
1820+
linked_chunk.insert_items_at(['p', 'q'], position_after_f)?;
18121821
assert_items_eq!(
18131822
linked_chunk,
18141823
['l', 'm', 'n'] ['o', 'a', 'b'] ['r', 's', 'c'] ['d', 'w', 'x'] ['y', 'z', 'e'] ['f', 'p', 'q']

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl EventCache {
215215
// re-creating the `RoomEventCache` would create a new unrelated sender.
216216

217217
let rooms = inner.by_room.write().await;
218+
218219
for room in rooms.values() {
219220
// Notify all the observers that we've lost track of state. (We ignore the
220221
// error if there aren't any.)
@@ -761,19 +762,19 @@ impl RoomEventCacheInner {
761762
Some(first_event_pos) => {
762763
if let Some(prev_token_gap) = prev_token {
763764
room_events
764-
.insert_gap_at(prev_token_gap, first_event_pos)
765-
// SAFETY: The `first_event_pos` can only be an `Item` chunk, it's
766-
// an invariant of `LinkedChunk`. Also, it can only represent a valid
767-
// `ChunkIdentifier` as the data structure isn't modified yet.
768-
.expect("`first_event_pos` must point to a valid `Item` chunk when inserting a gap");
765+
.insert_gap_at(prev_token_gap, first_event_pos)
766+
// SAFETY: The `first_event_pos` can only be an `Item` chunk, it's
767+
// an invariant of `LinkedChunk`. Also, it can only represent a valid
768+
// `ChunkIdentifier` as the data structure isn't modified yet.
769+
.expect("`first_event_pos` must point to a valid `Item` chunk when inserting a gap");
769770
}
770771

771772
room_events
772-
.insert_events_at(sync_events, first_event_pos)
773-
// SAFETY: The `first_event_pos` can only be an `Item` chunk, it's
774-
// an invariant of `LinkedChunk`. The chunk it points to has not been
775-
// removed.
776-
.expect("The `first_event_pos` must point to a valid `Item` chunk when inserting events");
773+
.insert_events_at(sync_events, first_event_pos)
774+
// SAFETY: The `first_event_pos` can only be an `Item` chunk, it's
775+
// an invariant of `LinkedChunk`. The chunk it points to has not been
776+
// removed.
777+
.expect("The `first_event_pos` must point to a valid `Item` chunk when inserting events");
777778
}
778779

779780
// There is no first item. Let's simply push.

0 commit comments

Comments
 (0)