diff options
| author | CoffeeBlend <jonasboettiger@icloud.com> | 2021-01-11 23:32:03 +0100 |
|---|---|---|
| committer | CoffeeBlend <jonasboettiger@icloud.com> | 2021-01-11 23:32:03 +0100 |
| commit | 5d65b7e055f469ee5bcc733860c6ebe37649bfa5 (patch) | |
| tree | 95a0d713a60e5ddfd5a953f909bf789456f79018 | |
| parent | dec8c033a3d3287cdb9e0b9f4888b21bbb86d60c (diff) | |
| download | rust-5d65b7e055f469ee5bcc733860c6ebe37649bfa5.tar.gz rust-5d65b7e055f469ee5bcc733860c6ebe37649bfa5.zip | |
Simplify array_assume_init
| -rw-r--r-- | library/core/src/mem/maybe_uninit.rs | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 3d96ad32a12..b186aa61eb0 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -833,21 +833,14 @@ impl<T> MaybeUninit<T> { #[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")] #[inline(always)] pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] { - // Convert using a union because mem::transmute does not support const_generics - union ArrayInit<T, const N: usize> { - maybe_uninit: ManuallyDrop<[MaybeUninit<T>; N]>, - init: ManuallyDrop<[T; N]>, - } - // SAFETY: - // * The caller guarantees that all elements of the array are initialized, - // * `MaybeUninit<T>` and T are guaranteed to have the same layout, - // Therefore the conversion is safe + // * The caller guarantees that all elements of the array are initialized + // * `MaybeUninit<T>` and T are guaranteed to have the same layout + // * MaybeUnint does not drop, so there are no double-frees + // And thus the conversion is safe unsafe { intrinsics::assert_inhabited::<T>(); - - let array = ArrayInit { maybe_uninit: ManuallyDrop::new(array) }; - ManuallyDrop::into_inner(array.init) + (&array as *const _ as *const T).read() } } |
