@@ -388,16 +388,16 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
388
388
self . as_header ( ) . is_shared_root ( )
389
389
}
390
390
391
- /// Borrows a view into the keys stored in the node.
392
- /// Unsafe because the caller must ensure that the node is not the shared root .
393
- pub unsafe fn keys ( & self ) -> & [ K ] {
394
- self . reborrow ( ) . into_key_slice ( )
391
+ /// Borrows a reference to one of the keys stored in the node.
392
+ /// Unsafe because the caller must ensure that the node has more than `idx` elements .
393
+ pub unsafe fn key_at ( & self , idx : usize ) -> & K {
394
+ self . reborrow ( ) . into_key ( idx )
395
395
}
396
396
397
- /// Borrows a view into the values stored in the node.
398
- /// Unsafe because the caller must ensure that the node is not the shared root .
399
- unsafe fn vals ( & self ) -> & [ V ] {
400
- self . reborrow ( ) . into_val_slice ( )
397
+ /// Borrows a reference to one of the values stored in the node.
398
+ /// Unsafe because the caller must ensure that the node has more than `idx` elements .
399
+ unsafe fn val_at ( & self , idx : usize ) -> & V {
400
+ self . reborrow ( ) . into_val ( idx )
401
401
}
402
402
403
403
/// Finds the parent of the current node. Returns `Ok(handle)` if the current
@@ -525,24 +525,24 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
525
525
}
526
526
527
527
impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Immut < ' a > , K , V , Type > {
528
- /// Unsafe because the caller must ensure that the node is not the shared root .
529
- unsafe fn into_key_slice ( self ) -> & ' a [ K ] {
530
- debug_assert ! ( ! self . is_shared_root ( ) ) ;
531
- // We cannot be the shared root, so `as_leaf` is okay.
532
- slice :: from_raw_parts ( MaybeUninit :: first_ptr ( & self . as_leaf ( ) . keys ) , self . len ( ) )
528
+ /// Unsafe because the caller must ensure that the node has more than `idx` elements .
529
+ unsafe fn into_key ( self , idx : usize ) -> & ' a K {
530
+ debug_assert ! ( idx < self . len ( ) ) ;
531
+ // We cannot be empty, so we cannot be the shared root, so `as_leaf` is okay.
532
+ & * MaybeUninit :: first_ptr ( & self . as_leaf ( ) . keys ) . add ( idx )
533
533
}
534
534
535
- /// Unsafe because the caller must ensure that the node is not the shared root .
536
- unsafe fn into_val_slice ( self ) -> & ' a [ V ] {
537
- debug_assert ! ( ! self . is_shared_root ( ) ) ;
538
- // We cannot be the shared root, so `as_leaf` is okay.
539
- slice :: from_raw_parts ( MaybeUninit :: first_ptr ( & self . as_leaf ( ) . vals ) , self . len ( ) )
535
+ /// Unsafe because the caller must ensure that the node has more than `idx` elements .
536
+ unsafe fn into_val ( self , idx : usize ) -> & ' a V {
537
+ debug_assert ! ( idx < self . len ( ) ) ;
538
+ // We cannot be empty, so we cannot be the shared root, so `as_leaf` is okay.
539
+ & * MaybeUninit :: first_ptr ( & self . as_leaf ( ) . vals ) . add ( idx )
540
540
}
541
541
542
- /// Unsafe because the caller must ensure that the node is not the shared root .
543
- unsafe fn into_slices ( self ) -> ( & ' a [ K ] , & ' a [ V ] ) {
542
+ /// Unsafe because the caller must ensure that the node has more than `idx` elements .
543
+ unsafe fn into_key_val ( self , idx : usize ) -> ( & ' a K , & ' a V ) {
544
544
let k = ptr:: read ( & self ) ;
545
- ( k. into_key_slice ( ) , self . into_val_slice ( ) )
545
+ ( k. into_key ( idx ) , self . into_val ( idx ) )
546
546
}
547
547
}
548
548
@@ -572,19 +572,13 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
572
572
)
573
573
}
574
574
575
- /// Unsafe because the caller must ensure that the node is not the shared root.
576
- unsafe fn into_slices_mut ( mut self ) -> ( & ' a mut [ K ] , & ' a mut [ V ] ) {
577
- debug_assert ! ( !self . is_shared_root( ) ) ;
578
- // We cannot use the getters here, because calling the second one
579
- // invalidates the reference returned by the first.
580
- // More precisely, it is the call to `len` that is the culprit,
581
- // because that creates a shared reference to the header, which *can*
582
- // overlap with the keys (and even the values, for ZST keys).
583
- let len = self . len ( ) ;
575
+ /// Unsafe because the caller must ensure that the node has more than `idx` elements.
576
+ unsafe fn into_key_val_mut ( mut self , idx : usize ) -> ( & ' a mut K , & ' a mut V ) {
577
+ debug_assert ! ( idx < self . len( ) ) ;
584
578
let leaf = self . as_leaf_mut ( ) ;
585
- let keys = slice :: from_raw_parts_mut ( MaybeUninit :: first_ptr_mut ( & mut ( * leaf) . keys ) , len ) ;
586
- let vals = slice :: from_raw_parts_mut ( MaybeUninit :: first_ptr_mut ( & mut ( * leaf) . vals ) , len ) ;
587
- ( keys , vals )
579
+ let key = MaybeUninit :: first_ptr_mut ( & mut ( * leaf) . keys ) . add ( idx ) . as_mut ( ) . unwrap ( ) ;
580
+ let val = MaybeUninit :: first_ptr_mut ( & mut ( * leaf) . vals ) . add ( idx ) . as_mut ( ) . unwrap ( ) ;
581
+ ( key , val )
588
582
}
589
583
}
590
584
@@ -688,8 +682,8 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
688
682
let idx = self . len ( ) - 1 ;
689
683
690
684
unsafe {
691
- let key = ptr:: read ( self . keys ( ) . get_unchecked ( idx) ) ;
692
- let val = ptr:: read ( self . vals ( ) . get_unchecked ( idx) ) ;
685
+ let key = ptr:: read ( self . key_at ( idx) ) ;
686
+ let val = ptr:: read ( self . val_at ( idx) ) ;
693
687
let edge = match self . reborrow_mut ( ) . force ( ) {
694
688
ForceResult :: Leaf ( _) => None ,
695
689
ForceResult :: Internal ( internal) => {
@@ -1039,28 +1033,19 @@ impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V, marker::Internal>, marke
1039
1033
1040
1034
impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Immut < ' a > , K , V , NodeType > , marker:: KV > {
1041
1035
pub fn into_kv ( self ) -> ( & ' a K , & ' a V ) {
1042
- unsafe {
1043
- let ( keys, vals) = self . node . into_slices ( ) ;
1044
- ( keys. get_unchecked ( self . idx ) , vals. get_unchecked ( self . idx ) )
1045
- }
1036
+ unsafe { self . node . into_key_val ( self . idx ) }
1046
1037
}
1047
1038
}
1048
1039
1049
1040
impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1050
1041
pub fn into_kv_mut ( self ) -> ( & ' a mut K , & ' a mut V ) {
1051
- unsafe {
1052
- let ( keys, vals) = self . node . into_slices_mut ( ) ;
1053
- ( keys. get_unchecked_mut ( self . idx ) , vals. get_unchecked_mut ( self . idx ) )
1054
- }
1042
+ unsafe { self . node . into_key_val_mut ( self . idx ) }
1055
1043
}
1056
1044
}
1057
1045
1058
1046
impl < ' a , K , V , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1059
1047
pub fn kv_mut ( & mut self ) -> ( & mut K , & mut V ) {
1060
- unsafe {
1061
- let ( keys, vals) = self . node . reborrow_mut ( ) . into_slices_mut ( ) ;
1062
- ( keys. get_unchecked_mut ( self . idx ) , vals. get_unchecked_mut ( self . idx ) )
1063
- }
1048
+ unsafe { self . node . reborrow_mut ( ) . into_key_val_mut ( self . idx ) }
1064
1049
}
1065
1050
}
1066
1051
@@ -1077,18 +1062,18 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
1077
1062
unsafe {
1078
1063
let mut new_node = Box :: new ( LeafNode :: new ( ) ) ;
1079
1064
1080
- let k = ptr:: read ( self . node . keys ( ) . get_unchecked ( self . idx ) ) ;
1081
- let v = ptr:: read ( self . node . vals ( ) . get_unchecked ( self . idx ) ) ;
1065
+ let k = ptr:: read ( self . node . key_at ( self . idx ) ) ;
1066
+ let v = ptr:: read ( self . node . val_at ( self . idx ) ) ;
1082
1067
1083
1068
let new_len = self . node . len ( ) - self . idx - 1 ;
1084
1069
1085
1070
ptr:: copy_nonoverlapping (
1086
- self . node . keys ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1071
+ self . node . key_at ( self . idx + 1 ) ,
1087
1072
new_node. keys . as_mut_ptr ( ) as * mut K ,
1088
1073
new_len,
1089
1074
) ;
1090
1075
ptr:: copy_nonoverlapping (
1091
- self . node . vals ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1076
+ self . node . val_at ( self . idx + 1 ) ,
1092
1077
new_node. vals . as_mut_ptr ( ) as * mut V ,
1093
1078
new_len,
1094
1079
) ;
@@ -1127,19 +1112,19 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
1127
1112
unsafe {
1128
1113
let mut new_node = Box :: new ( InternalNode :: new ( ) ) ;
1129
1114
1130
- let k = ptr:: read ( self . node . keys ( ) . get_unchecked ( self . idx ) ) ;
1131
- let v = ptr:: read ( self . node . vals ( ) . get_unchecked ( self . idx ) ) ;
1115
+ let k = ptr:: read ( self . node . key_at ( self . idx ) ) ;
1116
+ let v = ptr:: read ( self . node . val_at ( self . idx ) ) ;
1132
1117
1133
1118
let height = self . node . height ;
1134
1119
let new_len = self . node . len ( ) - self . idx - 1 ;
1135
1120
1136
1121
ptr:: copy_nonoverlapping (
1137
- self . node . keys ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1122
+ self . node . key_at ( self . idx + 1 ) ,
1138
1123
new_node. data . keys . as_mut_ptr ( ) as * mut K ,
1139
1124
new_len,
1140
1125
) ;
1141
1126
ptr:: copy_nonoverlapping (
1142
- self . node . vals ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1127
+ self . node . val_at ( self . idx + 1 ) ,
1143
1128
new_node. data . vals . as_mut_ptr ( ) as * mut V ,
1144
1129
new_len,
1145
1130
) ;
@@ -1196,7 +1181,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
1196
1181
slice_remove ( self . node . keys_mut ( ) , self . idx ) ,
1197
1182
) ;
1198
1183
ptr:: copy_nonoverlapping (
1199
- right_node. keys ( ) . as_ptr ( ) ,
1184
+ right_node. key_at ( 0 ) ,
1200
1185
left_node. keys_mut ( ) . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1201
1186
right_len,
1202
1187
) ;
@@ -1205,7 +1190,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
1205
1190
slice_remove ( self . node . vals_mut ( ) , self . idx ) ,
1206
1191
) ;
1207
1192
ptr:: copy_nonoverlapping (
1208
- right_node. vals ( ) . as_ptr ( ) ,
1193
+ right_node. val_at ( 0 ) ,
1209
1194
left_node. vals_mut ( ) . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1210
1195
right_len,
1211
1196
) ;
0 commit comments