From 63a865296141fb883352f30c603d46299d49e7c1 Mon Sep 17 00:00:00 2001 From: ltdk Date: Fri, 15 Apr 2022 20:53:50 -0400 Subject: [PATCH] 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 --- library/core/src/lib.rs | 1 + library/core/src/mem/maybe_uninit.rs | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 8aa355a554d..dafd07b1617 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -117,6 +117,7 @@ #![feature(const_intrinsic_copy)] #![feature(const_intrinsic_forget)] #![feature(const_likely)] +#![feature(const_maybe_uninit_uninit_array)] #![feature(const_maybe_uninit_as_mut_ptr)] #![feature(const_maybe_uninit_assume_init)] #![feature(const_num_from_num)] diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 58d682fc4c8..cac7f435573 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -350,13 +350,13 @@ impl MaybeUninit { /// let mut buf: [MaybeUninit; 32] = MaybeUninit::uninit_array(); /// let data = read(&mut buf); /// ``` - #[unstable(feature = "maybe_uninit_uninit_array", issue = "none")] - #[rustc_const_unstable(feature = "maybe_uninit_uninit_array", issue = "none")] + #[unstable(feature = "maybe_uninit_uninit_array", issue = "96097")] + #[rustc_const_unstable(feature = "const_maybe_uninit_uninit_array", issue = "96097")] #[must_use] #[inline(always)] - pub const fn uninit_array() -> [Self; LEN] { + pub const fn uninit_array() -> [Self; N] { // SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid. - unsafe { MaybeUninit::<[MaybeUninit; LEN]>::uninit().assume_init() } + unsafe { MaybeUninit::<[MaybeUninit; N]>::uninit().assume_init() } } /// Creates a new `MaybeUninit` in an uninitialized state, with the memory being @@ -942,19 +942,24 @@ impl MaybeUninit { /// /// assert_eq!(array, [0, 1, 2]); /// ``` - #[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")] + #[unstable(feature = "maybe_uninit_array_assume_init", issue = "96097")] + #[rustc_const_unstable(feature = "const_maybe_uninit_array_assume_init", issue = "96097")] #[inline(always)] #[track_caller] - pub unsafe fn array_assume_init(array: [Self; N]) -> [T; N] { + pub const unsafe fn array_assume_init(array: [Self; N]) -> [T; N] { // SAFETY: // * The caller guarantees that all elements of the array are initialized // * `MaybeUninit` and T are guaranteed to have the same layout // * `MaybeUninit` does not drop, so there are no double-frees // And thus the conversion is safe - unsafe { + let ret = unsafe { intrinsics::assert_inhabited::<[T; N]>(); (&array as *const _ as *const [T; N]).read() - } + }; + + // FIXME: required to avoid `~const Destruct` bound + super::forget(array); + ret } /// Assuming all the elements are initialized, get a slice to them.