@@ -465,6 +465,22 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
465
465
unsafe { & mut ( * self . node . as_ptr ( ) ) }
466
466
}
467
467
468
+ /// Borrows a mutable reference to one of the keys stored in the node.
469
+ ///
470
+ /// # Safety
471
+ /// The node has more than `idx` initialized elements.
472
+ pub unsafe fn key_mut_at ( & mut self , idx : usize ) -> & mut K {
473
+ unsafe { self . reborrow_mut ( ) . into_key_mut_at ( idx) }
474
+ }
475
+
476
+ /// Borrows a mutable reference to one of the values stored in the node.
477
+ ///
478
+ /// # Safety
479
+ /// The node has more than `idx` initialized elements.
480
+ pub unsafe fn val_mut_at ( & mut self , idx : usize ) -> & mut V {
481
+ unsafe { self . reborrow_mut ( ) . into_val_mut_at ( idx) }
482
+ }
483
+
468
484
fn keys_mut ( & mut self ) -> & mut [ K ] {
469
485
// SAFETY: the caller will not be able to call further methods on self
470
486
// until the key slice reference is dropped, as we have unique access
@@ -555,15 +571,14 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
555
571
impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > {
556
572
/// Adds a key/value pair to the end of the node.
557
573
pub fn push ( & mut self , key : K , val : V ) {
558
- assert ! ( self . len ( ) < CAPACITY ) ;
559
-
560
- let idx = self . len ( ) ;
561
-
574
+ let len = & mut self . as_leaf_mut ( ) . len ;
575
+ let idx = * len as usize ;
576
+ assert ! ( idx < CAPACITY ) ;
577
+ * len += 1 ;
562
578
unsafe {
563
- ptr:: write ( self . keys_mut ( ) . get_unchecked_mut ( idx) , key) ;
564
- ptr:: write ( self . vals_mut ( ) . get_unchecked_mut ( idx) , val) ;
579
+ ptr:: write ( self . key_mut_at ( idx) , key) ;
580
+ ptr:: write ( self . val_mut_at ( idx) , val) ;
565
581
}
566
- self . as_leaf_mut ( ) . len += 1 ;
567
582
}
568
583
569
584
/// Adds a key/value pair to the beginning of the node.
@@ -600,17 +615,15 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
600
615
/// the end of the node.
601
616
pub fn push ( & mut self , key : K , val : V , edge : Root < K , V > ) {
602
617
assert ! ( edge. height == self . height - 1 ) ;
603
- assert ! ( self . len( ) < CAPACITY ) ;
604
-
605
- let idx = self . len ( ) ;
606
618
619
+ let len = & mut self . as_leaf_mut ( ) . len ;
620
+ let idx = * len as usize ;
621
+ assert ! ( idx < CAPACITY ) ;
622
+ * len += 1 ;
607
623
unsafe {
608
- ptr:: write ( self . keys_mut ( ) . get_unchecked_mut ( idx) , key) ;
609
- ptr:: write ( self . vals_mut ( ) . get_unchecked_mut ( idx) , val) ;
624
+ ptr:: write ( self . key_mut_at ( idx) , key) ;
625
+ ptr:: write ( self . val_mut_at ( idx) , val) ;
610
626
self . as_internal_mut ( ) . edges . get_unchecked_mut ( idx + 1 ) . write ( edge. node ) ;
611
-
612
- self . as_leaf_mut ( ) . len += 1 ;
613
-
614
627
Handle :: new_edge ( self . reborrow_mut ( ) , idx + 1 ) . correct_parent_link ( ) ;
615
628
}
616
629
}
@@ -903,7 +916,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
903
916
/// The returned pointer points to the inserted value.
904
917
fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
905
918
self . leafy_insert_fit ( key, val) ;
906
- unsafe { self . node . vals_mut ( ) . get_unchecked_mut ( self . idx ) }
919
+ unsafe { self . node . val_mut_at ( self . idx ) }
907
920
}
908
921
}
909
922
0 commit comments