mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 15:01:51 +00:00
Add MaybeUninit array transpose impls
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
This commit is contained in:
parent
8147e6e427
commit
393434c29e
@ -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)) }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user