Basically, if you have a union of A, B, C, you create a struct with three zero-sized fields using BindgenUnionField<A>, and then add a field after that containing enough bits to actually fill out the size. Because the BindgenUnionField is zero sized, a pointer to it is a pointer to the beginning of the struct, and it has an accessor that treats the pointer as the contained type.
This makes the API for field access `union.field.as_ref()` instead of `union.field`, but that's still pretty clean.
It's still a hack, and I'll be happy to see it go, but it's a really fun hack.
https://github.com/servo/rust-bindgen/blob/master/tests/expe...
Basically, if you have a union of A, B, C, you create a struct with three zero-sized fields using BindgenUnionField<A>, and then add a field after that containing enough bits to actually fill out the size. Because the BindgenUnionField is zero sized, a pointer to it is a pointer to the beginning of the struct, and it has an accessor that treats the pointer as the contained type.
This makes the API for field access `union.field.as_ref()` instead of `union.field`, but that's still pretty clean.
It's still a hack, and I'll be happy to see it go, but it's a really fun hack.