4 ms·
Casting bytes to a structure is tricky! First of all, unlike C, Rust doesn't define struct layout. We change the internal layout every so often to add optimizat
by steveklabnik 9y ago
Casting bytes to a structure is tricky! First of all, unlike C, Rust doesn't define struct layout. We change the internal layout every so often to add optimizations, and we can only do this thanks to that guarantee. We do have a way to say "use C's layout", if it's a struct you're defining. Also, you have things like endianness to worry about, portability is tough here!
Depending on your sitaution, you don't have to use unsafe yourself; for example, the byteorder crate can help here, or any of the serialization libraries that can serialize to a binary format.