Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename MaybeUninit to MaybeUninitialized #56138

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
// This implies that even an empty internal node has at least one edge.

use core::marker::PhantomData;
use core::mem::{self, MaybeUninit};
use core::mem::{self, MaybeUninitialized};
use core::ptr::{self, Unique, NonNull};
use core::slice;

Expand Down Expand Up @@ -70,7 +70,7 @@ struct LeafNode<K, V> {
/// This node's index into the parent node's `edges` array.
/// `*node.parent.edges[node.parent_idx]` should be the same thing as `node`.
/// This is only guaranteed to be initialized when `parent` is non-null.
parent_idx: MaybeUninit<u16>,
parent_idx: MaybeUninitialized<u16>,

/// The number of keys and values this node stores.
///
Expand All @@ -80,8 +80,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: MaybeUninitialized<[K; CAPACITY]>,
vals: MaybeUninitialized<[V; CAPACITY]>,
}

impl<K, V> LeafNode<K, V> {
Expand All @@ -91,10 +91,10 @@ 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(),
keys: MaybeUninitialized::uninitialized(),
vals: MaybeUninitialized::uninitialized(),
parent: ptr::null(),
parent_idx: MaybeUninit::uninitialized(),
parent_idx: MaybeUninitialized::uninitialized(),
len: 0
}
}
Expand All @@ -112,10 +112,10 @@ unsafe impl Sync for LeafNode<(), ()> {}
// ever take a pointer past the first key.
static EMPTY_ROOT_NODE: LeafNode<(), ()> = LeafNode {
parent: ptr::null(),
parent_idx: MaybeUninit::uninitialized(),
parent_idx: MaybeUninitialized::uninitialized(),
len: 0,
keys: MaybeUninit::uninitialized(),
vals: MaybeUninit::uninitialized(),
keys: MaybeUninitialized::uninitialized(),
vals: MaybeUninitialized::uninitialized(),
};

/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
Expand Down
20 changes: 10 additions & 10 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
use mem::MaybeUninit;
use mem::MaybeUninitialized;
use num::flt2dec;

// Don't inline this so callers don't use the stack space this function
Expand All @@ -20,10 +20,10 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
where T: flt2dec::DecodableFloat
{
unsafe {
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
let mut buf = MaybeUninitialized::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
let mut parts = MaybeUninitialized::<[flt2dec::Part; 4]>::uninitialized();
// FIXME(#53491): Technically, this is calling `get_mut` on an uninitialized
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
// `MaybeUninitialized` (here and elsewhere in this file). Revisit this once
// we decided whether that is valid or not.
let formatted = flt2dec::to_exact_fixed_str(flt2dec::strategy::grisu::format_exact,
*num, sign, precision,
Expand All @@ -41,8 +41,8 @@ fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
{
unsafe {
// enough for f32 and f64
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
let mut parts = MaybeUninit::<[flt2dec::Part; 4]>::uninitialized();
let mut buf = MaybeUninitialized::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
let mut parts = MaybeUninitialized::<[flt2dec::Part; 4]>::uninitialized();
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
sign, precision, false, buf.get_mut(),
parts.get_mut());
Expand Down Expand Up @@ -79,8 +79,8 @@ fn float_to_exponential_common_exact<T>(fmt: &mut Formatter, num: &T,
where T: flt2dec::DecodableFloat
{
unsafe {
let mut buf = MaybeUninit::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
let mut buf = MaybeUninitialized::<[u8; 1024]>::uninitialized(); // enough for f32 and f64
let mut parts = MaybeUninitialized::<[flt2dec::Part; 6]>::uninitialized();
let formatted = flt2dec::to_exact_exp_str(flt2dec::strategy::grisu::format_exact,
*num, sign, precision,
upper, buf.get_mut(), parts.get_mut());
Expand All @@ -98,8 +98,8 @@ fn float_to_exponential_common_shortest<T>(fmt: &mut Formatter,
{
unsafe {
// enough for f32 and f64
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
let mut parts = MaybeUninit::<[flt2dec::Part; 6]>::uninitialized();
let mut buf = MaybeUninitialized::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninitialized();
let mut parts = MaybeUninitialized::<[flt2dec::Part; 6]>::uninitialized();
let formatted = flt2dec::to_shortest_exp_str(flt2dec::strategy::grisu::format_shortest,
*num, sign, (0, 0), upper,
buf.get_mut(), parts.get_mut());
Expand Down
61 changes: 32 additions & 29 deletions src/libcore/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ pub const fn needs_drop<T>() -> bool {
/// assert_eq!(0, x);
/// ```
#[inline]
#[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::zeroed` instead")]
#[rustc_deprecated(since = "2.0.0",
reason = "use `mem::MaybeUninitialized::zeroed` instead")]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn zeroed<T>() -> T {
intrinsics::init()
Expand Down Expand Up @@ -592,7 +593,8 @@ pub unsafe fn zeroed<T>() -> T {
/// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html
/// [`Drop`]: ../ops/trait.Drop.html
#[inline]
#[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::uninitialized` instead")]
#[rustc_deprecated(since = "2.0.0",
reason = "use `mem::MaybeUninitialized::uninitialized` instead")]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn uninitialized<T>() -> T {
intrinsics::uninit()
Expand Down Expand Up @@ -1031,52 +1033,53 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
/// A newtype to construct uninitialized instances of `T`
#[allow(missing_debug_implementations)]
#[unstable(feature = "maybe_uninit", issue = "53491")]
// NOTE after stabilizing `MaybeUninit` proceed to deprecate `mem::{uninitialized,zeroed}`
pub union MaybeUninit<T> {
// NOTE after stabilizing `MaybeUninitialized` proceed to deprecate `mem::{uninitialized,zeroed}`
pub union MaybeUninitialized<T> {
uninit: (),
value: ManuallyDrop<T>,
}

impl<T> MaybeUninit<T> {
/// Create a new `MaybeUninit` initialized with the given value.
impl<T> MaybeUninitialized<T> {
/// Create a new `MaybeUninitialized` initialized with the given value.
///
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
/// Note that dropping a `MaybeUninitialized` will never call `T`'s drop code.
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub const fn new(val: T) -> MaybeUninit<T> {
MaybeUninit { value: ManuallyDrop::new(val) }
pub const fn new(val: T) -> MaybeUninitialized<T> {
MaybeUninitialized { value: ManuallyDrop::new(val) }
}

/// Create a new `MaybeUninit` in an uninitialized state.
/// Create a new `MaybeUninitialized` in an uninitialized state.
///
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
/// Note that dropping a `MaybeUninitialized` will never call `T`'s drop code.
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub const fn uninitialized() -> MaybeUninit<T> {
MaybeUninit { uninit: () }
pub const fn uninitialized() -> MaybeUninitialized<T> {
MaybeUninitialized { uninit: () }
}

/// Create a new `MaybeUninit` in an uninitialized state, with the memory being
/// Create a new `MaybeUninitialized` in an uninitialized state, with the memory being
/// filled with `0` bytes. It depends on `T` whether that already makes for
/// proper initialization. For example, `MaybeUninit<usize>::zeroed()` is initialized,
/// but `MaybeUninit<&'static i32>::zeroed()` is not because references must not
/// proper initialization. For example, `MaybeUninitialized<usize>::zeroed()` is initialized,
/// but `MaybeUninitialized<&'static i32>::zeroed()` is not because references must not
/// be null.
///
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
/// Note that dropping a `MaybeUninitialized` will never call `T`'s drop code.
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline]
pub fn zeroed() -> MaybeUninit<T> {
let mut u = MaybeUninit::<T>::uninitialized();
pub fn zeroed() -> MaybeUninitialized<T> {
let mut u = MaybeUninitialized::<T>::uninitialized();
unsafe {
u.as_mut_ptr().write_bytes(0u8, 1);
}
u
}

/// Set the value of the `MaybeUninit`. This overwrites any previous value without dropping it.
/// Set the value of the `MaybeUninitialized`. This overwrites any previous value without
/// dropping it.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub fn set(&mut self, val: T) {
Expand All @@ -1085,14 +1088,14 @@ impl<T> MaybeUninit<T> {
}
}

/// Extract the value from the `MaybeUninit` container. This is a great way
/// Extract the value from the `MaybeUninitialized` container. This is a great way
/// to ensure that the data will get dropped, because the resulting `T` is
/// subject to the usual drop handling.
///
/// # Unsafety
///
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
/// state, otherwise this will immediately cause undefined behavior.
/// It is up to the caller to guarantee that the `MaybeUninitialized` really is in an
/// initialized state, otherwise this will immediately cause undefined behavior.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub unsafe fn into_inner(self) -> T {
Expand All @@ -1103,8 +1106,8 @@ impl<T> MaybeUninit<T> {
///
/// # Unsafety
///
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
/// state, otherwise this will immediately cause undefined behavior.
/// It is up to the caller to guarantee that the `MaybeUninitialized` really is in an
/// initialized state, otherwise this will immediately cause undefined behavior.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub unsafe fn get_ref(&self) -> &T {
Expand All @@ -1115,8 +1118,8 @@ impl<T> MaybeUninit<T> {
///
/// # Unsafety
///
/// It is up to the caller to guarantee that the `MaybeUninit` really is in an initialized
/// state, otherwise this will immediately cause undefined behavior.
/// It is up to the caller to guarantee that the `MaybeUninitialized` really is in an
/// initialized state, otherwise this will immediately cause undefined behavior.
// FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references
// to uninitialized data (e.g., in `libcore/fmt/float.rs`). We should make
// a final decision about the rules before stabilization.
Expand All @@ -1127,15 +1130,15 @@ impl<T> MaybeUninit<T> {
}

/// Get a pointer to the contained value. Reading from this pointer will be undefined
/// behavior unless the `MaybeUninit` is initialized.
/// behavior unless the `MaybeUninitialized` is initialized.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub fn as_ptr(&self) -> *const T {
unsafe { &*self.value as *const T }
}

/// Get a mutable pointer to the contained value. Reading from this pointer will be undefined
/// behavior unless the `MaybeUninit` is initialized.
/// behavior unless the `MaybeUninitialized` is initialized.
#[unstable(feature = "maybe_uninit", issue = "53491")]
#[inline(always)]
pub fn as_mut_ptr(&mut self) -> *mut T {
Expand Down
14 changes: 7 additions & 7 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use ops::{CoerceUnsized, DispatchFromDyn};
use fmt;
use hash;
use marker::{PhantomData, Unsize};
use mem::{self, MaybeUninit};
use mem::{self, MaybeUninitialized};
use nonzero::NonZero;

use cmp::Ordering::{self, Less, Equal, Greater};
Expand Down Expand Up @@ -306,8 +306,8 @@ pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
// Give ourselves some scratch space to work with.
// We do not have to worry about drops: `MaybeUninit` does nothing when dropped.
let mut tmp = MaybeUninit::<T>::uninitialized();
// We do not have to worry about drops: `MaybeUninitialized` does nothing when dropped.
let mut tmp = MaybeUninitialized::<T>::uninitialized();

// Perform the swap
copy_nonoverlapping(x, tmp.as_mut_ptr(), 1);
Expand Down Expand Up @@ -399,7 +399,7 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {
while i + block_size <= len {
// Create some uninitialized memory as scratch space
// Declaring `t` here avoids aligning the stack when this loop is unused
let mut t = mem::MaybeUninit::<Block>::uninitialized();
let mut t = mem::MaybeUninitialized::<Block>::uninitialized();
let t = t.as_mut_ptr() as *mut u8;
let x = x.add(i);
let y = y.add(i);
Expand All @@ -414,7 +414,7 @@ unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) {

if i < len {
// Swap any remaining bytes
let mut t = mem::MaybeUninit::<UnalignedBlock>::uninitialized();
let mut t = mem::MaybeUninitialized::<UnalignedBlock>::uninitialized();
let rem = len - i;

let t = t.as_mut_ptr() as *mut u8;
Expand Down Expand Up @@ -582,7 +582,7 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn read<T>(src: *const T) -> T {
let mut tmp = MaybeUninit::<T>::uninitialized();
let mut tmp = MaybeUninitialized::<T>::uninitialized();
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
tmp.into_inner()
}
Expand Down Expand Up @@ -649,7 +649,7 @@ pub unsafe fn read<T>(src: *const T) -> T {
#[inline]
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
let mut tmp = MaybeUninit::<T>::uninitialized();
let mut tmp = MaybeUninitialized::<T>::uninitialized();
copy_nonoverlapping(src as *const u8,
tmp.as_mut_ptr() as *mut u8,
mem::size_of::<T>());
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/slice/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use cmp;
use mem::{self, MaybeUninit};
use mem::{self, MaybeUninitialized};
use ptr;

/// Rotation is much faster if it has access to a little bit of memory. This
Expand Down Expand Up @@ -82,7 +82,7 @@ pub unsafe fn ptr_rotate<T>(mut left: usize, mid: *mut T, mut right: usize) {
}
}

let mut rawarray = MaybeUninit::<RawArray<T>>::uninitialized();
let mut rawarray = MaybeUninitialized::<RawArray<T>>::uninitialized();
let buf = &mut (*rawarray.as_mut_ptr()).typed as *mut [T; 2] as *mut T;

let dim = mid.sub(left).add(right);
Expand Down
6 changes: 3 additions & 3 deletions src/libcore/slice/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! stable sorting implementation.

use cmp;
use mem::{self, MaybeUninit};
use mem::{self, MaybeUninitialized};
use ptr;

/// When dropped, copies from `src` into `dest`.
Expand Down Expand Up @@ -226,14 +226,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();
let mut offsets_l = MaybeUninit::<[u8; BLOCK]>::uninitialized();
let mut offsets_l = MaybeUninitialized::<[u8; BLOCK]>::uninitialized();

// 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]>::uninitialized();
let mut offsets_r = MaybeUninitialized::<[u8; BLOCK]>::uninitialized();

// 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.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ macro_rules! make_value_visitor {
},
layout::FieldPlacement::Arbitrary { ref offsets, .. } => {
// Special handling needed for generators: All but the first field
// (which is the state) are actually implicitly `MaybeUninit`, i.e.,
// (which is the state) are actually implicitly `MaybeUninitialized`, i.e.,
// they may or may not be initialized, so we cannot visit them.
match v.layout().ty.sty {
ty::Generator(..) => {
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sys/windows/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! detect recursive locks.

use cell::UnsafeCell;
use mem::{self, MaybeUninit};
use mem::{self, MaybeUninitialized};
use sync::atomic::{AtomicUsize, Ordering};
use sys::c;
use sys::compat;
Expand Down Expand Up @@ -157,14 +157,14 @@ fn kind() -> Kind {
return ret;
}

pub struct ReentrantMutex { inner: UnsafeCell<MaybeUninit<c::CRITICAL_SECTION>> }
pub struct ReentrantMutex { inner: UnsafeCell<MaybeUninitialized<c::CRITICAL_SECTION>> }

unsafe impl Send for ReentrantMutex {}
unsafe impl Sync for ReentrantMutex {}

impl ReentrantMutex {
pub fn uninitialized() -> ReentrantMutex {
ReentrantMutex { inner: UnsafeCell::new(MaybeUninit::uninitialized()) }
ReentrantMutex { inner: UnsafeCell::new(MaybeUninitialized::uninitialized()) }
}

pub unsafe fn init(&mut self) {
Expand Down
Loading