Let's assume I have a few types A,B and C, as well as the following union:
#[repr(C)]
union U {
a: A
b: B
c: C
}
I'm trying to know if it is correct to do the following:
let value: B = todo!();
let by_ref: &U = unsafe { transmute(&value) };
// Do something with by_ref that only reads the `b` field
let by_mut: &mut U = unsafe { transmute(&mut value) };
// Do something with by_mut that only reads and writes the `b` field
The opposite is always possible by just accessing the fields of the union (with unsafe) but I'm not convinced this is not UB.
I think it is sound because as long as you only access the b
field of the union, you'll never read or write data that was not part of the initial value.
From the C spec:
A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit- field, then to the unit in which it resides), and vice versa.
My understanding is that this would be sound with pointers. Does it also make it sound for Rust references?
If so, what is the point of the proposed #[repr(transparent)]
for unions?