Skip to content

Commit 0522040

Browse files
committed
Stop BTreeMap casts from reborrowing
1 parent 441fd22 commit 0522040

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
}
@@ -1082,7 +1082,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
10821082
}
10831083

10841084
/// Removes the key/value pair pointed to by this handle and returns it, along with the edge
1085-
/// between the now adjacent key/value pairs (if any) to the left and right of this handle.
1085+
/// that the key/value pair collapsed into.
10861086
pub fn remove(
10871087
mut self,
10881088
) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
@@ -1140,18 +1140,17 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
11401140
/// to by this handle, and the node immediately to the right of this handle into one new
11411141
/// child of the underlying node, returning an edge referencing that new child.
11421142
///
1143-
/// Assumes that this edge `.can_merge()`.
1143+
/// Panics unless this edge `.can_merge()`.
11441144
pub fn merge(
11451145
mut self,
11461146
) -> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::Edge> {
11471147
let self1 = unsafe { ptr::read(&self) };
11481148
let self2 = unsafe { ptr::read(&self) };
11491149
let mut left_node = self1.left_edge().descend();
11501150
let left_len = left_node.len();
1151-
let mut right_node = self2.right_edge().descend();
1151+
let right_node = self2.right_edge().descend();
11521152
let right_len = right_node.len();
11531153

1154-
// necessary for correctness, but in a private module
11551154
assert!(left_len + right_len < CAPACITY);
11561155

11571156
unsafe {
@@ -1182,28 +1181,25 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
11821181

11831182
(*left_node.as_leaf_mut()).len += right_len as u16 + 1;
11841183

1185-
let layout = if self.node.height > 1 {
1184+
if self.node.height > 1 {
1185+
// SAFETY: the height of the nodes being merged is one below the height
1186+
// of the node of this edge, thus above zero, so they are internal.
1187+
let mut left_node = left_node.cast_unchecked();
1188+
let right_node = right_node.cast_unchecked();
11861189
ptr::copy_nonoverlapping(
1187-
right_node.cast_unchecked().as_internal().edges.as_ptr(),
1188-
left_node
1189-
.cast_unchecked()
1190-
.as_internal_mut()
1191-
.edges
1192-
.as_mut_ptr()
1193-
.add(left_len + 1),
1190+
right_node.reborrow().as_internal().edges.as_ptr(),
1191+
left_node.reborrow_mut().as_internal_mut().edges.as_mut_ptr().add(left_len + 1),
11941192
right_len + 1,
11951193
);
11961194

11971195
for i in left_len + 1..left_len + right_len + 2 {
1198-
Handle::new_edge(left_node.cast_unchecked().reborrow_mut(), i)
1199-
.correct_parent_link();
1196+
Handle::new_edge(left_node.reborrow_mut(), i).correct_parent_link();
12001197
}
12011198

1202-
Layout::new::<InternalNode<K, V>>()
1199+
Global.dealloc(right_node.node.cast(), Layout::new::<InternalNode<K, V>>());
12031200
} else {
1204-
Layout::new::<LeafNode<K, V>>()
1205-
};
1206-
Global.dealloc(right_node.node.cast(), layout);
1201+
Global.dealloc(right_node.node.cast(), Layout::new::<LeafNode<K, V>>());
1202+
}
12071203

12081204
Handle::new_edge(self.node, self.idx)
12091205
}

0 commit comments

Comments
 (0)