From 3f42bec5393f586189459e2db03b39f8f9438997 Mon Sep 17 00:00:00 2001 From: Lukasz Anforowicz Date: Tue, 27 Aug 2024 09:30:57 -0700 Subject: [PATCH] Force `#[repr(C)]` layout to guarantee same offset of `union` fields. (#268) https://rust-lang.github.io/unsafe-code-guidelines/layout/unions.html points out that > [...] the default layout of Rust unions is, in general, > unspecified. > > That is, there are no general guarantees about the offset of the > fields, whether all fields have the same offset, what the call ABI > of the union is, etc. This commit explicitly asks for `#[repr(C)]` layout to guarantee that both fields have the same offeset. --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index cafab3b..ca7c1d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,6 +127,7 @@ macro_rules! transmute { // since the compiler hedges that the type being borrowed could have interior mutability. ($srcty:ty; $dstty:ty; $val:expr) => { { + #[repr(C)] union Transmute { src: ::core::mem::ManuallyDrop, dst: ::core::mem::ManuallyDrop,