Skip to content

Commit 7bb5b3e

Browse files
committed
add MaybeUninit and deprecate mem::{uninitialized,zeroed}
1 parent af50e38 commit 7bb5b3e

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/libcore/mem.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ pub fn needs_drop<T>() -> bool {
514514
/// assert_eq!(0, x);
515515
/// ```
516516
#[inline]
517+
#[rustc_deprecated(since = "1.30.0", reason = "use `mem::MaybeUninit::zeroed` instead")]
517518
#[stable(feature = "rust1", since = "1.0.0")]
518519
pub unsafe fn zeroed<T>() -> T {
519520
intrinsics::init()
@@ -608,6 +609,7 @@ pub unsafe fn zeroed<T>() -> T {
608609
/// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html
609610
/// [`Drop`]: ../ops/trait.Drop.html
610611
#[inline]
612+
#[rustc_deprecated(since = "1.30.0", reason = "use `mem::MaybeUninit::uninitialized` instead")]
611613
#[stable(feature = "rust1", since = "1.0.0")]
612614
pub unsafe fn uninitialized<T>() -> T {
613615
intrinsics::uninit()
@@ -1024,3 +1026,96 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
10241026
&mut self.value
10251027
}
10261028
}
1029+
1030+
/// A newtype to construct uninitialized instances of `T`
1031+
#[allow(missing_debug_implementations)]
1032+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1033+
pub union MaybeUninit<T> {
1034+
uninit: (),
1035+
value: ManuallyDrop<T>,
1036+
}
1037+
1038+
impl<T> MaybeUninit<T> {
1039+
/// Create a new `MaybeUninit` in an uninitialized state.
1040+
///
1041+
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
1042+
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
1043+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1044+
pub const fn uninitialized() -> MaybeUninit<T> {
1045+
MaybeUninit { uninit: () }
1046+
}
1047+
1048+
/// Create a new `MaybeUninit` in an uninitialized state, with the memory being
1049+
/// filled with `0` bytes. It depends on `T` whether that already makes for
1050+
/// proper initialization. For example, `MaybeUninit<usize>::zeroed()` is initialized,
1051+
/// but `MaybeUninit<&'static i32>::zeroed()` is not because references must not
1052+
/// be null.
1053+
///
1054+
/// Note that dropping a `MaybeUninit` will never call `T`'s drop code.
1055+
/// It is your responsibility to make sure `T` gets dropped if it got initialized.
1056+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1057+
pub fn zeroed() -> MaybeUninit<T> {
1058+
let mut u = MaybeUninit::<T>::uninitialized();
1059+
unsafe {
1060+
u.as_mut_ptr().write_bytes(0u8, 1);
1061+
}
1062+
u
1063+
}
1064+
1065+
/// Set the value of the `MaybeUninit`. This overwrites any previous value without dropping it.
1066+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1067+
pub fn set(&mut self, val: T) {
1068+
unsafe {
1069+
self.value = ManuallyDrop::new(val);
1070+
}
1071+
}
1072+
1073+
/// Extract the value from the `MaybeUninit` container. This is a great way
1074+
/// to ensure that the data will get dropped, because the resulting `T` is
1075+
/// subject to the usual drop handling.
1076+
///
1077+
/// # Unsafety
1078+
///
1079+
/// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
1080+
/// state, otherwise this will immediately cause undefined behavior.
1081+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1082+
pub unsafe fn into_inner(self) -> T {
1083+
ManuallyDrop::into_inner(self.value)
1084+
}
1085+
1086+
/// Get a reference to the contained value.
1087+
///
1088+
/// # Unsafety
1089+
///
1090+
/// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
1091+
/// state, otherwise this will immediately cause undefined behavior.
1092+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1093+
pub unsafe fn get_ref(&self) -> &T {
1094+
&*self.value
1095+
}
1096+
1097+
/// Get a mutable reference to the contained value.
1098+
///
1099+
/// # Unsafety
1100+
///
1101+
/// It is up to the caller to guarantee that the the `MaybeUninit` really is in an initialized
1102+
/// state, otherwise this will immediately cause undefined behavior.
1103+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1104+
pub unsafe fn get_mut(&mut self) -> &mut T {
1105+
&mut *self.value
1106+
}
1107+
1108+
/// Get a pointer to the contained value. Reading from this pointer will be undefined
1109+
/// behavior unless the `MaybeUninit` is initialized.
1110+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1111+
pub fn as_ptr(&self) -> *const T {
1112+
unsafe { &*self.value as *const T }
1113+
}
1114+
1115+
/// Get a mutable pointer to the contained value. Reading from this pointer will be undefined
1116+
/// behavior unless the `MaybeUninit` is initialized.
1117+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1118+
pub fn as_mut_ptr(&mut self) -> *mut T {
1119+
unsafe { &mut *self.value as *mut T }
1120+
}
1121+
}

0 commit comments

Comments
 (0)