@@ -351,7 +351,22 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
351
351
unsafe fn val_at ( & self , idx : usize ) -> & V {
352
352
unsafe { self . reborrow ( ) . into_val_at ( idx) }
353
353
}
354
+ }
354
355
356
+ impl < BorrowType , K , V > NodeRef < BorrowType , K , V , marker:: Internal > {
357
+ /// Borrows a reference to the contents of one of the edges that delimit
358
+ /// the elements of the node, without invalidating other references.
359
+ ///
360
+ /// # Safety
361
+ /// The node has more than `idx` initialized elements.
362
+ unsafe fn edge_at ( & self , idx : usize ) -> & BoxedNode < K , V > {
363
+ debug_assert ! ( idx <= self . len( ) ) ;
364
+ let node = self . as_internal_ptr ( ) ;
365
+ unsafe { ( * node) . edges . get_unchecked ( idx) . assume_init_ref ( ) }
366
+ }
367
+ }
368
+
369
+ impl < BorrowType , K , V , Type > NodeRef < BorrowType , K , V , Type > {
355
370
/// Finds the parent of the current node. Returns `Ok(handle)` if the current
356
371
/// node actually has a parent, where `handle` points to the edge of the parent
357
372
/// that points to the current node. Returns `Err(self)` if the current node has
@@ -498,6 +513,17 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
498
513
}
499
514
}
500
515
516
+ impl < ' a , K , V > NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > {
517
+ fn edges_mut ( & mut self ) -> & mut [ BoxedNode < K , V > ] {
518
+ unsafe {
519
+ slice:: from_raw_parts_mut (
520
+ MaybeUninit :: slice_as_mut_ptr ( & mut self . as_internal_mut ( ) . edges ) ,
521
+ self . len ( ) + 1 ,
522
+ )
523
+ }
524
+ }
525
+ }
526
+
501
527
impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Immut < ' a > , K , V , Type > {
502
528
/// # Safety
503
529
/// The node has more than `idx` initialized elements.
@@ -669,9 +695,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
669
695
let val = ptr:: read ( self . val_at ( idx) ) ;
670
696
let edge = match self . reborrow_mut ( ) . force ( ) {
671
697
ForceResult :: Leaf ( _) => None ,
672
- ForceResult :: Internal ( mut internal) => {
673
- let edge =
674
- ptr:: read ( internal. as_internal ( ) . edges . get_unchecked ( idx + 1 ) . as_ptr ( ) ) ;
698
+ ForceResult :: Internal ( internal) => {
699
+ let edge = ptr:: read ( internal. edge_at ( idx + 1 ) ) ;
675
700
let mut new_root = Root { node : edge, height : internal. height - 1 } ;
676
701
new_root. node_as_mut ( ) . as_leaf_mut ( ) . parent = None ;
677
702
Some ( new_root)
@@ -696,14 +721,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
696
721
let edge = match self . reborrow_mut ( ) . force ( ) {
697
722
ForceResult :: Leaf ( _) => None ,
698
723
ForceResult :: Internal ( mut internal) => {
699
- let edge = slice_remove (
700
- slice:: from_raw_parts_mut (
701
- MaybeUninit :: slice_as_mut_ptr ( & mut internal. as_internal_mut ( ) . edges ) ,
702
- old_len + 1 ,
703
- ) ,
704
- 0 ,
705
- ) ;
706
-
724
+ let edge = slice_remove ( internal. edges_mut ( ) , 0 ) ;
707
725
let mut new_root = Root { node : edge, height : internal. height - 1 } ;
708
726
new_root. node_as_mut ( ) . as_leaf_mut ( ) . parent = None ;
709
727
@@ -972,17 +990,9 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
972
990
debug_assert ! ( edge. height == self . node. height - 1 ) ;
973
991
974
992
unsafe {
993
+ slice_insert ( self . node . edges_mut ( ) , self . idx + 1 , edge. node ) ;
975
994
self . leafy_insert_fit ( key, val) ;
976
995
977
- slice_insert (
978
- slice:: from_raw_parts_mut (
979
- MaybeUninit :: slice_as_mut_ptr ( & mut self . node . as_internal_mut ( ) . edges ) ,
980
- self . node . len ( ) ,
981
- ) ,
982
- self . idx + 1 ,
983
- edge. node ,
984
- ) ;
985
-
986
996
self . node . correct_childrens_parent_links ( ( self . idx + 1 ) ..=self . node . len ( ) ) ;
987
997
}
988
998
}
@@ -1253,7 +1263,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1253
1263
right_len,
1254
1264
) ;
1255
1265
1256
- slice_remove ( & mut self . node . as_internal_mut ( ) . edges , self . idx + 1 ) ;
1266
+ slice_remove ( & mut self . node . edges_mut ( ) , self . idx + 1 ) ;
1257
1267
let self_len = self . node . len ( ) ;
1258
1268
self . node . correct_childrens_parent_links ( self . idx + 1 ..self_len) ;
1259
1269
self . node . as_leaf_mut ( ) . len -= 1 ;
@@ -1264,10 +1274,10 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1264
1274
// SAFETY: the height of the nodes being merged is one below the height
1265
1275
// of the node of this edge, thus above zero, so they are internal.
1266
1276
let mut left_node = left_node. cast_unchecked :: < marker:: Internal > ( ) ;
1267
- let mut right_node = right_node. cast_unchecked :: < marker:: Internal > ( ) ;
1277
+ let right_node = right_node. cast_unchecked :: < marker:: Internal > ( ) ;
1268
1278
ptr:: copy_nonoverlapping (
1269
- right_node. as_internal ( ) . edges . as_ptr ( ) ,
1270
- left_node. as_internal_mut ( ) . edges . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1279
+ right_node. edge_at ( 0 ) ,
1280
+ left_node. edges_mut ( ) . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1271
1281
right_len + 1 ,
1272
1282
) ;
1273
1283
0 commit comments