Skip to content

Commit f559cf9

Browse files
authored
Rollup merge of rust-lang#96099 - clarfonthey:maybeuninit_array_cleanup, r=dtolnay
MaybeUninit array cleanup * Links `MaybeUninit::uninit_array` to meta-tracking issue * Links `MaybeUninit::array_assume_init` to meta-tracking issue * Unstably constifies `MaybeUninit::array_assume_init` Another thing worth mentioning: this splits the const feature flag for `maybe_uninit_uninit_array` into `const_maybe_uninit_uninit_array` to avoid weird cases where only one gets stabilised. Note that it may be desired to keep the `array_assume_init` method linked to its dedicated issue, but at least for now, I decided to link to the meta-tracking issue so that all of the methods lead users to the same place. But I can revert that bit if desired. The meta-tracking issue that I filed is rust-lang#96097.
2 parents 04ccba8 + 63a8652 commit f559cf9

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
#![feature(const_intrinsic_copy)]
118118
#![feature(const_intrinsic_forget)]
119119
#![feature(const_likely)]
120+
#![feature(const_maybe_uninit_uninit_array)]
120121
#![feature(const_maybe_uninit_as_mut_ptr)]
121122
#![feature(const_maybe_uninit_assume_init)]
122123
#![feature(const_num_from_num)]

library/core/src/mem/maybe_uninit.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,13 @@ impl<T> MaybeUninit<T> {
350350
/// let mut buf: [MaybeUninit<u8>; 32] = MaybeUninit::uninit_array();
351351
/// let data = read(&mut buf);
352352
/// ```
353-
#[unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
354-
#[rustc_const_unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
353+
#[unstable(feature = "maybe_uninit_uninit_array", issue = "96097")]
354+
#[rustc_const_unstable(feature = "const_maybe_uninit_uninit_array", issue = "96097")]
355355
#[must_use]
356356
#[inline(always)]
357-
pub const fn uninit_array<const LEN: usize>() -> [Self; LEN] {
357+
pub const fn uninit_array<const N: usize>() -> [Self; N] {
358358
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
359-
unsafe { MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init() }
359+
unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
360360
}
361361

362362
/// Creates a new `MaybeUninit<T>` in an uninitialized state, with the memory being
@@ -942,19 +942,24 @@ impl<T> MaybeUninit<T> {
942942
///
943943
/// assert_eq!(array, [0, 1, 2]);
944944
/// ```
945-
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
945+
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "96097")]
946+
#[rustc_const_unstable(feature = "const_maybe_uninit_array_assume_init", issue = "96097")]
946947
#[inline(always)]
947948
#[track_caller]
948-
pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
949+
pub const unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
949950
// SAFETY:
950951
// * The caller guarantees that all elements of the array are initialized
951952
// * `MaybeUninit<T>` and T are guaranteed to have the same layout
952953
// * `MaybeUninit` does not drop, so there are no double-frees
953954
// And thus the conversion is safe
954-
unsafe {
955+
let ret = unsafe {
955956
intrinsics::assert_inhabited::<[T; N]>();
956957
(&array as *const _ as *const [T; N]).read()
957-
}
958+
};
959+
960+
// FIXME: required to avoid `~const Destruct` bound
961+
super::forget(array);
962+
ret
958963
}
959964

960965
/// Assuming all the elements are initialized, get a slice to them.

0 commit comments

Comments
 (0)