@@ -308,15 +308,15 @@ impl<K, V> Root<K, V> {
308
308
/// `Leaf`, the `NodeRef` points to a leaf node, when this is `Internal` the
309
309
/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
310
310
/// `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.
316
316
pub struct NodeRef < BorrowType , K , V , Type > {
317
317
height : usize ,
318
318
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`
320
320
root : * const Root < K , V > ,
321
321
_marker : PhantomData < ( BorrowType , Type ) > ,
322
322
}
@@ -370,23 +370,33 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
370
370
NodeRef { height : self . height , node : self . node , root : self . root , _marker : PhantomData }
371
371
}
372
372
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.
374
378
unsafe fn as_leaf ( & self ) -> & LeafNode < K , V > {
379
+ debug_assert ! ( !self . is_shared_root( ) ) ;
375
380
self . node . as_ref ( )
376
381
}
377
382
378
383
fn as_header ( & self ) -> & NodeHeader < K , V > {
379
384
unsafe { & * ( self . node . as_ptr ( ) as * const NodeHeader < K , V > ) }
380
385
}
381
386
387
+ /// Returns whether the node is the shared, empty root.
382
388
pub fn is_shared_root ( & self ) -> bool {
383
389
self . as_header ( ) . is_shared_root ( )
384
390
}
385
391
392
+ /// Borrows a view into the keys stored in the node.
393
+ /// Works on all possible nodes, including the shared root.
386
394
pub fn keys ( & self ) -> & [ K ] {
387
395
self . reborrow ( ) . into_key_slice ( )
388
396
}
389
397
398
+ /// Borrows a view into the values stored in the node.
399
+ /// The caller must ensure that the node is not the shared root.
390
400
fn vals ( & self ) -> & [ V ] {
391
401
self . reborrow ( ) . into_val_slice ( )
392
402
}
@@ -491,16 +501,24 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
491
501
NodeRef { height : self . height , node : self . node , root : self . root , _marker : PhantomData }
492
502
}
493
503
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
+ ///
494
509
/// 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.
495
512
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.
497
513
self . node . as_ptr ( )
498
514
}
499
515
516
+ /// The caller must ensure that the node is not the shared root.
500
517
fn keys_mut ( & mut self ) -> & mut [ K ] {
501
518
unsafe { self . reborrow_mut ( ) . into_key_slice_mut ( ) }
502
519
}
503
520
521
+ /// The caller must ensure that the node is not the shared root.
504
522
fn vals_mut ( & mut self ) -> & mut [ V ] {
505
523
unsafe { self . reborrow_mut ( ) . into_val_slice_mut ( ) }
506
524
}
@@ -551,9 +569,10 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
551
569
}
552
570
}
553
571
572
+ /// The caller must ensure that the node is not the shared root.
554
573
fn into_val_slice ( self ) -> & ' a [ V ] {
555
574
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.
557
576
unsafe { slice:: from_raw_parts ( MaybeUninit :: first_ptr ( & self . as_leaf ( ) . vals ) , self . len ( ) ) }
558
577
}
559
578
@@ -587,6 +606,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
587
606
}
588
607
}
589
608
609
+ /// The caller must ensure that the node is not the shared root.
590
610
fn into_val_slice_mut ( mut self ) -> & ' a mut [ V ] {
591
611
debug_assert ! ( !self . is_shared_root( ) ) ;
592
612
unsafe {
@@ -597,6 +617,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
597
617
}
598
618
}
599
619
620
+ /// The caller must ensure that the node is not the shared root.
600
621
fn into_slices_mut ( mut self ) -> ( & ' a mut [ K ] , & ' a mut [ V ] ) {
601
622
debug_assert ! ( !self . is_shared_root( ) ) ;
602
623
// 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> {
655
676
// Necessary for correctness, but this is an internal module
656
677
debug_assert ! ( edge. height == self . height - 1 ) ;
657
678
debug_assert ! ( self . len( ) < CAPACITY ) ;
679
+ debug_assert ! ( !self . is_shared_root( ) ) ;
658
680
659
681
let idx = self . len ( ) ;
660
682
@@ -686,6 +708,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
686
708
// Necessary for correctness, but this is an internal module
687
709
debug_assert ! ( edge. height == self . height - 1 ) ;
688
710
debug_assert ! ( self . len( ) < CAPACITY ) ;
711
+ debug_assert ! ( !self . is_shared_root( ) ) ;
689
712
690
713
unsafe {
691
714
slice_insert ( self . keys_mut ( ) , 0 , key) ;
@@ -773,6 +796,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
773
796
}
774
797
}
775
798
799
+ /// The caller must ensure that the node is not the shared root.
776
800
fn into_kv_pointers_mut ( mut self ) -> ( * mut K , * mut V ) {
777
801
( self . keys_mut ( ) . as_mut_ptr ( ) , self . vals_mut ( ) . as_mut_ptr ( ) )
778
802
}
@@ -1116,8 +1140,8 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
1116
1140
}
1117
1141
}
1118
1142
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.
1121
1145
pub fn remove (
1122
1146
mut self ,
1123
1147
) -> ( 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::
1260
1284
}
1261
1285
}
1262
1286
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
1264
1288
/// pointed to by this handle while pushing the old key/value pair of this handle into the right
1265
1289
/// child.
1266
1290
pub fn steal_left ( & mut self ) {
@@ -1277,7 +1301,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
1277
1301
}
1278
1302
}
1279
1303
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
1281
1305
/// pointed to by this handle while pushing the old key/value pair of this handle into the left
1282
1306
/// child.
1283
1307
pub fn steal_right ( & mut self ) {
0 commit comments