Skip to content

Commit 630aaa4

Browse files
committed
avoid some raw ptr casts in BTreeMap
1 parent ffd73df commit 630aaa4

File tree

1 file changed

+10
-7
lines changed
  • src/liballoc/collections/btree

1 file changed

+10
-7
lines changed

src/liballoc/collections/btree/node.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ struct LeafNode<K, V> {
9595

9696
/// The arrays storing the actual data of the node. Only the first `len` elements of each
9797
/// array are initialized and valid.
98-
keys: MaybeUninit<[K; CAPACITY]>,
99-
vals: MaybeUninit<[V; CAPACITY]>,
98+
keys: [MaybeUninit<K>; CAPACITY],
99+
vals: [MaybeUninit<V>; CAPACITY],
100100
}
101101

102102
impl<K, V> LeafNode<K, V> {
@@ -106,8 +106,11 @@ impl<K, V> LeafNode<K, V> {
106106
LeafNode {
107107
// As a general policy, we leave fields uninitialized if they can be, as this should
108108
// be both slightly faster and easier to track in Valgrind.
109-
keys: MaybeUninit::uninitialized(),
110-
vals: MaybeUninit::uninitialized(),
109+
// Creating a `[MaybeUninit; N]` array by first creating a
110+
// `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
111+
// array does not require initialization.
112+
keys: MaybeUninit::uninitialized().into_inner(),
113+
vals: MaybeUninit::uninitialized().into_inner(),
111114
parent: ptr::null(),
112115
parent_idx: MaybeUninit::uninitialized(),
113116
len: 0
@@ -626,7 +629,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
626629
// We cannot be the root, so `as_leaf` is okay
627630
unsafe {
628631
slice::from_raw_parts(
629-
self.as_leaf().vals.as_ptr() as *const V,
632+
MaybeUninit::first_ptr(&self.as_leaf().vals),
630633
self.len()
631634
)
632635
}
@@ -653,7 +656,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
653656
} else {
654657
unsafe {
655658
slice::from_raw_parts_mut(
656-
(*self.as_leaf_mut()).keys.as_mut_ptr() as *mut K,
659+
MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).keys),
657660
self.len()
658661
)
659662
}
@@ -664,7 +667,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
664667
debug_assert!(!self.is_shared_root());
665668
unsafe {
666669
slice::from_raw_parts_mut(
667-
(*self.as_leaf_mut()).vals.as_mut_ptr() as *mut V,
670+
MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).vals),
668671
self.len()
669672
)
670673
}

0 commit comments

Comments
 (0)