@@ -514,6 +514,7 @@ pub fn needs_drop<T>() -> bool {
514
514
/// assert_eq!(0, x);
515
515
/// ```
516
516
#[ inline]
517
+ #[ rustc_deprecated( since = "1.30.0" , reason = "use `mem::MaybeUninit::zeroed` instead" ) ]
517
518
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
518
519
pub unsafe fn zeroed < T > ( ) -> T {
519
520
intrinsics:: init ( )
@@ -608,6 +609,7 @@ pub unsafe fn zeroed<T>() -> T {
608
609
/// [copy_no]: ../intrinsics/fn.copy_nonoverlapping.html
609
610
/// [`Drop`]: ../ops/trait.Drop.html
610
611
#[ inline]
612
+ #[ rustc_deprecated( since = "1.30.0" , reason = "use `mem::MaybeUninit::uninitialized` instead" ) ]
611
613
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
612
614
pub unsafe fn uninitialized < T > ( ) -> T {
613
615
intrinsics:: uninit ( )
@@ -1024,3 +1026,96 @@ impl<T: ?Sized> DerefMut for ManuallyDrop<T> {
1024
1026
& mut self . value
1025
1027
}
1026
1028
}
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