add macro for creating uninitialized array

This commit is contained in:
Ralf Jung 2018-12-22 11:02:06 +01:00
parent 630aaa4e80
commit 22a947f3aa
5 changed files with 25 additions and 30 deletions

View file

@ -106,11 +106,8 @@ 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.
// 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(),
keys: uninitialized_array![_; CAPACITY],
vals: uninitialized_array![_; CAPACITY],
parent: ptr::null(),
parent_idx: MaybeUninit::uninitialized(),
len: 0
@ -162,10 +159,7 @@ impl<K, V> InternalNode<K, V> {
unsafe fn new() -> Self {
InternalNode {
data: LeafNode::new(),
// 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.
edges: MaybeUninit::uninitialized().into_inner(),
edges: uninitialized_array![_; 2*B],
}
}
}

View file

@ -51,12 +51,7 @@ trait GenericRadix {
// characters for a base 2 number.
let zero = T::zero();
let is_nonnegative = x >= zero;
// 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.
let mut buf: [MaybeUninit<u8>; 128] = unsafe {
MaybeUninit::uninitialized().into_inner()
};
let mut buf = uninitialized_array![u8; 128];
let mut curr = buf.len();
let base = T::from_u8(Self::BASE);
if is_nonnegative {
@ -203,12 +198,7 @@ macro_rules! impl_Display {
// convert the negative num to positive by summing 1 to it's 2 complement
(!self.$conv_fn()).wrapping_add(1)
};
// 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.
let mut buf: [MaybeUninit<u8>; 39] = unsafe {
MaybeUninit::uninitialized().into_inner()
};
let mut buf = uninitialized_array![u8; 39];
let mut curr = buf.len() as isize;
let buf_ptr = MaybeUninit::first_mut_ptr(&mut buf);
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

View file

@ -123,6 +123,7 @@
#![feature(structural_match)]
#![feature(abi_unadjusted)]
#![feature(adx_target_feature)]
#![feature(maybe_uninit)]
#[prelude_import]
#[allow(unused)]

View file

@ -547,6 +547,23 @@ macro_rules! unimplemented {
($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)*)));
}
/// A macro to create an array of [`MaybeUninit`]
///
/// This macro constructs and uninitialized array of the type `[MaybeUninit<K>; N]`.
///
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
#[macro_export]
#[unstable(feature = "maybe_uninit", issue = "53491")]
macro_rules! uninitialized_array {
// This `into_inner` is safe because an array of `MaybeUninit` does not
// require initialization.
// FIXME(#49147): Could be replaced by an array initializer, once those can
// be any const expression.
($t:ty; $size:expr) => (unsafe {
MaybeUninit::<[MaybeUninit<$t>; $size]>::uninitialized().into_inner()
});
}
/// Built-in macros to the compiler itself.
///
/// These macros do not have any corresponding definition with a `macro_rules!`

View file

@ -216,21 +216,14 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
let mut block_l = BLOCK;
let mut start_l = ptr::null_mut();
let mut end_l = ptr::null_mut();
// 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.
let mut offsets_l: [MaybeUninit<u8>; BLOCK] = unsafe {
MaybeUninit::uninitialized().into_inner()
};
let mut offsets_l: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
// The current block on the right side (from `r.sub(block_r)` to `r`).
let mut r = unsafe { l.add(v.len()) };
let mut block_r = BLOCK;
let mut start_r = ptr::null_mut();
let mut end_r = ptr::null_mut();
let mut offsets_r: [MaybeUninit<u8>; BLOCK] = unsafe {
MaybeUninit::uninitialized().into_inner()
};
let mut offsets_r: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
// FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather
// than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient.