Skip to content

Commit 5d65b7e

Browse files
committed
Simplify array_assume_init
1 parent dec8c03 commit 5d65b7e

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

library/core/src/mem/maybe_uninit.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -833,21 +833,14 @@ impl<T> MaybeUninit<T> {
833833
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
834834
#[inline(always)]
835835
pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
836-
// Convert using a union because mem::transmute does not support const_generics
837-
union ArrayInit<T, const N: usize> {
838-
maybe_uninit: ManuallyDrop<[MaybeUninit<T>; N]>,
839-
init: ManuallyDrop<[T; N]>,
840-
}
841-
842836
// SAFETY:
843-
// * The caller guarantees that all elements of the array are initialized,
844-
// * `MaybeUninit<T>` and T are guaranteed to have the same layout,
845-
// Therefore the conversion is safe
837+
// * The caller guarantees that all elements of the array are initialized
838+
// * `MaybeUninit<T>` and T are guaranteed to have the same layout
839+
// * MaybeUnint does not drop, so there are no double-frees
840+
// And thus the conversion is safe
846841
unsafe {
847842
intrinsics::assert_inhabited::<T>();
848-
849-
let array = ArrayInit { maybe_uninit: ManuallyDrop::new(array) };
850-
ManuallyDrop::into_inner(array.init)
843+
(&array as *const _ as *const T).read()
851844
}
852845
}
853846

0 commit comments

Comments
 (0)