From 8e68496a3a21677b09cf99166aeb7a28f4253072 Mon Sep 17 00:00:00 2001 From: Alex Saveau Date: Sun, 16 Oct 2022 21:01:28 -0700 Subject: [PATCH] Kill array_assume_init Signed-off-by: Alex Saveau --- library/alloc/src/lib.rs | 2 +- library/alloc/src/vec/into_iter.rs | 4 +-- library/core/src/array/iter.rs | 5 ++- library/core/src/array/mod.rs | 2 +- library/core/src/mem/maybe_uninit.rs | 46 ---------------------------- library/core/tests/lib.rs | 2 +- library/core/tests/mem.rs | 6 ++-- 7 files changed, 10 insertions(+), 57 deletions(-) diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 7fde8f670a231..ce36b116f139b 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -125,9 +125,9 @@ #![feature(iter_advance_by)] #![feature(iter_next_chunk)] #![feature(layout_for_ptr)] -#![feature(maybe_uninit_array_assume_init)] #![feature(maybe_uninit_slice)] #![feature(maybe_uninit_uninit_array)] +#![feature(maybe_uninit_uninit_array_transpose)] #![cfg_attr(test, feature(new_uninit))] #![feature(nonnull_slice_from_raw_parts)] #![feature(pattern)] diff --git a/library/alloc/src/vec/into_iter.rs b/library/alloc/src/vec/into_iter.rs index 73d7c90cf78ec..02cc7691a82db 100644 --- a/library/alloc/src/vec/into_iter.rs +++ b/library/alloc/src/vec/into_iter.rs @@ -223,7 +223,7 @@ impl Iterator for IntoIter { self.ptr = self.ptr.wrapping_byte_add(N); // Safety: ditto - return Ok(unsafe { MaybeUninit::array_assume_init(raw_ary) }); + return Ok(unsafe { raw_ary.transpose().assume_init() }); } if len < N { @@ -241,7 +241,7 @@ impl Iterator for IntoIter { return unsafe { ptr::copy_nonoverlapping(self.ptr, raw_ary.as_mut_ptr() as *mut T, N); self.ptr = self.ptr.add(N); - Ok(MaybeUninit::array_assume_init(raw_ary)) + Ok(raw_ary.transpose().assume_init()) }; } diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index b3b2604006718..b91c630183d4f 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -104,8 +104,7 @@ impl IntoIter { /// /// ``` /// #![feature(array_into_iter_constructors)] - /// - /// #![feature(maybe_uninit_array_assume_init)] + /// #![feature(maybe_uninit_uninit_array_transpose)] /// #![feature(maybe_uninit_uninit_array)] /// use std::array::IntoIter; /// use std::mem::MaybeUninit; @@ -134,7 +133,7 @@ impl IntoIter { /// } /// /// // SAFETY: We've initialized all N items - /// unsafe { Ok(MaybeUninit::array_assume_init(buffer)) } + /// unsafe { Ok(buffer.transpose().assume_init()) } /// } /// /// let r: [_; 4] = next_chunk(&mut (10..16)).unwrap(); diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index b82bbf2267a96..04dd821efdec0 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -912,7 +912,7 @@ where mem::forget(guard); // SAFETY: All elements of the array were populated in the loop above. - let output = unsafe { MaybeUninit::array_assume_init(array) }; + let output = unsafe { array.transpose().assume_init() }; Ok(Try::from_output(output)) } diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 2ae9636762813..0feb1032c4e1f 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -910,52 +910,6 @@ impl MaybeUninit { } } - /// Extracts the values from an array of `MaybeUninit` containers. - /// - /// # Safety - /// - /// It is up to the caller to guarantee that all elements of the array are - /// in an initialized state. - /// - /// # Examples - /// - /// ``` - /// #![feature(maybe_uninit_uninit_array)] - /// #![feature(maybe_uninit_array_assume_init)] - /// use std::mem::MaybeUninit; - /// - /// let mut array: [MaybeUninit; 3] = MaybeUninit::uninit_array(); - /// array[0].write(0); - /// array[1].write(1); - /// array[2].write(2); - /// - /// // SAFETY: Now safe as we initialised all elements - /// let array = unsafe { - /// MaybeUninit::array_assume_init(array) - /// }; - /// - /// assert_eq!(array, [0, 1, 2]); - /// ``` - #[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 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 - 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. /// /// # Safety diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index b5c34f5df3b76..b1f492381b136 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -49,8 +49,8 @@ #![feature(slice_from_ptr_range)] #![feature(split_as_slice)] #![feature(maybe_uninit_uninit_array)] -#![feature(maybe_uninit_array_assume_init)] #![feature(maybe_uninit_write_slice)] +#![feature(maybe_uninit_uninit_array_transpose)] #![feature(min_specialization)] #![feature(numfmt)] #![feature(step_trait)] diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 6856d1a1f51ae..b0cc08a15f0a0 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -163,18 +163,18 @@ fn assume_init_good() { #[test] fn uninit_array_assume_init() { - let mut array: [MaybeUninit; 5] = MaybeUninit::uninit_array(); + let mut array = [MaybeUninit::::uninit(); 5]; array[0].write(3); array[1].write(1); array[2].write(4); array[3].write(1); array[4].write(5); - let array = unsafe { MaybeUninit::array_assume_init(array) }; + let array = unsafe { array.transpose().assume_init() }; assert_eq!(array, [3, 1, 4, 1, 5]); - let [] = unsafe { MaybeUninit::::array_assume_init([]) }; + let [] = unsafe { [MaybeUninit::::uninit(); 0].transpose().assume_init() }; } #[test]