Skip to content

Commit 8ff3c1c

Browse files
committed
feat(common): Update::RemoveChunk emits VectorDiff::Remove.
This patch updates `Update::RemoveChunk` to emit `VectorDiff::Remove`. Until now, `RemoveChunk` was expecting the chunk to be empty, because it is how it is used so far. However, with matrix-org#4694, it can change rapidly.
1 parent 5c57631 commit 8ff3c1c

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use std::{
1616
collections::VecDeque,
17+
iter::repeat_n,
1718
ops::{ControlFlow, Not},
1819
sync::{Arc, RwLock},
1920
};
@@ -133,7 +134,8 @@ impl UpdateToVectorDiff {
133134
///
134135
/// * [`Update::NewItemsChunk`] and [`Update::NewGapChunk`] are inserting a
135136
/// new pair with a chunk length of 0 at the appropriate index,
136-
/// * [`Update::RemoveChunk`] is removing a pair,
137+
/// * [`Update::RemoveChunk`] is removing a pair, and is potentially
138+
/// emitting [`VectorDiff`],
137139
/// * [`Update::PushItems`] is increasing the length of the appropriate pair
138140
/// by the number of new items, and is potentially emitting
139141
/// [`VectorDiff`],
@@ -318,22 +320,17 @@ impl UpdateToVectorDiff {
318320
}
319321
}
320322

321-
Update::RemoveChunk(expected_chunk_identifier) => {
322-
let chunk_index = self
323+
Update::RemoveChunk(chunk_identifier) => {
324+
let (offset, (chunk_index, _)) =
325+
self.map_to_offset(&Position(*chunk_identifier, 0));
326+
327+
let (_, number_of_items) = self
323328
.chunks
324-
.iter()
325-
.position(|(chunk_identifier, _)| {
326-
chunk_identifier == expected_chunk_identifier
327-
})
328-
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not buggy, and
329-
// assuming `Self::chunks` is correctly initialized, it is not possible to
330-
// remove a chunk that does not exist. If this predicate fails, it means
331-
// `LinkedChunk` or `ObservableUpdates` contain a bug.
332-
.expect("Removing a chunk: The chunk is not found");
329+
.remove(chunk_index)
330+
.expect("Removing an index out of the bounds");
333331

334-
// It's OK to ignore the result. The `chunk_index` exists because it's been
335-
// found, and we don't care about its associated value.
336-
let _ = self.chunks.remove(chunk_index);
332+
// Removing at the same index because each `Remove` shifts items to the left.
333+
diffs.extend(repeat_n(VectorDiff::Remove { index: offset }, number_of_items));
337334
}
338335

339336
Update::PushItems { at: position, items } => {

0 commit comments

Comments
 (0)