@@ -351,7 +351,22 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
351351 unsafe fn val_at ( & self , idx : usize ) -> & V {
352352 unsafe { self . reborrow ( ) . into_val_at ( idx) }
353353 }
354+ }
354355
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 > {
355370 /// Finds the parent of the current node. Returns `Ok(handle)` if the current
356371 /// node actually has a parent, where `handle` points to the edge of the parent
357372 /// 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> {
498513 }
499514}
500515
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+
501527impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Immut < ' a > , K , V , Type > {
502528 /// # Safety
503529 /// 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> {
669695 let val = ptr:: read ( self . val_at ( idx) ) ;
670696 let edge = match self . reborrow_mut ( ) . force ( ) {
671697 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 ) ) ;
675700 let mut new_root = Root { node : edge, height : internal. height - 1 } ;
676701 new_root. node_as_mut ( ) . as_leaf_mut ( ) . parent = None ;
677702 Some ( new_root)
@@ -696,14 +721,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
696721 let edge = match self . reborrow_mut ( ) . force ( ) {
697722 ForceResult :: Leaf ( _) => None ,
698723 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 ) ;
707725 let mut new_root = Root { node : edge, height : internal. height - 1 } ;
708726 new_root. node_as_mut ( ) . as_leaf_mut ( ) . parent = None ;
709727
@@ -972,17 +990,9 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
972990 debug_assert ! ( edge. height == self . node. height - 1 ) ;
973991
974992 unsafe {
993+ slice_insert ( self . node . edges_mut ( ) , self . idx + 1 , edge. node ) ;
975994 self . leafy_insert_fit ( key, val) ;
976995
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-
986996 self . node . correct_childrens_parent_links ( ( self . idx + 1 ) ..=self . node . len ( ) ) ;
987997 }
988998 }
@@ -1253,7 +1263,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12531263 right_len,
12541264 ) ;
12551265
1256- slice_remove ( & mut self . node . as_internal_mut ( ) . edges , self . idx + 1 ) ;
1266+ slice_remove ( & mut self . node . edges_mut ( ) , self . idx + 1 ) ;
12571267 let self_len = self . node . len ( ) ;
12581268 self . node . correct_childrens_parent_links ( self . idx + 1 ..self_len) ;
12591269 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>,
12641274 // SAFETY: the height of the nodes being merged is one below the height
12651275 // of the node of this edge, thus above zero, so they are internal.
12661276 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 > ( ) ;
12681278 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 ) ,
12711281 right_len + 1 ,
12721282 ) ;
12731283
0 commit comments