@@ -238,13 +238,12 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
238
238
/// such restrictions:
239
239
/// - For each type parameter, we can only define a method either generically
240
240
/// or for one particular type. For example, we cannot define a method like
241
- /// `key_at` generically for all `BorrowType`, because we want it to return
242
- /// `&'a K` for most choices of `BorrowType`, but plain `K` for `Owned`.
243
- /// We cannot define `key_at` once for all types that carry a lifetime.
241
+ /// `into_kv` generically for all `BorrowType`, or once for all types that
242
+ /// carry a lifetime, because we want it to return `&'a` references.
244
243
/// Therefore, we define it only for the least powerful type `Immut<'a>`.
245
244
/// - We cannot get implicit coercion from say `Mut<'a>` to `Immut<'a>`.
246
245
/// Therefore, we have to explicitly call `reborrow` on a more powerfull
247
- /// `NodeRef` in order to reach a method like `key_at `.
246
+ /// `NodeRef` in order to reach a method like `into_kv `.
248
247
///
249
248
/// All methods on `NodeRef` that return some kind of reference, either:
250
249
/// - Take `self` by value, and return the lifetime carried by `BorrowType`.
@@ -344,26 +343,6 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
344
343
}
345
344
}
346
345
347
- impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Immut < ' a > , K , V , Type > {
348
- /// Exposes one of the keys stored in the node.
349
- ///
350
- /// # Safety
351
- /// The node has more than `idx` initialized elements.
352
- pub unsafe fn key_at ( self , idx : usize ) -> & ' a K {
353
- debug_assert ! ( idx < self . len( ) ) ;
354
- unsafe { self . into_leaf ( ) . keys . get_unchecked ( idx) . assume_init_ref ( ) }
355
- }
356
-
357
- /// Exposes one of the values stored in the node.
358
- ///
359
- /// # Safety
360
- /// The node has more than `idx` initialized elements.
361
- unsafe fn val_at ( self , idx : usize ) -> & ' a V {
362
- debug_assert ! ( idx < self . len( ) ) ;
363
- unsafe { self . into_leaf ( ) . vals . get_unchecked ( idx) . assume_init_ref ( ) }
364
- }
365
- }
366
-
367
346
impl < BorrowType , K , V , Type > NodeRef < BorrowType , K , V , Type > {
368
347
/// Finds the parent of the current node. Returns `Ok(handle)` if the current
369
348
/// node actually has a parent, where `handle` points to the edge of the parent
@@ -421,6 +400,14 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
421
400
// SAFETY: there can be no mutable references into this tree borrowed as `Immut`.
422
401
unsafe { & * ptr }
423
402
}
403
+
404
+ /// Borrows a view into the keys stored in the node.
405
+ pub fn keys ( & self ) -> & [ K ] {
406
+ let leaf = self . into_leaf ( ) ;
407
+ unsafe {
408
+ MaybeUninit :: slice_assume_init_ref ( leaf. keys . get_unchecked ( ..usize:: from ( leaf. len ) ) )
409
+ }
410
+ }
424
411
}
425
412
426
413
impl < K , V > NodeRef < marker:: Owned , K , V , marker:: LeafOrInternal > {
@@ -987,7 +974,11 @@ impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V, marker::Internal>, marke
987
974
988
975
impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Immut < ' a > , K , V , NodeType > , marker:: KV > {
989
976
pub fn into_kv ( self ) -> ( & ' a K , & ' a V ) {
990
- ( unsafe { self . node . key_at ( self . idx ) } , unsafe { self . node . val_at ( self . idx ) } )
977
+ debug_assert ! ( self . idx < self . node. len( ) ) ;
978
+ let leaf = self . node . into_leaf ( ) ;
979
+ let k = unsafe { leaf. keys . get_unchecked ( self . idx ) . assume_init_ref ( ) } ;
980
+ let v = unsafe { leaf. vals . get_unchecked ( self . idx ) . assume_init_ref ( ) } ;
981
+ ( k, v)
991
982
}
992
983
}
993
984
@@ -997,6 +988,7 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
997
988
}
998
989
999
990
pub fn into_val_mut ( self ) -> & ' a mut V {
991
+ debug_assert ! ( self . idx < self . node. len( ) ) ;
1000
992
let leaf = self . node . into_leaf_mut ( ) ;
1001
993
unsafe { leaf. vals . get_unchecked_mut ( self . idx ) . assume_init_mut ( ) }
1002
994
}
@@ -1010,6 +1002,7 @@ impl<'a, K, V, NodeType> Handle<NodeRef<marker::ValMut<'a>, K, V, NodeType>, mar
1010
1002
1011
1003
impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1012
1004
pub fn kv_mut ( & mut self ) -> ( & mut K , & mut V ) {
1005
+ debug_assert ! ( self . idx < self . node. len( ) ) ;
1013
1006
// We cannot call separate key and value methods, because calling the second one
1014
1007
// invalidates the reference returned by the first.
1015
1008
unsafe {
0 commit comments