@@ -337,7 +337,7 @@ impl<T> MaybeUninit<T> {
337
337
/// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
338
338
/// unsafe {
339
339
/// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len());
340
- /// MaybeUninit::slice_assume_init_ref(& buf[..len])
340
+ /// buf[..len].assume_init_ref( )
341
341
/// }
342
342
/// }
343
343
///
@@ -956,48 +956,6 @@ impl<T> MaybeUninit<T> {
956
956
ret
957
957
}
958
958
959
- /// Assuming all the elements are initialized, get a slice to them.
960
- ///
961
- /// # Safety
962
- ///
963
- /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
964
- /// really are in an initialized state.
965
- /// Calling this when the content is not yet fully initialized causes undefined behavior.
966
- ///
967
- /// See [`assume_init_ref`] for more details and examples.
968
- ///
969
- /// [`assume_init_ref`]: MaybeUninit::assume_init_ref
970
- #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
971
- #[ rustc_const_unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
972
- #[ inline( always) ]
973
- pub const unsafe fn slice_assume_init_ref ( slice : & [ Self ] ) -> & [ T ] {
974
- // SAFETY: casting `slice` to a `*const [T]` is safe since the caller guarantees that
975
- // `slice` is initialized, and `MaybeUninit` is guaranteed to have the same layout as `T`.
976
- // The pointer obtained is valid since it refers to memory owned by `slice` which is a
977
- // reference and thus guaranteed to be valid for reads.
978
- unsafe { & * ( slice as * const [ Self ] as * const [ T ] ) }
979
- }
980
-
981
- /// Assuming all the elements are initialized, get a mutable slice to them.
982
- ///
983
- /// # Safety
984
- ///
985
- /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
986
- /// really are in an initialized state.
987
- /// Calling this when the content is not yet fully initialized causes undefined behavior.
988
- ///
989
- /// See [`assume_init_mut`] for more details and examples.
990
- ///
991
- /// [`assume_init_mut`]: MaybeUninit::assume_init_mut
992
- #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
993
- #[ rustc_const_unstable( feature = "const_maybe_uninit_assume_init" , issue = "none" ) ]
994
- #[ inline( always) ]
995
- pub const unsafe fn slice_assume_init_mut ( slice : & mut [ Self ] ) -> & mut [ T ] {
996
- // SAFETY: similar to safety notes for `slice_get_ref`, but we have a
997
- // mutable reference which is also guaranteed to be valid for writes.
998
- unsafe { & mut * ( slice as * mut [ Self ] as * mut [ T ] ) }
999
- }
1000
-
1001
959
/// Gets a pointer to the first element of the array.
1002
960
#[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
1003
961
#[ rustc_const_unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
@@ -1068,7 +1026,7 @@ impl<T> MaybeUninit<T> {
1068
1026
this. copy_from_slice ( uninit_src) ;
1069
1027
1070
1028
// SAFETY: Valid elements have just been copied into `this` so it is initialized
1071
- unsafe { MaybeUninit :: slice_assume_init_mut ( this) }
1029
+ unsafe { this. assume_init_mut ( ) }
1072
1030
}
1073
1031
1074
1032
/// Clones the elements from `src` to `this`, returning a mutable reference to the now initialized contents of `this`.
@@ -1136,7 +1094,7 @@ impl<T> MaybeUninit<T> {
1136
1094
// SAFETY: this raw slice will contain only initialized objects
1137
1095
// that's why, it is allowed to drop it.
1138
1096
unsafe {
1139
- crate :: ptr:: drop_in_place ( MaybeUninit :: slice_assume_init_mut ( initialized_part) ) ;
1097
+ crate :: ptr:: drop_in_place ( initialized_part. assume_init_mut ( ) ) ;
1140
1098
}
1141
1099
}
1142
1100
}
@@ -1159,7 +1117,7 @@ impl<T> MaybeUninit<T> {
1159
1117
super :: forget ( guard) ;
1160
1118
1161
1119
// SAFETY: Valid elements have just been written into `this` so it is initialized
1162
- unsafe { MaybeUninit :: slice_assume_init_mut ( this) }
1120
+ unsafe { this. assume_init_mut ( ) }
1163
1121
}
1164
1122
1165
1123
/// Returns the contents of this `MaybeUninit` as a slice of potentially uninitialized bytes.
@@ -1176,7 +1134,7 @@ impl<T> MaybeUninit<T> {
1176
1134
/// let val = 0x12345678i32;
1177
1135
/// let uninit = MaybeUninit::new(val);
1178
1136
/// let uninit_bytes = uninit.as_bytes();
1179
- /// let bytes = unsafe { MaybeUninit::slice_assume_init_ref( uninit_bytes) };
1137
+ /// let bytes = unsafe { uninit_bytes.assume_init_ref( ) };
1180
1138
/// assert_eq!(bytes, val.to_ne_bytes());
1181
1139
/// ```
1182
1140
#[ unstable( feature = "maybe_uninit_as_bytes" , issue = "93092" ) ]
@@ -1235,7 +1193,7 @@ impl<T> MaybeUninit<T> {
1235
1193
///
1236
1194
/// let uninit = [MaybeUninit::new(0x1234u16), MaybeUninit::new(0x5678u16)];
1237
1195
/// let uninit_bytes = MaybeUninit::slice_as_bytes(&uninit);
1238
- /// let bytes = unsafe { MaybeUninit::slice_assume_init_ref(& uninit_bytes) };
1196
+ /// let bytes = unsafe { uninit_bytes.assume_init_ref( ) };
1239
1197
/// let val1 = u16::from_ne_bytes(bytes[0..2].try_into().unwrap());
1240
1198
/// let val2 = u16::from_ne_bytes(bytes[2..4].try_into().unwrap());
1241
1199
/// assert_eq!(&[val1, val2], &[0x1234u16, 0x5678u16]);
@@ -1266,7 +1224,7 @@ impl<T> MaybeUninit<T> {
1266
1224
/// let mut uninit = [MaybeUninit::<u16>::uninit(), MaybeUninit::<u16>::uninit()];
1267
1225
/// let uninit_bytes = MaybeUninit::slice_as_bytes_mut(&mut uninit);
1268
1226
/// MaybeUninit::write_slice(uninit_bytes, &[0x12, 0x34, 0x56, 0x78]);
1269
- /// let vals = unsafe { MaybeUninit::slice_assume_init_ref(& uninit) };
1227
+ /// let vals = unsafe { uninit.assume_init_ref( ) };
1270
1228
/// if cfg!(target_endian = "little") {
1271
1229
/// assert_eq!(vals, &[0x3412u16, 0x7856u16]);
1272
1230
/// } else {
@@ -1321,3 +1279,47 @@ impl<T, const N: usize> [MaybeUninit<T>; N] {
1321
1279
unsafe { super :: transmute_copy ( & ManuallyDrop :: new ( self ) ) }
1322
1280
}
1323
1281
}
1282
+
1283
+ impl < T > [ MaybeUninit < T > ] {
1284
+ /// Assuming all the elements are initialized, get a slice to them.
1285
+ ///
1286
+ /// # Safety
1287
+ ///
1288
+ /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
1289
+ /// really are in an initialized state.
1290
+ /// Calling this when the content is not yet fully initialized causes undefined behavior.
1291
+ ///
1292
+ /// See [`assume_init_ref`] for more details and examples.
1293
+ ///
1294
+ /// [`assume_init_ref`]: MaybeUninit::assume_init_ref
1295
+ #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
1296
+ #[ rustc_const_unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
1297
+ #[ inline( always) ]
1298
+ pub const unsafe fn assume_init_ref ( & self ) -> & [ T ] {
1299
+ // SAFETY: casting `slice` to a `*const [T]` is safe since the caller guarantees that
1300
+ // `slice` is initialized, and `MaybeUninit` is guaranteed to have the same layout as `T`.
1301
+ // The pointer obtained is valid since it refers to memory owned by `slice` which is a
1302
+ // reference and thus guaranteed to be valid for reads.
1303
+ unsafe { & * ( self as * const [ MaybeUninit < T > ] as * const [ T ] ) }
1304
+ }
1305
+
1306
+ /// Assuming all the elements are initialized, get a mutable slice to them.
1307
+ ///
1308
+ /// # Safety
1309
+ ///
1310
+ /// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
1311
+ /// really are in an initialized state.
1312
+ /// Calling this when the content is not yet fully initialized causes undefined behavior.
1313
+ ///
1314
+ /// See [`assume_init_mut`] for more details and examples.
1315
+ ///
1316
+ /// [`assume_init_mut`]: MaybeUninit::assume_init_mut
1317
+ #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
1318
+ #[ rustc_const_unstable( feature = "const_maybe_uninit_assume_init" , issue = "none" ) ]
1319
+ #[ inline( always) ]
1320
+ pub const unsafe fn assume_init_mut ( & mut self ) -> & mut [ T ] {
1321
+ // SAFETY: similar to safety notes for `slice_get_ref`, but we have a
1322
+ // mutable reference which is also guaranteed to be valid for writes.
1323
+ unsafe { & mut * ( self as * mut [ MaybeUninit < T > ] as * mut [ T ] ) }
1324
+ }
1325
+ }
0 commit comments