diff options
| author | Ralf Jung <post@ralfj.de> | 2018-12-21 15:10:19 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2019-01-28 10:48:38 +0100 |
| commit | 630aaa4e801393c55c8327f642aec2349adfaee3 (patch) | |
| tree | 91f0d3c6c6364464bdc21e545e3e305afe34d906 /src/liballoc | |
| parent | ffd73df7559be73398679d7f09447379dd0c2098 (diff) | |
| download | rust-630aaa4e801393c55c8327f642aec2349adfaee3.tar.gz rust-630aaa4e801393c55c8327f642aec2349adfaee3.zip | |
avoid some raw ptr casts in BTreeMap
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/collections/btree/node.rs | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index cc46bae01cb..8dd4aec136a 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -95,8 +95,8 @@ struct LeafNode<K, V> { /// The arrays storing the actual data of the node. Only the first `len` elements of each /// array are initialized and valid. - keys: MaybeUninit<[K; CAPACITY]>, - vals: MaybeUninit<[V; CAPACITY]>, + keys: [MaybeUninit<K>; CAPACITY], + vals: [MaybeUninit<V>; CAPACITY], } impl<K, V> LeafNode<K, V> { @@ -106,8 +106,11 @@ impl<K, V> LeafNode<K, V> { LeafNode { // As a general policy, we leave fields uninitialized if they can be, as this should // be both slightly faster and easier to track in Valgrind. - keys: MaybeUninit::uninitialized(), - vals: MaybeUninit::uninitialized(), + // Creating a `[MaybeUninit; N]` array by first creating a + // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner + // array does not require initialization. + keys: MaybeUninit::uninitialized().into_inner(), + vals: MaybeUninit::uninitialized().into_inner(), parent: ptr::null(), parent_idx: MaybeUninit::uninitialized(), len: 0 @@ -626,7 +629,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> { // We cannot be the root, so `as_leaf` is okay unsafe { slice::from_raw_parts( - self.as_leaf().vals.as_ptr() as *const V, + MaybeUninit::first_ptr(&self.as_leaf().vals), self.len() ) } @@ -653,7 +656,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> { } else { unsafe { slice::from_raw_parts_mut( - (*self.as_leaf_mut()).keys.as_mut_ptr() as *mut K, + MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).keys), self.len() ) } @@ -664,7 +667,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> { debug_assert!(!self.is_shared_root()); unsafe { slice::from_raw_parts_mut( - (*self.as_leaf_mut()).vals.as_mut_ptr() as *mut V, + MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).vals), self.len() ) } |
