Skip to content

Commit 7996182

Browse files
committed
Auto merge of #74777 - ssomers:btree_cleanup_7, r=Mark-Simulacrum
Stop BTreeMap casts from reborrowing Down in btree/node.rs, the interface and use of `cast_unchecked` look a bit shady. It's really just there for inverting `forget_type` which does not borrow. By borrowing we can't write the same `cast_unchecked` in the same way at the Handle level. No change in undefined behaviour or performance.
2 parents 6b2ca84 + 0522040 commit 7996182

File tree

1 file changed

+16
-20
lines changed
  • library/alloc/src/collections/btree

1 file changed

+16
-20
lines changed

library/alloc/src/collections/btree/node.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
413413
impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
414414
/// Unsafely asserts to the compiler some static information about whether this
415415
/// node is a `Leaf` or an `Internal`.
416-
unsafe fn cast_unchecked<NewType>(&mut self) -> NodeRef<marker::Mut<'_>, K, V, NewType> {
416+
unsafe fn cast_unchecked<NewType>(self) -> NodeRef<marker::Mut<'a>, K, V, NewType> {
417417
NodeRef { height: self.height, node: self.node, root: self.root, _marker: PhantomData }
418418
}
419419

@@ -721,7 +721,7 @@ impl<Node: Copy, Type> Clone for Handle<Node, Type> {
721721
}
722722

723723
impl<Node, Type> Handle<Node, Type> {
724-
/// Retrieves the node that contains the edge of key/value pair this handle points to.
724+
/// Retrieves the node that contains the edge or key/value pair this handle points to.
725725
pub fn into_node(self) -> Node {
726726
self.node
727727
}
@@ -1131,7 +1131,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11311131
}
11321132

11331133
/// Removes the key/value pair pointed to by this handle and returns it, along with the edge
1134-
/// between the now adjacent key/value pairs (if any) to the left and right of this handle.
1134+
/// that the key/value pair collapsed into.
11351135
pub fn remove(
11361136
mut self,
11371137
) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
@@ -1189,18 +1189,17 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
11891189
/// to by this handle, and the node immediately to the right of this handle into one new
11901190
/// child of the underlying node, returning an edge referencing that new child.
11911191
///
1192-
/// Assumes that this edge `.can_merge()`.
1192+
/// Panics unless this edge `.can_merge()`.
11931193
pub fn merge(
11941194
mut self,
11951195
) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
11961196
let self1 = unsafe { ptr::read(&self) };
11971197
let self2 = unsafe { ptr::read(&self) };
11981198
let mut left_node = self1.left_edge().descend();
11991199
let left_len = left_node.len();
1200-
let mut right_node = self2.right_edge().descend();
1200+
let right_node = self2.right_edge().descend();
12011201
let right_len = right_node.len();
12021202

1203-
// necessary for correctness, but in a private module
12041203
assert!(left_len + right_len < CAPACITY);
12051204

12061205
unsafe {
@@ -1231,28 +1230,25 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12311230

12321231
(*left_node.as_leaf_mut()).len += right_len as u16 + 1;
12331232

1234-
let layout = if self.node.height > 1 {
1233+
if self.node.height > 1 {
1234+
// SAFETY: the height of the nodes being merged is one below the height
1235+
// of the node of this edge, thus above zero, so they are internal.
1236+
let mut left_node = left_node.cast_unchecked();
1237+
let right_node = right_node.cast_unchecked();
12351238
ptr::copy_nonoverlapping(
1236-
right_node.cast_unchecked().as_internal().edges.as_ptr(),
1237-
left_node
1238-
.cast_unchecked()
1239-
.as_internal_mut()
1240-
.edges
1241-
.as_mut_ptr()
1242-
.add(left_len + 1),
1239+
right_node.reborrow().as_internal().edges.as_ptr(),
1240+
left_node.reborrow_mut().as_internal_mut().edges.as_mut_ptr().add(left_len + 1),
12431241
right_len + 1,
12441242
);
12451243

12461244
for i in left_len + 1..left_len + right_len + 2 {
1247-
Handle::new_edge(left_node.cast_unchecked().reborrow_mut(), i)
1248-
.correct_parent_link();
1245+
Handle::new_edge(left_node.reborrow_mut(), i).correct_parent_link();
12491246
}
12501247

1251-
Layout::new::<InternalNode<K, V>>()
1248+
Global.dealloc(right_node.node.cast(), Layout::new::<InternalNode<K, V>>());
12521249
} else {
1253-
Layout::new::<LeafNode<K, V>>()
1254-
};
1255-
Global.dealloc(right_node.node.cast(), layout);
1250+
Global.dealloc(right_node.node.cast(), Layout::new::<LeafNode<K, V>>());
1251+
}
12561252

12571253
Handle::new_edge(self.node, self.idx)
12581254
}

0 commit comments

Comments
 (0)