-
Notifications
You must be signed in to change notification settings - Fork 269
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(event cache): redact previously-seen events in the linked chunk too #4536
Changes from all commits
50a8bbf
201f43f
de71c77
17db2fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -528,6 +528,47 @@ impl<const CAP: usize, Item, Gap> LinkedChunk<CAP, Item, Gap> { | |
Ok(removed_item) | ||
} | ||
|
||
/// Replace item at a specified position in the [`LinkedChunk`]. | ||
/// | ||
/// `position` must point to a valid item, otherwise the method returns | ||
/// `Err`. | ||
pub fn replace_item_at(&mut self, position: Position, item: Item) -> Result<(), Error> | ||
where | ||
Item: Clone, | ||
{ | ||
let chunk_identifier = position.chunk_identifier(); | ||
let item_index = position.index(); | ||
|
||
let chunk = self | ||
.links | ||
.chunk_mut(chunk_identifier) | ||
.ok_or(Error::InvalidChunkIdentifier { identifier: chunk_identifier })?; | ||
|
||
match &mut chunk.content { | ||
ChunkContent::Gap(..) => { | ||
return Err(Error::ChunkIsAGap { identifier: chunk_identifier }) | ||
} | ||
|
||
ChunkContent::Items(current_items) => { | ||
if item_index >= current_items.len() { | ||
return Err(Error::InvalidItemIndex { index: item_index }); | ||
} | ||
|
||
// Avoid one spurious clone by notifying about the update *before* applying it. | ||
if let Some(updates) = self.updates.as_mut() { | ||
updates.push(Update::ReplaceItem { | ||
at: Position(chunk_identifier, item_index), | ||
item: item.clone(), | ||
}); | ||
} | ||
Comment on lines
+558
to
+563
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Push the update after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means we have to clone before, and the original thing is unused if |
||
|
||
current_items[item_index] = item; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Insert a gap at a specified position in the [`LinkedChunk`]. | ||
/// | ||
/// Because the `position` can be invalid, this method returns a | ||
|
@@ -2919,4 +2960,30 @@ mod tests { | |
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_replace_item() { | ||
let mut linked_chunk = LinkedChunk::<3, char, ()>::new(); | ||
|
||
linked_chunk.push_items_back(['a', 'b', 'c']); | ||
linked_chunk.push_gap_back(()); | ||
// Sanity check. | ||
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] [-]); | ||
|
||
// Replace item in bounds. | ||
linked_chunk.replace_item_at(Position(ChunkIdentifier::new(0), 1), 'B').unwrap(); | ||
assert_items_eq!(linked_chunk, ['a', 'B', 'c'] [-]); | ||
|
||
// Attempt to replace out-of-bounds. | ||
assert_matches!( | ||
linked_chunk.replace_item_at(Position(ChunkIdentifier::new(0), 3), 'Z'), | ||
Err(Error::InvalidItemIndex { index: 3 }) | ||
); | ||
|
||
// Attempt to replace gap. | ||
assert_matches!( | ||
linked_chunk.replace_item_at(Position(ChunkIdentifier::new(1), 0), 'Z'), | ||
Err(Error::ChunkIsAGap { .. }) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍