Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MaybeUninit array transpose impls #102023

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
}
}