Skip to content

Commit

Permalink
Rollup merge of rust-lang#102023 - SUPERCILEX:maybeuninit-transpose, …
Browse files Browse the repository at this point in the history
…r=scottmcm

Add MaybeUninit array transpose From impls

See discussion in rust-lang#101179 and rust-lang#96097. I believe this solution offers the simplest implementation with minimal future API regret.

`@RalfJung` mind doing a correctness review?
  • Loading branch information
JohnTitor authored Oct 16, 2022
2 parents cbc0a73 + 393434c commit 166f664
Showing 1 changed file with 37 additions and 0 deletions.
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)) }
}
}

0 comments on commit 166f664

Please sign in to comment.