Skip to content

Commit c13d296

Browse files
authored
Rollup merge of #69668 - ssomers:btreemap_even_more_comments, r=Mark-Simulacrum
More documentation and simplification of BTreeMap's internals Salvage the documentation and simplification from #67980, without changing the type locked down by debuginfo. r? @rkruppe
2 parents 8ce45d8 + 9384cba commit c13d296

File tree

1 file changed

+19
-14
lines changed
  • src/liballoc/collections/btree

1 file changed

+19
-14
lines changed

src/liballoc/collections/btree/node.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,15 @@ impl<K, V> InternalNode<K, V> {
153153
}
154154
}
155155

156-
/// An owned pointer to a node. This basically is either `Box<LeafNode<K, V>>` or
157-
/// `Box<InternalNode<K, V>>`. However, it contains no information as to which of the two types
158-
/// of nodes is actually behind the box, and, partially due to this lack of information, has no
159-
/// destructor.
156+
/// A managed, non-null pointer to a node. This is either an owned pointer to
157+
/// `LeafNode<K, V>`, an owned pointer to `InternalNode<K, V>`, or a (not owned)
158+
/// pointer to `NodeHeader<(), ()` (more specifically, the pointer to EMPTY_ROOT_NODE).
159+
/// All of these types have a `NodeHeader<K, V>` prefix, meaning that they have at
160+
/// least the same size as `NodeHeader<K, V>` and store the same kinds of data at the same
161+
/// offsets; and they have a pointer alignment at least as large as `NodeHeader<K, V>`'s.
162+
/// However, `BoxedNode` contains no information as to which of the three types
163+
/// of nodes it actually contains, and, partially due to this lack of information,
164+
/// has no destructor.
160165
struct BoxedNode<K, V> {
161166
ptr: Unique<LeafNode<K, V>>,
162167
}
@@ -167,9 +172,7 @@ impl<K, V> BoxedNode<K, V> {
167172
}
168173

169174
fn from_internal(node: Box<InternalNode<K, V>>) -> Self {
170-
unsafe {
171-
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node) as *mut LeafNode<K, V>) }
172-
}
175+
BoxedNode { ptr: Box::into_unique(node).cast() }
173176
}
174177

175178
unsafe fn from_ptr(ptr: NonNull<LeafNode<K, V>>) -> Self {
@@ -181,32 +184,33 @@ impl<K, V> BoxedNode<K, V> {
181184
}
182185
}
183186

184-
/// An owned tree. Note that despite being owned, this does not have a destructor,
185-
/// and must be cleaned up manually.
187+
/// Either an owned tree or a shared, empty tree. Note that this does not have a destructor,
188+
/// and must be cleaned up manually if it is an owned tree.
186189
pub struct Root<K, V> {
187190
node: BoxedNode<K, V>,
191+
/// The number of levels below the root node.
188192
height: usize,
189193
}
190194

191195
unsafe impl<K: Sync, V: Sync> Sync for Root<K, V> {}
192196
unsafe impl<K: Send, V: Send> Send for Root<K, V> {}
193197

194198
impl<K, V> Root<K, V> {
199+
/// Whether the instance of `Root` wraps a shared, empty root node. If not,
200+
/// the entire tree is uniquely owned by the owner of the `Root` instance.
195201
pub fn is_shared_root(&self) -> bool {
196202
self.as_ref().is_shared_root()
197203
}
198204

205+
/// Returns a shared tree, wrapping a shared root node that is eternally empty.
199206
pub fn shared_empty_root() -> Self {
200207
Root {
201-
node: unsafe {
202-
BoxedNode::from_ptr(NonNull::new_unchecked(
203-
&EMPTY_ROOT_NODE as *const _ as *const LeafNode<K, V> as *mut _,
204-
))
205-
},
208+
node: unsafe { BoxedNode::from_ptr(NonNull::from(&EMPTY_ROOT_NODE).cast()) },
206209
height: 0,
207210
}
208211
}
209212

213+
/// Returns a new owned tree, with its own root node that is initially empty.
210214
pub fn new_leaf() -> Self {
211215
Root { node: BoxedNode::from_leaf(Box::new(unsafe { LeafNode::new() })), height: 0 }
212216
}
@@ -310,6 +314,7 @@ impl<K, V> Root<K, V> {
310314
/// so '&LeafNode` or `&InternalNode` pointing to the shared root is undefined behavior.
311315
/// Turning this into a `NodeHeader` reference is always safe.
312316
pub struct NodeRef<BorrowType, K, V, Type> {
317+
/// The number of levels below the node.
313318
height: usize,
314319
node: NonNull<LeafNode<K, V>>,
315320
// `root` is null unless the borrow type is `Mut`

0 commit comments

Comments
 (0)