@@ -308,15 +308,15 @@ impl<K, V> Root<K, V> {
308308/// `Leaf`, the `NodeRef` points to a leaf node, when this is `Internal` the
309309/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
310310/// `NodeRef` could be pointing to either type of node.
311- /// Note that in case of a leaf node, this might still be the shared root! Only turn
312- /// this into a `LeafNode` reference if you know it is not a root! Shared references
313- /// must be dereferencable *for the entire size of their pointee*, so `&InternalNode`
314- /// pointing to the shared root is UB .
315- /// Turning this into a `NodeHeader` is always safe.
311+ /// Note that in case of a leaf node, this might still be the shared root!
312+ /// Only turn this into a `LeafNode` reference if you know it is not the shared root!
313+ /// Shared references must be dereferencable *for the entire size of their pointee*,
314+ /// so '&LeafNode` or `&InternalNode` pointing to the shared root is undefined behavior .
315+ /// Turning this into a `NodeHeader` reference is always safe.
316316pub struct NodeRef < BorrowType , K , V , Type > {
317317 height : usize ,
318318 node : NonNull < LeafNode < K , V > > ,
319- // This is null unless the borrow type is `Mut`
319+ // `root` is null unless the borrow type is `Mut`
320320 root : * const Root < K , V > ,
321321 _marker : PhantomData < ( BorrowType , Type ) > ,
322322}
@@ -370,23 +370,33 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
370370 NodeRef { height : self . height , node : self . node , root : self . root , _marker : PhantomData }
371371 }
372372
373- /// Assert that this is indeed a proper leaf node, and not the shared root.
373+ /// Exposes the leaf "portion" of any leaf or internal node that is not the shared root.
374+ /// If the node is a leaf, this function simply opens up its data.
375+ /// If the node is an internal node, so not a leaf, it does have all the data a leaf has
376+ /// (header, keys and values), and this function exposes that.
377+ /// See `NodeRef` on why the node may not be a shared root.
374378 unsafe fn as_leaf ( & self ) -> & LeafNode < K , V > {
379+ debug_assert ! ( !self . is_shared_root( ) ) ;
375380 self . node . as_ref ( )
376381 }
377382
378383 fn as_header ( & self ) -> & NodeHeader < K , V > {
379384 unsafe { & * ( self . node . as_ptr ( ) as * const NodeHeader < K , V > ) }
380385 }
381386
387+ /// Returns whether the node is the shared, empty root.
382388 pub fn is_shared_root ( & self ) -> bool {
383389 self . as_header ( ) . is_shared_root ( )
384390 }
385391
392+ /// Borrows a view into the keys stored in the node.
393+ /// Works on all possible nodes, including the shared root.
386394 pub fn keys ( & self ) -> & [ K ] {
387395 self . reborrow ( ) . into_key_slice ( )
388396 }
389397
398+ /// Borrows a view into the values stored in the node.
399+ /// The caller must ensure that the node is not the shared root.
390400 fn vals ( & self ) -> & [ V ] {
391401 self . reborrow ( ) . into_val_slice ( )
392402 }
@@ -491,16 +501,24 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
491501 NodeRef { height : self . height , node : self . node , root : self . root , _marker : PhantomData }
492502 }
493503
504+ /// Exposes the leaf "portion" of any leaf or internal node for writing.
505+ /// If the node is a leaf, this function simply opens up its data.
506+ /// If the node is an internal node, so not a leaf, it does have all the data a leaf has
507+ /// (header, keys and values), and this function exposes that.
508+ ///
494509 /// Returns a raw ptr to avoid asserting exclusive access to the entire node.
510+ /// This also implies you can invoke this member on the shared root, but the resulting pointer
511+ /// might not be properly aligned and definitely would not allow accessing keys and values.
495512 fn as_leaf_mut ( & mut self ) -> * mut LeafNode < K , V > {
496- // We are mutable, so we cannot be the shared root, so accessing this as a leaf is okay.
497513 self . node . as_ptr ( )
498514 }
499515
516+ /// The caller must ensure that the node is not the shared root.
500517 fn keys_mut ( & mut self ) -> & mut [ K ] {
501518 unsafe { self . reborrow_mut ( ) . into_key_slice_mut ( ) }
502519 }
503520
521+ /// The caller must ensure that the node is not the shared root.
504522 fn vals_mut ( & mut self ) -> & mut [ V ] {
505523 unsafe { self . reborrow_mut ( ) . into_val_slice_mut ( ) }
506524 }
@@ -551,9 +569,10 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
551569 }
552570 }
553571
572+ /// The caller must ensure that the node is not the shared root.
554573 fn into_val_slice ( self ) -> & ' a [ V ] {
555574 debug_assert ! ( !self . is_shared_root( ) ) ;
556- // We cannot be the shared root, so `as_leaf` is okay
575+ // We cannot be the shared root, so `as_leaf` is okay.
557576 unsafe { slice:: from_raw_parts ( MaybeUninit :: first_ptr ( & self . as_leaf ( ) . vals ) , self . len ( ) ) }
558577 }
559578
@@ -587,6 +606,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
587606 }
588607 }
589608
609+ /// The caller must ensure that the node is not the shared root.
590610 fn into_val_slice_mut ( mut self ) -> & ' a mut [ V ] {
591611 debug_assert ! ( !self . is_shared_root( ) ) ;
592612 unsafe {
@@ -597,6 +617,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
597617 }
598618 }
599619
620+ /// The caller must ensure that the node is not the shared root.
600621 fn into_slices_mut ( mut self ) -> ( & ' a mut [ K ] , & ' a mut [ V ] ) {
601622 debug_assert ! ( !self . is_shared_root( ) ) ;
602623 // We cannot use the getters here, because calling the second one
@@ -655,6 +676,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
655676 // Necessary for correctness, but this is an internal module
656677 debug_assert ! ( edge. height == self . height - 1 ) ;
657678 debug_assert ! ( self . len( ) < CAPACITY ) ;
679+ debug_assert ! ( !self . is_shared_root( ) ) ;
658680
659681 let idx = self . len ( ) ;
660682
@@ -686,6 +708,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
686708 // Necessary for correctness, but this is an internal module
687709 debug_assert ! ( edge. height == self . height - 1 ) ;
688710 debug_assert ! ( self . len( ) < CAPACITY ) ;
711+ debug_assert ! ( !self . is_shared_root( ) ) ;
689712
690713 unsafe {
691714 slice_insert ( self . keys_mut ( ) , 0 , key) ;
@@ -773,6 +796,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
773796 }
774797 }
775798
799+ /// The caller must ensure that the node is not the shared root.
776800 fn into_kv_pointers_mut ( mut self ) -> ( * mut K , * mut V ) {
777801 ( self . keys_mut ( ) . as_mut_ptr ( ) , self . vals_mut ( ) . as_mut_ptr ( ) )
778802 }
@@ -1116,8 +1140,8 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11161140 }
11171141 }
11181142
1119- /// Removes the key/value pair pointed to by this handle, returning the edge between the
1120- /// now adjacent key/value pairs to the left and right of this handle.
1143+ /// Removes the key/value pair pointed to by this handle and returns it, along with the edge
1144+ /// between the now adjacent key/value pairs (if any) to the left and right of this handle.
11211145 pub fn remove (
11221146 mut self ,
11231147 ) -> ( Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > , K , V ) {
@@ -1260,7 +1284,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12601284 }
12611285 }
12621286
1263- /// This removes a key/value pair from the left child and replaces it with the key/value pair
1287+ /// This removes a key/value pair from the left child and places it in the key/value storage
12641288 /// pointed to by this handle while pushing the old key/value pair of this handle into the right
12651289 /// child.
12661290 pub fn steal_left ( & mut self ) {
@@ -1277,7 +1301,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12771301 }
12781302 }
12791303
1280- /// This removes a key/value pair from the right child and replaces it with the key/value pair
1304+ /// This removes a key/value pair from the right child and places it in the key/value storage
12811305 /// pointed to by this handle while pushing the old key/value pair of this handle into the left
12821306 /// child.
12831307 pub fn steal_right ( & mut self ) {
0 commit comments