Skip to content

Commit 166f664

Browse files
authored
Rollup merge of #102023 - SUPERCILEX:maybeuninit-transpose, r=scottmcm
Add MaybeUninit array transpose From impls See discussion in #101179 and #96097. I believe this solution offers the simplest implementation with minimal future API regret. `@RalfJung` mind doing a correctness review?
2 parents cbc0a73 + 393434c commit 166f664

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

library/core/src/mem/maybe_uninit.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1284,3 +1284,40 @@ impl<T> MaybeUninit<T> {
12841284
}
12851285
}
12861286
}
1287+
1288+
impl<T, const N: usize> MaybeUninit<[T; N]> {
1289+
/// Transposes a `MaybeUninit<[T; N]>` into a `[MaybeUninit<T>; N]`.
1290+
///
1291+
/// # Examples
1292+
///
1293+
/// ```
1294+
/// #![feature(maybe_uninit_uninit_array_transpose)]
1295+
/// # use std::mem::MaybeUninit;
1296+
///
1297+
/// let data: [MaybeUninit<u8>; 1000] = MaybeUninit::uninit().transpose();
1298+
/// ```
1299+
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")]
1300+
pub fn transpose(self) -> [MaybeUninit<T>; N] {
1301+
// SAFETY: T and MaybeUninit<T> have the same layout
1302+
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
1303+
}
1304+
}
1305+
1306+
impl<T, const N: usize> [MaybeUninit<T>; N] {
1307+
/// Transposes a `[MaybeUninit<T>; N]` into a `MaybeUninit<[T; N]>`.
1308+
///
1309+
/// # Examples
1310+
///
1311+
/// ```
1312+
/// #![feature(maybe_uninit_uninit_array_transpose)]
1313+
/// # use std::mem::MaybeUninit;
1314+
///
1315+
/// let data = [MaybeUninit::<u8>::uninit(); 1000];
1316+
/// let data: MaybeUninit<[u8; 1000]> = data.transpose();
1317+
/// ```
1318+
#[unstable(feature = "maybe_uninit_uninit_array_transpose", issue = "96097")]
1319+
pub fn transpose(self) -> MaybeUninit<[T; N]> {
1320+
// SAFETY: T and MaybeUninit<T> have the same layout
1321+
unsafe { super::transmute_copy(&ManuallyDrop::new(self)) }
1322+
}
1323+
}

0 commit comments

Comments
 (0)