From 9eabce8b14e8f6a105b5a585760b367775259d4e Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Mon, 10 Oct 2022 00:04:56 +0000 Subject: [PATCH] Make Unalign::into_inner const --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 18ee496adf..59ab3cae0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,7 +133,7 @@ use core::{ cmp::Ordering, fmt::{self, Debug, Display, Formatter}, marker::PhantomData, - mem::{self, MaybeUninit}, + mem::{self, ManuallyDrop, MaybeUninit}, num::{ NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, Wrapping, @@ -863,9 +863,29 @@ impl Unalign { } /// Consumes `self`, returning the inner `T`. - pub fn into_inner(self) -> T { - let Unalign(val) = self; - val + pub const fn into_inner(self) -> T { + // Use this instead of `mem::transmute` since the latter can't tell + // that `Unalign` and `T` have the same size. + #[repr(C)] + union Transmute { + u: ManuallyDrop>, + t: ManuallyDrop, + } + + // SAFETY: Since `Unalign` is `#[repr(C, packed)]`, it has the same + // layout as `T`. `ManuallyDrop` is guaranteed to have the same + // layout as `U`, and so `ManuallyDrop>` has the same layout + // as `ManuallyDrop`. Since `Transmute` is `#[repr(C)]`, its `t` + // and `u` fields both start at the same offset (namely, 0) within the + // union. + // + // We do this instead of just destructuring in order to prevent + // `Unalign`'s `Drop::drop` from being run, since dropping is not + // supported in `const fn`s. + // + // TODO(https://github.com/rust-lang/rust/issues/73255): Destructure + // instead of using unsafe. + unsafe { ManuallyDrop::into_inner(Transmute { u: ManuallyDrop::new(self) }.t) } } /// Gets an unaligned raw pointer to the inner `T`. @@ -2465,6 +2485,14 @@ mod tests { fn _takes_unaligned(_: &dyn Unaligned) {} } + #[test] + fn test_unalign_const() { + // Test that some `Unalign` functions and methods are `const`. + const _UNALIGN: Unalign = Unalign::new(0); + const _UNALIGN_PTR: *const u64 = _UNALIGN.get_ptr(); + const _U64: u64 = _UNALIGN.into_inner(); + } + #[test] fn test_read_write() { const VAL: u64 = 0x12345678;