@@ -337,7 +337,7 @@ impl<T> MaybeUninit<T> {
337337 /// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
338338 /// unsafe {
339339 /// 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( )
341341 /// }
342342 /// }
343343 ///
@@ -956,48 +956,6 @@ impl<T> MaybeUninit<T> {
956956 ret
957957 }
958958
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-
1001959 /// Gets a pointer to the first element of the array.
1002960 #[ unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
1003961 #[ rustc_const_unstable( feature = "maybe_uninit_slice" , issue = "63569" ) ]
@@ -1068,7 +1026,7 @@ impl<T> MaybeUninit<T> {
10681026 this. copy_from_slice ( uninit_src) ;
10691027
10701028 // 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 ( ) }
10721030 }
10731031
10741032 /// 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> {
11361094 // SAFETY: this raw slice will contain only initialized objects
11371095 // that's why, it is allowed to drop it.
11381096 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 ( ) ) ;
11401098 }
11411099 }
11421100 }
@@ -1159,7 +1117,7 @@ impl<T> MaybeUninit<T> {
11591117 super :: forget ( guard) ;
11601118
11611119 // 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 ( ) }
11631121 }
11641122
11651123 /// Returns the contents of this `MaybeUninit` as a slice of potentially uninitialized bytes.
@@ -1176,7 +1134,7 @@ impl<T> MaybeUninit<T> {
11761134 /// let val = 0x12345678i32;
11771135 /// let uninit = MaybeUninit::new(val);
11781136 /// 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( ) };
11801138 /// assert_eq!(bytes, val.to_ne_bytes());
11811139 /// ```
11821140 #[ unstable( feature = "maybe_uninit_as_bytes" , issue = "93092" ) ]
@@ -1235,7 +1193,7 @@ impl<T> MaybeUninit<T> {
12351193 ///
12361194 /// let uninit = [MaybeUninit::new(0x1234u16), MaybeUninit::new(0x5678u16)];
12371195 /// 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( ) };
12391197 /// let val1 = u16::from_ne_bytes(bytes[0..2].try_into().unwrap());
12401198 /// let val2 = u16::from_ne_bytes(bytes[2..4].try_into().unwrap());
12411199 /// assert_eq!(&[val1, val2], &[0x1234u16, 0x5678u16]);
@@ -1266,7 +1224,7 @@ impl<T> MaybeUninit<T> {
12661224 /// let mut uninit = [MaybeUninit::<u16>::uninit(), MaybeUninit::<u16>::uninit()];
12671225 /// let uninit_bytes = MaybeUninit::slice_as_bytes_mut(&mut uninit);
12681226 /// 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( ) };
12701228 /// if cfg!(target_endian = "little") {
12711229 /// assert_eq!(vals, &[0x3412u16, 0x7856u16]);
12721230 /// } else {
@@ -1321,3 +1279,47 @@ impl<T, const N: usize> [MaybeUninit<T>; N] {
13211279 unsafe { super :: transmute_copy ( & ManuallyDrop :: new ( self ) ) }
13221280 }
13231281}
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