Skip to content

Commit 4ace25d

Browse files
Rollup merge of rust-lang#79354 - ssomers:btree_bereave_BoxedNode, r=Mark-Simulacrum
BTreeMap: cut out the ceremony around BoxedNode The opposite direction of rust-lang#79093. r? ``@Mark-Simulacrum``
2 parents 1e9ad4f + c52a5ec commit 4ace25d

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

alloc/src/collections/btree/node.rs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,8 @@ impl<K, V> InternalNode<K, V> {
112112
///
113113
/// However, `BoxedNode` contains no information as to which of the two types
114114
/// of nodes it actually contains, and, partially due to this lack of information,
115-
/// has no destructor.
116-
struct BoxedNode<K, V> {
117-
ptr: NonNull<LeafNode<K, V>>,
118-
}
119-
120-
impl<K, V> BoxedNode<K, V> {
121-
fn from_owned(ptr: NonNull<LeafNode<K, V>>) -> Self {
122-
BoxedNode { ptr }
123-
}
124-
125-
fn as_ptr(&self) -> NonNull<LeafNode<K, V>> {
126-
self.ptr
127-
}
128-
}
115+
/// is not a separate type and has no destructor.
116+
type BoxedNode<K, V> = NonNull<LeafNode<K, V>>;
129117

130118
/// An owned tree.
131119
///
@@ -168,11 +156,6 @@ impl<K, V, Type> NodeRef<marker::Owned, K, V, Type> {
168156
pub fn borrow_valmut(&mut self) -> NodeRef<marker::ValMut<'_>, K, V, Type> {
169157
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
170158
}
171-
172-
/// Packs the reference, aware of type and height, into a type-agnostic pointer.
173-
fn into_boxed_node(self) -> BoxedNode<K, V> {
174-
BoxedNode::from_owned(self.node)
175-
}
176159
}
177160

178161
impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
@@ -181,7 +164,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
181164
/// and is the opposite of `pop_internal_level`.
182165
pub fn push_internal_level(&mut self) -> NodeRef<marker::Mut<'_>, K, V, marker::Internal> {
183166
let mut new_node = Box::new(unsafe { InternalNode::new() });
184-
new_node.edges[0].write(BoxedNode::from_owned(self.node));
167+
new_node.edges[0].write(self.node);
185168
let mut new_root = NodeRef::from_new_internal(new_node, self.height + 1);
186169
new_root.borrow_mut().first_edge().correct_parent_link();
187170
*self = new_root.forget_type();
@@ -288,13 +271,6 @@ unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send for NodeRef<marker::Mut<'
288271
unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send for NodeRef<marker::ValMut<'a>, K, V, Type> {}
289272
unsafe impl<K: Send, V: Send, Type> Send for NodeRef<marker::Owned, K, V, Type> {}
290273

291-
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
292-
/// Unpack a node reference that was packed by `Root::into_boxed_node`.
293-
fn from_boxed_node(boxed_node: BoxedNode<K, V>, height: usize) -> Self {
294-
NodeRef { height, node: boxed_node.as_ptr(), _marker: PhantomData }
295-
}
296-
}
297-
298274
impl<BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Internal> {
299275
/// Unpack a node reference that was packed as `NodeRef::parent`.
300276
fn from_internal(node: NonNull<InternalNode<K, V>>, height: usize) -> Self {
@@ -695,7 +671,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
695671
unsafe {
696672
self.reborrow_mut().into_key_area_mut_at(idx).write(key);
697673
self.reborrow_mut().into_val_area_mut_at(idx).write(val);
698-
self.reborrow_mut().into_edge_area_mut_at(idx + 1).write(edge.into_boxed_node());
674+
self.reborrow_mut().into_edge_area_mut_at(idx + 1).write(edge.node);
699675
Handle::new_edge(self.reborrow_mut(), idx + 1).correct_parent_link();
700676
}
701677
}
@@ -710,7 +686,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
710686
*self.reborrow_mut().into_len_mut() += 1;
711687
slice_insert(self.reborrow_mut().into_key_area_slice(), 0, key);
712688
slice_insert(self.reborrow_mut().into_val_area_slice(), 0, val);
713-
slice_insert(self.reborrow_mut().into_edge_area_slice(), 0, edge.into_boxed_node());
689+
slice_insert(self.reborrow_mut().into_edge_area_slice(), 0, edge.node);
714690
}
715691

716692
self.correct_all_childrens_parent_links();
@@ -732,8 +708,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
732708
let edge = match self.reborrow_mut().force() {
733709
ForceResult::Leaf(_) => None,
734710
ForceResult::Internal(internal) => {
735-
let boxed_node = ptr::read(internal.reborrow().edge_at(idx + 1));
736-
let mut edge = Root::from_boxed_node(boxed_node, internal.height - 1);
711+
let node = ptr::read(internal.reborrow().edge_at(idx + 1));
712+
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
737713
// In practice, clearing the parent is a waste of time, because we will
738714
// insert the node elsewhere and set its parent link again.
739715
edge.borrow_mut().clear_parent_link();
@@ -760,9 +736,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
760736
let edge = match self.reborrow_mut().force() {
761737
ForceResult::Leaf(_) => None,
762738
ForceResult::Internal(mut internal) => {
763-
let boxed_node =
764-
slice_remove(internal.reborrow_mut().into_edge_area_slice(), 0);
765-
let mut edge = Root::from_boxed_node(boxed_node, internal.height - 1);
739+
let node = slice_remove(internal.reborrow_mut().into_edge_area_slice(), 0);
740+
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
766741
// In practice, clearing the parent is a waste of time, because we will
767742
// insert the node elsewhere and set its parent link again.
768743
edge.borrow_mut().clear_parent_link();
@@ -1041,12 +1016,11 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
10411016
debug_assert!(self.node.len() < CAPACITY);
10421017
debug_assert!(edge.height == self.node.height - 1);
10431018

1044-
let boxed_node = edge.into_boxed_node();
10451019
unsafe {
10461020
*self.node.reborrow_mut().into_len_mut() += 1;
10471021
slice_insert(self.node.reborrow_mut().into_key_area_slice(), self.idx, key);
10481022
slice_insert(self.node.reborrow_mut().into_val_area_slice(), self.idx, val);
1049-
slice_insert(self.node.reborrow_mut().into_edge_area_slice(), self.idx + 1, boxed_node);
1023+
slice_insert(self.node.reborrow_mut().into_edge_area_slice(), self.idx + 1, edge.node);
10501024

10511025
self.node.correct_childrens_parent_links((self.idx + 1)..=self.node.len());
10521026
}
@@ -1135,8 +1109,8 @@ impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V, marker::Internal>, marke
11351109
// reference (Rust issue #73987) and invalidate any other references
11361110
// to or inside the array, should any be around.
11371111
let parent_ptr = NodeRef::as_internal_ptr(&self.node);
1138-
let boxed_node = unsafe { (*parent_ptr).edges.get_unchecked(self.idx).assume_init_read() };
1139-
NodeRef::from_boxed_node(boxed_node, self.node.height - 1)
1112+
let node = unsafe { (*parent_ptr).edges.get_unchecked(self.idx).assume_init_read() };
1113+
NodeRef { node, height: self.node.height - 1, _marker: PhantomData }
11401114
}
11411115
}
11421116

0 commit comments

Comments
 (0)