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.
This commit is contained in:
Lukasz Anforowicz 2024-08-27 09:30:57 -07:00 committed by GitHub
parent 3c42ba7f23
commit 3f42bec539
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<A, B> {
src: ::core::mem::ManuallyDrop<A>,
dst: ::core::mem::ManuallyDrop<B>,