4242// This implies that even an empty internal node has at least one edge.
4343
4444use core:: marker:: PhantomData ;
45- use core:: mem;
45+ use core:: mem:: { self , MaybeUninit } ;
4646use core:: ptr:: { self , Unique , NonNull } ;
4747use core:: slice;
4848
@@ -73,7 +73,7 @@ struct LeafNode<K, V> {
7373 /// This node's index into the parent node's `edges` array.
7474 /// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
7575 /// This is only guaranteed to be initialized when `parent` is nonnull.
76- parent_idx : u16 ,
76+ parent_idx : MaybeUninit < u16 > ,
7777
7878 /// The number of keys and values this node stores.
7979 ///
@@ -83,8 +83,8 @@ struct LeafNode<K, V> {
8383
8484 /// The arrays storing the actual data of the node. Only the first `len` elements of each
8585 /// array are initialized and valid.
86- keys : [ K ; CAPACITY ] ,
87- vals : [ V ; CAPACITY ] ,
86+ keys : MaybeUninit < [ K ; CAPACITY ] > ,
87+ vals : MaybeUninit < [ V ; CAPACITY ] > ,
8888}
8989
9090impl < K , V > LeafNode < K , V > {
@@ -94,10 +94,10 @@ impl<K, V> LeafNode<K, V> {
9494 LeafNode {
9595 // As a general policy, we leave fields uninitialized if they can be, as this should
9696 // be both slightly faster and easier to track in Valgrind.
97- keys : mem :: uninitialized ( ) ,
98- vals : mem :: uninitialized ( ) ,
97+ keys : MaybeUninit :: uninitialized ( ) ,
98+ vals : MaybeUninit :: uninitialized ( ) ,
9999 parent : ptr:: null ( ) ,
100- parent_idx : mem :: uninitialized ( ) ,
100+ parent_idx : MaybeUninit :: uninitialized ( ) ,
101101 len : 0
102102 }
103103 }
@@ -115,10 +115,10 @@ unsafe impl Sync for LeafNode<(), ()> {}
115115// ever take a pointer past the first key.
116116static EMPTY_ROOT_NODE : LeafNode < ( ) , ( ) > = LeafNode {
117117 parent : ptr:: null ( ) ,
118- parent_idx : 0 ,
118+ parent_idx : MaybeUninit :: uninitialized ( ) ,
119119 len : 0 ,
120- keys : [ ( ) ; CAPACITY ] ,
121- vals : [ ( ) ; CAPACITY ] ,
120+ keys : MaybeUninit :: uninitialized ( ) ,
121+ vals : MaybeUninit :: uninitialized ( ) ,
122122} ;
123123
124124/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
@@ -430,7 +430,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
430430 root : self . root ,
431431 _marker : PhantomData
432432 } ,
433- idx : self . as_leaf ( ) . parent_idx as usize ,
433+ idx : unsafe { usize :: from ( * self . as_leaf ( ) . parent_idx . get_ref ( ) ) } ,
434434 _marker : PhantomData
435435 } )
436436 } else {
@@ -567,7 +567,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
567567 // the node, which is allowed by LLVM.
568568 unsafe {
569569 slice:: from_raw_parts (
570- self . as_leaf ( ) . keys . as_ptr ( ) ,
570+ self . as_leaf ( ) . keys . as_ptr ( ) as * const K ,
571571 self . len ( )
572572 )
573573 }
@@ -578,7 +578,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
578578 debug_assert ! ( !self . is_shared_root( ) ) ;
579579 unsafe {
580580 slice:: from_raw_parts (
581- self . as_leaf ( ) . vals . as_ptr ( ) ,
581+ self . as_leaf ( ) . vals . as_ptr ( ) as * const V ,
582582 self . len ( )
583583 )
584584 }
@@ -605,7 +605,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
605605 } else {
606606 unsafe {
607607 slice:: from_raw_parts_mut (
608- & mut self . as_leaf_mut ( ) . keys as * mut [ K ] as * mut K ,
608+ self . as_leaf_mut ( ) . keys . get_mut ( ) as * mut [ K ] as * mut K ,
609609 self . len ( )
610610 )
611611 }
@@ -616,7 +616,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
616616 debug_assert ! ( !self . is_shared_root( ) ) ;
617617 unsafe {
618618 slice:: from_raw_parts_mut (
619- & mut self . as_leaf_mut ( ) . vals as * mut [ V ] as * mut V ,
619+ self . as_leaf_mut ( ) . vals . get_mut ( ) as * mut [ V ] as * mut V ,
620620 self . len ( )
621621 )
622622 }
@@ -1013,7 +1013,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
10131013 let ptr = self . node . as_internal_mut ( ) as * mut _ ;
10141014 let mut child = self . descend ( ) ;
10151015 child. as_leaf_mut ( ) . parent = ptr;
1016- child. as_leaf_mut ( ) . parent_idx = idx;
1016+ child. as_leaf_mut ( ) . parent_idx . set ( idx) ;
10171017 }
10181018
10191019 /// Unsafely asserts to the compiler some static information about whether the underlying
@@ -1152,12 +1152,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
11521152
11531153 ptr:: copy_nonoverlapping (
11541154 self . node . keys ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1155- new_node. keys . as_mut_ptr ( ) ,
1155+ new_node. keys . as_mut_ptr ( ) as * mut K ,
11561156 new_len
11571157 ) ;
11581158 ptr:: copy_nonoverlapping (
11591159 self . node . vals ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1160- new_node. vals . as_mut_ptr ( ) ,
1160+ new_node. vals . as_mut_ptr ( ) as * mut V ,
11611161 new_len
11621162 ) ;
11631163
@@ -1210,12 +1210,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
12101210
12111211 ptr:: copy_nonoverlapping (
12121212 self . node . keys ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1213- new_node. data . keys . as_mut_ptr ( ) ,
1213+ new_node. data . keys . as_mut_ptr ( ) as * mut K ,
12141214 new_len
12151215 ) ;
12161216 ptr:: copy_nonoverlapping (
12171217 self . node . vals ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1218- new_node. data . vals . as_mut_ptr ( ) ,
1218+ new_node. data . vals . as_mut_ptr ( ) as * mut V ,
12191219 new_len
12201220 ) ;
12211221 ptr:: copy_nonoverlapping (
0 commit comments