Pre-RFC: Transparent unions

Don’t want to bikeshed too much, but an alternative would be to allow #[repr(transparent)] on any union, and give it the representation of its first member or something (which would have to be large enough). Thus, the following union would have the ABI of a u64, which would make it a way to pass around a pair of u32s that’s more efficient in certain cases:

#[repr(transparent)]
union Foo {
    one_u64: u64,
    two_u32s: TwoU32s,
}
#[repr(C)]
struct TwoU32s(u32, u32);

Edit: Or if, say, you ran out of registers for a function’s integer arguments, and instead of putting the rest on the stack as usual, you decide you want to stick them in floating-point registers for some reason :slight_smile: :

#[repr(transparent)]
union Bar {
    floating: f64,
    integer: u64,
}
2 Likes