Skip to content

Commit 22a947f

Browse files
committed
add macro for creating uninitialized array
1 parent 630aaa4 commit 22a947f

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

src/liballoc/collections/btree/node.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,8 @@ 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-
// 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(),
109+
keys: uninitialized_array![_; CAPACITY],
110+
vals: uninitialized_array![_; CAPACITY],
114111
parent: ptr::null(),
115112
parent_idx: MaybeUninit::uninitialized(),
116113
len: 0
@@ -162,10 +159,7 @@ impl<K, V> InternalNode<K, V> {
162159
unsafe fn new() -> Self {
163160
InternalNode {
164161
data: LeafNode::new(),
165-
// Creating a `[MaybeUninit; N]` array by first creating a
166-
// `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
167-
// array does not require initialization.
168-
edges: MaybeUninit::uninitialized().into_inner(),
162+
edges: uninitialized_array![_; 2*B],
169163
}
170164
}
171165
}

src/libcore/fmt/num.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ trait GenericRadix {
5151
// characters for a base 2 number.
5252
let zero = T::zero();
5353
let is_nonnegative = x >= zero;
54-
// Creating a `[MaybeUninit; N]` array by first creating a
55-
// `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
56-
// array does not require initialization.
57-
let mut buf: [MaybeUninit<u8>; 128] = unsafe {
58-
MaybeUninit::uninitialized().into_inner()
59-
};
54+
let mut buf = uninitialized_array![u8; 128];
6055
let mut curr = buf.len();
6156
let base = T::from_u8(Self::BASE);
6257
if is_nonnegative {
@@ -203,12 +198,7 @@ macro_rules! impl_Display {
203198
// convert the negative num to positive by summing 1 to it's 2 complement
204199
(!self.$conv_fn()).wrapping_add(1)
205200
};
206-
// Creating a `[MaybeUninit; N]` array by first creating a
207-
// `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
208-
// array does not require initialization.
209-
let mut buf: [MaybeUninit<u8>; 39] = unsafe {
210-
MaybeUninit::uninitialized().into_inner()
211-
};
201+
let mut buf = uninitialized_array![u8; 39];
212202
let mut curr = buf.len() as isize;
213203
let buf_ptr = MaybeUninit::first_mut_ptr(&mut buf);
214204
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
#![feature(structural_match)]
124124
#![feature(abi_unadjusted)]
125125
#![feature(adx_target_feature)]
126+
#![feature(maybe_uninit)]
126127

127128
#[prelude_import]
128129
#[allow(unused)]

src/libcore/macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,23 @@ macro_rules! unimplemented {
547547
($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)*)));
548548
}
549549

550+
/// A macro to create an array of [`MaybeUninit`]
551+
///
552+
/// This macro constructs and uninitialized array of the type `[MaybeUninit<K>; N]`.
553+
///
554+
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
555+
#[macro_export]
556+
#[unstable(feature = "maybe_uninit", issue = "53491")]
557+
macro_rules! uninitialized_array {
558+
// This `into_inner` is safe because an array of `MaybeUninit` does not
559+
// require initialization.
560+
// FIXME(#49147): Could be replaced by an array initializer, once those can
561+
// be any const expression.
562+
($t:ty; $size:expr) => (unsafe {
563+
MaybeUninit::<[MaybeUninit<$t>; $size]>::uninitialized().into_inner()
564+
});
565+
}
566+
550567
/// Built-in macros to the compiler itself.
551568
///
552569
/// These macros do not have any corresponding definition with a `macro_rules!`

src/libcore/slice/sort.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,14 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
216216
let mut block_l = BLOCK;
217217
let mut start_l = ptr::null_mut();
218218
let mut end_l = ptr::null_mut();
219-
// Creating a `[MaybeUninit; N]` array by first creating a
220-
// `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner
221-
// array does not require initialization.
222-
let mut offsets_l: [MaybeUninit<u8>; BLOCK] = unsafe {
223-
MaybeUninit::uninitialized().into_inner()
224-
};
219+
let mut offsets_l: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
225220

226221
// The current block on the right side (from `r.sub(block_r)` to `r`).
227222
let mut r = unsafe { l.add(v.len()) };
228223
let mut block_r = BLOCK;
229224
let mut start_r = ptr::null_mut();
230225
let mut end_r = ptr::null_mut();
231-
let mut offsets_r: [MaybeUninit<u8>; BLOCK] = unsafe {
232-
MaybeUninit::uninitialized().into_inner()
233-
};
226+
let mut offsets_r: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
234227

235228
// FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather
236229
// than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient.

0 commit comments

Comments
 (0)