@@ -257,8 +257,13 @@ impl<K, V> Root<K, V> {
257
257
/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
258
258
/// `NodeRef` could be pointing to either type of node.
259
259
pub struct NodeRef < BorrowType , K , V , Type > {
260
- /// The number of levels below the node.
260
+ /// The number of levels below the node, a property of the node that cannot be
261
+ /// entirely described by `Type` and that the node does not store itself either.
262
+ /// Unconstrained if `Type` is `LeafOrInternal`, must be zero if `Type` is `Leaf`,
263
+ /// and must be non-zero if `Type` is `Internal`.
261
264
height : usize ,
265
+ /// The pointer to the leaf or internal node. The definition of `InternalNode`
266
+ /// ensures that the pointer is valid either way.
262
267
node : NonNull < LeafNode < K , V > > ,
263
268
_marker : PhantomData < ( BorrowType , Type ) > ,
264
269
}
@@ -315,8 +320,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
315
320
unsafe { usize:: from ( ( * self . as_leaf_ptr ( ) ) . len ) }
316
321
}
317
322
318
- /// Returns the height of this node in the whole tree . Zero height denotes the
319
- /// leaf level .
323
+ /// Returns the height of this node with respect to the leaf level . Zero height means the
324
+ /// node is a leaf itself .
320
325
pub fn height ( & self ) -> usize {
321
326
self . height
322
327
}
@@ -584,9 +589,11 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
584
589
// to avoid aliasing with outstanding references to other elements,
585
590
// in particular, those returned to the caller in earlier iterations.
586
591
let leaf = self . node . as_ptr ( ) ;
592
+ let keys = unsafe { & raw const ( * leaf) . keys } ;
593
+ let vals = unsafe { & raw mut ( * leaf) . vals } ;
587
594
// We must coerce to unsized array pointers because of Rust issue #74679.
588
- let keys: * const [ _ ] = unsafe { & raw const ( * leaf ) . keys } ;
589
- let vals: * mut [ _ ] = unsafe { & raw mut ( * leaf ) . vals } ;
595
+ let keys: * const [ _ ] = keys;
596
+ let vals: * mut [ _ ] = vals;
590
597
// SAFETY: The keys and values of a node must always be initialized up to length.
591
598
let key = unsafe { ( & * keys. get_unchecked ( idx) ) . assume_init_ref ( ) } ;
592
599
let val = unsafe { ( & mut * vals. get_unchecked_mut ( idx) ) . assume_init_mut ( ) } ;
@@ -817,19 +824,34 @@ impl<BorrowType, K, V, NodeType> Handle<NodeRef<BorrowType, K, V, NodeType>, mar
817
824
}
818
825
}
819
826
827
+ impl < BorrowType , K , V , NodeType > NodeRef < BorrowType , K , V , NodeType > {
828
+ /// Could be a public implementation of PartialEq, but only used in this module.
829
+ fn eq ( & self , other : & Self ) -> bool {
830
+ let Self { node, height, _marker : _ } = self ;
831
+ if * node == other. node {
832
+ debug_assert_eq ! ( * height, other. height) ;
833
+ true
834
+ } else {
835
+ false
836
+ }
837
+ }
838
+ }
839
+
820
840
impl < BorrowType , K , V , NodeType , HandleType > PartialEq
821
841
for Handle < NodeRef < BorrowType , K , V , NodeType > , HandleType >
822
842
{
823
843
fn eq ( & self , other : & Self ) -> bool {
824
- self . node . node == other. node . node && self . idx == other. idx
844
+ let Self { node, idx, _marker : _ } = self ;
845
+ node. eq ( & other. node ) && * idx == other. idx
825
846
}
826
847
}
827
848
828
849
impl < BorrowType , K , V , NodeType , HandleType > PartialOrd
829
850
for Handle < NodeRef < BorrowType , K , V , NodeType > , HandleType >
830
851
{
831
852
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
832
- if self . node . node == other. node . node { Some ( self . idx . cmp ( & other. idx ) ) } else { None }
853
+ let Self { node, idx, _marker : _ } = self ;
854
+ if node. eq ( & other. node ) { Some ( idx. cmp ( & other. idx ) ) } else { None }
833
855
}
834
856
}
835
857
0 commit comments