"Official" way to read the raw bytes of a `struct` I didn't define

Bit of an update: I messed around with bindgen a bunch and I now have an implementation of field-by-field serialization of sockaddr_in and sockaddr_in6 that should work on most Rust targets, and should bomb out in cargo test if it won't be reliable. It turns out there is less variation among surviving Unix species than I'd thought, and in particular there doesn't seem to be any existing implementation with anonymous padding anywhere within either struct.

The code is too long to paste here but if anyone is curious, here's a link: sockaddr.rs. I would welcome further suggestions, particularly re how to not have to repeat big long lists of cfg(any(target_os = "this", target_os = "that", ...)) expressions (I considered hoisting them to the submodule level but I think that would be even more repetitive).

If you have a favorite OS that isn't Linux, I would also encourage you to clone the entire workspace and try to build it and let me know how it goes. (It's not on crates.io yet, I want to get CI setup first and do some fuzz testing.)

The concept of an "active member" is only relevant in C++. This form of type punning is always permitted in C, barring trap representations.

See this recent discussion on the GCC bug tracker, though there are other discussions going back further where the consensus has always been that C is more permissive with unions than C++.