Rollup merge of #120505 - Amanieu:fix-btreemap-cursor-remove, r=m-ou-se

Fix BTreeMap's Cursor::remove_{next,prev}

These would incorrectly leave `current` as `None` after a failed attempt to remove an element (due to the cursor already being at the start/end).
This commit is contained in:
Guillaume Gomez 2024-02-15 14:33:00 +01:00 committed by GitHub
commit 472c820eb3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3247,9 +3247,15 @@ pub fn insert_before(&mut self, key: K, value: V) -> Result<(), UnorderedKeyErro
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn remove_next(&mut self) -> Option<(K, V)> {
let current = self.current.take()?;
if current.reborrow().next_kv().is_err() {
self.current = Some(current);
return None;
}
let mut emptied_internal_root = false;
let (kv, pos) = current
.next_kv()
// This should be unwrap(), but that doesn't work because NodeRef
// doesn't implement Debug. The condition is checked above.
.ok()?
.remove_kv_tracking(|| emptied_internal_root = true, self.alloc.clone());
self.current = Some(pos);
@ -3270,9 +3276,15 @@ pub fn remove_next(&mut self) -> Option<(K, V)> {
#[unstable(feature = "btree_cursors", issue = "107540")]
pub fn remove_prev(&mut self) -> Option<(K, V)> {
let current = self.current.take()?;
if current.reborrow().next_back_kv().is_err() {
self.current = Some(current);
return None;
}
let mut emptied_internal_root = false;
let (kv, pos) = current
.next_back_kv()
// This should be unwrap(), but that doesn't work because NodeRef
// doesn't implement Debug. The condition is checked above.
.ok()?
.remove_kv_tracking(|| emptied_internal_root = true, self.alloc.clone());
self.current = Some(pos);