Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): Add LinkedChunk::remove_item_at #4173

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(sdk): Extract code into a map_to_offset method.
This is only code move, nothing has changed.
Hywan committed Oct 28, 2024

Verified

This commit was signed with the committer’s verified signature.
Hywan Ivan Enderlin
commit 6a8a1db523a877673c499e853f2f70b60e118df2
71 changes: 40 additions & 31 deletions crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ use eyeball_im::VectorDiff;

use super::{
updates::{ReaderToken, Update, UpdatesInner},
ChunkContent, ChunkIdentifier, Iter,
ChunkContent, ChunkIdentifier, Iter, Position,
};

/// A type alias to represent a chunk's length. This is purely for commodity.
@@ -329,37 +329,9 @@ impl UpdateToVectorDiff {
}

Update::PushItems { at: position, items } => {
let expected_chunk_identifier = position.chunk_identifier();

let (chunk_index, offset, chunk_length) = {
let control_flow = self.chunks.iter_mut().enumerate().try_fold(
position.index(),
|offset, (chunk_index, (chunk_identifier, chunk_length))| {
if chunk_identifier == &expected_chunk_identifier {
ControlFlow::Break((chunk_index, offset, chunk_length))
} else {
ControlFlow::Continue(offset + *chunk_length)
}
},
);

match control_flow {
// Chunk has been found, and all values have been calculated as
// expected.
ControlFlow::Break(values) => values,

// Chunk has not been found.
ControlFlow::Continue(..) => {
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not
// buggy, and assuming `Self::chunks` is correctly initialized, it
// is not possible to push items on a chunk that does not exist. If
// this predicate fails, it means `LinkedChunk` or
// `ObservableUpdates` contain a bug.
panic!("Pushing items: The chunk is not found");
}
}
};
let (offset, (chunk_index, chunk_length)) = self.map_to_offset(position);

// Update the length of the chunk in `self.chunks`.
*chunk_length += items.len();

// See `mute_push_items` to learn more.
@@ -416,6 +388,43 @@ impl UpdateToVectorDiff {

diffs
}

fn map_to_offset(&mut self, position: &Position) -> (usize, (usize, &mut usize)) {
let expected_chunk_identifier = position.chunk_identifier();

let (offset, (chunk_index, chunk_length)) = {
let control_flow = self.chunks.iter_mut().enumerate().try_fold(
position.index(),
|offset, (chunk_index, (chunk_identifier, chunk_length))| {
if chunk_identifier == &expected_chunk_identifier {
ControlFlow::Break((offset, (chunk_index, chunk_length)))
} else {
ControlFlow::Continue(offset + *chunk_length)
}
},
);

match control_flow {
// Chunk has been found, and all values have been calculated as
// expected.
ControlFlow::Break(values) => values,

// Chunk has not been found.
ControlFlow::Continue(..) => {
// SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are
// not buggy, and assuming
// `Self::chunks` is correctly initialized, it
// is not possible to push items on a chunk that does not exist.
// If this predicate fails,
// it means `LinkedChunk` or
// `ObservableUpdates` contain a bug.
panic!("Pushing items: The chunk is not found");
}
}
};

(offset, (chunk_index, chunk_length))
}
}

#[cfg(test)]