Skip to content

Commit ad35895

Browse files
committed
refactor(linked chunk): simplify remove_item_at
1 parent 0ee89c8 commit ad35895

File tree

1 file changed

+15
-23
lines changed
  • crates/matrix-sdk-common/src/linked_chunk

1 file changed

+15
-23
lines changed

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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl<const CAP: usize, Item, Gap> Ends<CAP, Item, Gap> {
294294
// Fetch the previous chunk pointer.
295295
let previous_ptr = unsafe { chunk_ptr.as_ref() }.previous;
296296

297-
// Re-box the chunk, and let Rust does its job.
297+
// Re-box the chunk, and let Rust do its job.
298298
let _chunk_boxed = unsafe { Box::from_raw(chunk_ptr.as_ptr()) };
299299

300300
// Update the `current_chunk_ptr`.
@@ -569,32 +569,24 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
569569
.chunk_mut(chunk_identifier)
570570
.ok_or(Error::InvalidChunkIdentifier { identifier: chunk_identifier })?;
571571

572-
let can_unlink_chunk = match &mut chunk.content {
572+
let current_items = match &mut chunk.content {
573573
ChunkContent::Gap(..) => {
574574
return Err(Error::ChunkIsAGap { identifier: chunk_identifier });
575575
}
576+
ChunkContent::Items(current_items) => current_items,
577+
};
576578

577-
ChunkContent::Items(current_items) => {
578-
let current_items_length = current_items.len();
579-
580-
if item_index > current_items_length {
581-
return Err(Error::InvalidItemIndex { index: item_index });
582-
}
583-
584-
removed_item = current_items.remove(item_index);
585-
586-
if let Some(updates) = self.updates.as_mut() {
587-
updates
588-
.push(Update::RemoveItem { at: Position(chunk_identifier, item_index) })
589-
}
579+
if item_index > current_items.len() {
580+
return Err(Error::InvalidItemIndex { index: item_index });
581+
}
590582

591-
current_items.is_empty()
592-
}
593-
};
583+
removed_item = current_items.remove(item_index);
584+
if let Some(updates) = self.updates.as_mut() {
585+
updates.push(Update::RemoveItem { at: Position(chunk_identifier, item_index) })
586+
}
594587

595-
// If removing empty chunk is desired, and if the `chunk` can be unlinked, and
596-
// if the `chunk` is not the first one, we can remove it.
597-
if can_unlink_chunk && !chunk.is_first_chunk() {
588+
// If the chunk is empty and not the first one, we can remove it.
589+
if current_items.is_empty() && !chunk.is_first_chunk() {
598590
// Unlink `chunk`.
599591
chunk.unlink(self.updates.as_mut());
600592

@@ -613,7 +605,7 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
613605
if let Some(chunk_ptr) = chunk_ptr {
614606
// `chunk` has been unlinked.
615607

616-
// Re-box the chunk, and let Rust does its job.
608+
// Re-box the chunk, and let Rust do its job.
617609
//
618610
// SAFETY: `chunk` is unlinked and not borrowed anymore. `LinkedChunk` doesn't
619611
// use it anymore, it's a leak. It is time to re-`Box` it and drop it.
@@ -905,7 +897,7 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> {
905897
// Stop borrowing `chunk`.
906898
}
907899

908-
// Re-box the chunk, and let Rust does its job.
900+
// Re-box the chunk, and let Rust do its job.
909901
//
910902
// SAFETY: `chunk` is unlinked and not borrowed anymore. `LinkedChunk` doesn't
911903
// use it anymore, it's a leak. It is time to re-`Box` it and drop it.

0 commit comments

Comments
 (0)