@@ -597,18 +597,17 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
597597
598598impl < ' a , K , V > NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > {
599599 /// # Safety
600- /// 'first' and 'after_last' must be in range.
601- unsafe fn correct_childrens_parent_links ( & mut self , first : usize , after_last : usize ) {
602- debug_assert ! ( first <= self . len( ) ) ;
603- debug_assert ! ( after_last <= self . len( ) + 1 ) ;
604- for i in first..after_last {
600+ /// Every item returned by `range` is a valid edge index for the node.
601+ unsafe fn correct_childrens_parent_links < R : Iterator < Item = usize > > ( & mut self , range : R ) {
602+ for i in range {
603+ debug_assert ! ( i <= self . len( ) ) ;
605604 unsafe { Handle :: new_edge ( self . reborrow_mut ( ) , i) } . correct_parent_link ( ) ;
606605 }
607606 }
608607
609608 fn correct_all_childrens_parent_links ( & mut self ) {
610609 let len = self . len ( ) ;
611- unsafe { self . correct_childrens_parent_links ( 0 , len + 1 ) } ;
610+ unsafe { self . correct_childrens_parent_links ( 0 ..= len) } ;
612611 }
613612}
614613
@@ -708,9 +707,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
708707 let mut new_root = Root { node : edge, height : internal. height - 1 } ;
709708 new_root. node_as_mut ( ) . as_leaf_mut ( ) . parent = None ;
710709
711- for i in 0 ..old_len {
712- Handle :: new_edge ( internal. reborrow_mut ( ) , i) . correct_parent_link ( ) ;
713- }
710+ internal. correct_childrens_parent_links ( 0 ..old_len) ;
714711
715712 Some ( new_root)
716713 }
@@ -986,9 +983,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
986983 edge. node ,
987984 ) ;
988985
989- for i in ( self . idx + 1 ) ..( self . node . len ( ) + 1 ) {
990- Handle :: new_edge ( self . node . reborrow_mut ( ) , i) . correct_parent_link ( ) ;
991- }
986+ self . node . correct_childrens_parent_links ( ( self . idx + 1 ) ..=self . node . len ( ) ) ;
992987 }
993988 }
994989
@@ -1215,9 +1210,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12151210
12161211 let mut new_root = Root { node : BoxedNode :: from_internal ( new_node) , height } ;
12171212
1218- for i in 0 ..( new_len + 1 ) {
1219- Handle :: new_edge ( new_root. node_as_mut ( ) . cast_unchecked ( ) , i) . correct_parent_link ( ) ;
1220- }
1213+ new_root. node_as_mut ( ) . cast_unchecked ( ) . correct_childrens_parent_links ( 0 ..=new_len) ;
12211214
12221215 ( self . node , k, v, new_root)
12231216 }
@@ -1261,27 +1254,24 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
12611254 ) ;
12621255
12631256 slice_remove ( & mut self . node . as_internal_mut ( ) . edges , self . idx + 1 ) ;
1264- for i in self . idx + 1 ..self . node . len ( ) {
1265- Handle :: new_edge ( self . node . reborrow_mut ( ) , i) . correct_parent_link ( ) ;
1266- }
1257+ let self_len = self . node . len ( ) ;
1258+ self . node . correct_childrens_parent_links ( self . idx + 1 ..self_len) ;
12671259 self . node . as_leaf_mut ( ) . len -= 1 ;
12681260
12691261 left_node. as_leaf_mut ( ) . len += right_len as u16 + 1 ;
12701262
12711263 if self . node . height > 1 {
12721264 // SAFETY: the height of the nodes being merged is one below the height
12731265 // of the node of this edge, thus above zero, so they are internal.
1274- let mut left_node = left_node. cast_unchecked ( ) ;
1275- let mut right_node = right_node. cast_unchecked ( ) ;
1266+ let mut left_node = left_node. cast_unchecked :: < marker :: Internal > ( ) ;
1267+ let mut right_node = right_node. cast_unchecked :: < marker :: Internal > ( ) ;
12761268 ptr:: copy_nonoverlapping (
12771269 right_node. as_internal ( ) . edges . as_ptr ( ) ,
12781270 left_node. as_internal_mut ( ) . edges . as_mut_ptr ( ) . add ( left_len + 1 ) ,
12791271 right_len + 1 ,
12801272 ) ;
12811273
1282- for i in left_len + 1 ..left_len + right_len + 2 {
1283- Handle :: new_edge ( left_node. reborrow_mut ( ) , i) . correct_parent_link ( ) ;
1284- }
1274+ left_node. correct_childrens_parent_links ( left_len + 1 ..=left_len + 1 + right_len) ;
12851275
12861276 Global . dealloc ( right_node. node . cast ( ) , Layout :: new :: < InternalNode < K , V > > ( ) ) ;
12871277 } else {
@@ -1371,7 +1361,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
13711361 // Make room for stolen edges.
13721362 let right_edges = right. reborrow_mut ( ) . as_internal_mut ( ) . edges . as_mut_ptr ( ) ;
13731363 ptr:: copy ( right_edges, right_edges. add ( count) , right_len + 1 ) ;
1374- right. correct_childrens_parent_links ( count, count + right_len + 1 ) ;
1364+ right. correct_childrens_parent_links ( count.. count + right_len + 1 ) ;
13751365
13761366 move_edges ( left, new_left_len + 1 , right, 0 , count) ;
13771367 }
@@ -1430,7 +1420,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
14301420 // Fix right indexing.
14311421 let right_edges = right. reborrow_mut ( ) . as_internal_mut ( ) . edges . as_mut_ptr ( ) ;
14321422 ptr:: copy ( right_edges. add ( count) , right_edges, new_right_len + 1 ) ;
1433- right. correct_childrens_parent_links ( 0 , new_right_len + 1 ) ;
1423+ right. correct_childrens_parent_links ( 0 ..= new_right_len) ;
14341424 }
14351425 ( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
14361426 _ => {
@@ -1466,7 +1456,7 @@ unsafe fn move_edges<K, V>(
14661456 let dest_ptr = dest. as_internal_mut ( ) . edges . as_mut_ptr ( ) ;
14671457 unsafe {
14681458 ptr:: copy_nonoverlapping ( source_ptr. add ( source_offset) , dest_ptr. add ( dest_offset) , count) ;
1469- dest. correct_childrens_parent_links ( dest_offset, dest_offset + count) ;
1459+ dest. correct_childrens_parent_links ( dest_offset.. dest_offset + count) ;
14701460 }
14711461}
14721462
0 commit comments