Skip to content

Commit 7feab00

Browse files
committed
Auto merge of rust-lang#80005 - ssomers:btree_cleanup_3, r=Mark-Simulacrum
BTreeMap: declare clear_parent_link directly on the root it needs r? `@Mark-Simulacrum`
2 parents 803c602 + bdc6adf commit 7feab00

File tree

1 file changed

+11
-10
lines changed
  • library/alloc/src/collections/btree

1 file changed

+11
-10
lines changed

Diff for: library/alloc/src/collections/btree/node.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<K, V, Type> NodeRef<marker::Owned, K, V, Type> {
150150
/// Mutably borrows the owned node. Unlike `reborrow_mut`, this is safe,
151151
/// because the return value cannot be used to destroy the node itself,
152152
/// and there cannot be other references to the tree (except during the
153-
/// process of `into_iter` or `drop`, but that is a horrific already).
153+
/// process of `into_iter` or `drop`, but that is horrific already).
154154
pub fn borrow_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, Type> {
155155
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
156156
}
@@ -192,7 +192,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
192192

193193
let internal_node = NodeRef { height: self.height, node: top, _marker: PhantomData };
194194
*self = internal_node.first_edge().descend();
195-
self.borrow_mut().clear_parent_link();
195+
self.clear_parent_link();
196196

197197
unsafe {
198198
Global.deallocate(top.cast(), Layout::new::<InternalNode<K, V>>());
@@ -611,18 +611,19 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
611611
}
612612

613613
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
614-
/// Set or clear the node's link to its parent edge,
614+
/// Sets the node's link to its parent edge,
615615
/// without invalidating other references to the node.
616616
fn set_parent_link(&mut self, parent: NonNull<InternalNode<K, V>>, parent_idx: usize) {
617617
let leaf = Self::as_leaf_ptr(self);
618618
unsafe { (*leaf).parent = Some(parent) };
619619
unsafe { (*leaf).parent_idx.write(parent_idx as u16) };
620620
}
621+
}
621622

622-
/// Clear the node's link to its parent edge.
623-
/// This only makes sense when there are no other references to the node.
623+
impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
624+
/// Clears the root's link to its parent edge.
624625
fn clear_parent_link(&mut self) {
625-
let leaf = Self::as_leaf_mut(self);
626+
let leaf = NodeRef::as_leaf_mut(&mut self.borrow_mut());
626627
leaf.parent = None;
627628
}
628629
}
@@ -720,9 +721,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
720721
ForceResult::Internal(internal) => {
721722
let node = ptr::read(internal.reborrow().edge_at(idx + 1));
722723
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
723-
// In practice, clearing the parent is a waste of time, because we will
724+
// Currently, clearing the parent link is superfluous, because we will
724725
// insert the node elsewhere and set its parent link again.
725-
edge.borrow_mut().clear_parent_link();
726+
edge.clear_parent_link();
726727
Some(edge)
727728
}
728729
};
@@ -748,9 +749,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
748749
ForceResult::Internal(mut internal) => {
749750
let node = slice_remove(internal.reborrow_mut().into_edge_area_slice(), 0);
750751
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
751-
// In practice, clearing the parent is a waste of time, because we will
752+
// Currently, clearing the parent link is superfluous, because we will
752753
// insert the node elsewhere and set its parent link again.
753-
edge.borrow_mut().clear_parent_link();
754+
edge.clear_parent_link();
754755

755756
internal.correct_childrens_parent_links(0..old_len);
756757

0 commit comments

Comments
 (0)