diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2022-10-16 11:41:12 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-16 11:41:12 +0900 |
| commit | 166f66403746ffc8575c43819db9462ad485a96c (patch) | |
| tree | 2536ff428185852b800a8999f5b7bfa290b4e56f /library/core | |
| parent | cbc0a73c951128b783ea4953844ef5beb6e9cc5a (diff) | |
| parent | 393434c29eee2aa939dddfb8db0a4d692bcb96c0 (diff) | |
| download | rust-166f66403746ffc8575c43819db9462ad485a96c.tar.gz rust-166f66403746ffc8575c43819db9462ad485a96c.zip | |
Rollup merge of #102023 - SUPERCILEX:maybeuninit-transpose, r=scottmcm
Add MaybeUninit array transpose From impls See discussion in https://github.com/rust-lang/rust/pull/101179 and https://github.com/rust-lang/rust/issues/96097. I believe this solution offers the simplest implementation with minimal future API regret. `@RalfJung` mind doing a correctness review?
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/mem/maybe_uninit.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index e2ae39fbab3..2ae96367628 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -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)) } + } +} |
