Skip to content

Commit

Permalink
Force #[repr(C)] layout to guarantee same offset of union fields.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anforowicz committed Aug 27, 2024
1 parent 3c42ba7 commit b7976de
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down

0 comments on commit b7976de

Please sign in to comment.