@@ -153,10 +153,15 @@ impl<K, V> InternalNode<K, V> {
153
153
}
154
154
}
155
155
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.
160
165
struct BoxedNode < K , V > {
161
166
ptr : Unique < LeafNode < K , V > > ,
162
167
}
@@ -167,9 +172,7 @@ impl<K, V> BoxedNode<K, V> {
167
172
}
168
173
169
174
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 ( ) }
173
176
}
174
177
175
178
unsafe fn from_ptr ( ptr : NonNull < LeafNode < K , V > > ) -> Self {
@@ -181,32 +184,33 @@ impl<K, V> BoxedNode<K, V> {
181
184
}
182
185
}
183
186
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 .
186
189
pub struct Root < K , V > {
187
190
node : BoxedNode < K , V > ,
191
+ /// The number of levels below the root node.
188
192
height : usize ,
189
193
}
190
194
191
195
unsafe impl < K : Sync , V : Sync > Sync for Root < K , V > { }
192
196
unsafe impl < K : Send , V : Send > Send for Root < K , V > { }
193
197
194
198
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.
195
201
pub fn is_shared_root ( & self ) -> bool {
196
202
self . as_ref ( ) . is_shared_root ( )
197
203
}
198
204
205
+ /// Returns a shared tree, wrapping a shared root node that is eternally empty.
199
206
pub fn shared_empty_root ( ) -> Self {
200
207
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 ( ) ) } ,
206
209
height : 0 ,
207
210
}
208
211
}
209
212
213
+ /// Returns a new owned tree, with its own root node that is initially empty.
210
214
pub fn new_leaf ( ) -> Self {
211
215
Root { node : BoxedNode :: from_leaf ( Box :: new ( unsafe { LeafNode :: new ( ) } ) ) , height : 0 }
212
216
}
@@ -310,6 +314,7 @@ impl<K, V> Root<K, V> {
310
314
/// so '&LeafNode` or `&InternalNode` pointing to the shared root is undefined behavior.
311
315
/// Turning this into a `NodeHeader` reference is always safe.
312
316
pub struct NodeRef < BorrowType , K , V , Type > {
317
+ /// The number of levels below the node.
313
318
height : usize ,
314
319
node : NonNull < LeafNode < K , V > > ,
315
320
// `root` is null unless the borrow type is `Mut`
0 commit comments