Add MaybeUninit array transpose impls

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
This commit is contained in:
Alex Saveau 2022-10-15 15:57:19 -07:00
parent 8147e6e427
commit 393434c29e
No known key found for this signature in database
GPG Key ID: 3F8D5B16EB169D48

View File

@ -1284,3 +1284,40 @@ impl<T> MaybeUninit<T> {
}
}
}
impl<T, const N: usize> MaybeUninit<[T; N]> {
/// Transposes a `MaybeUninit<[T; N]>` into a `[MaybeUninit<T>; N]`.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_uninit_array_transpose)]
/// # use std::mem::MaybeUninit;
///
/// let data: [MaybeUninit<u8>; 1000] = MaybeUninit::uninit().transpose();
/// ```
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")]
pub fn transpose(self) -> [MaybeUninit<T>; N] {
// SAFETY: T and MaybeUninit<T> have the same layout
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
}
}
impl<T, const N: usize> [MaybeUninit<T>; N] {
/// Transposes a `[MaybeUninit<T>; N]` into a `MaybeUninit<[T; N]>`.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_uninit_array_transpose)]
/// # use std::mem::MaybeUninit;
///
/// let data = [MaybeUninit::<u8>::uninit(); 1000];
/// let data: MaybeUninit<[u8; 1000]> = data.transpose();
/// ```
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")]
pub fn transpose(self) -> MaybeUninit<[T; N]> {
// SAFETY: T and MaybeUninit<T> have the same layout
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
}
}