Hello, Is there was to put endianness to get bytes from Struct like so

Hello, Is there was to put endianness to get bytes from Struct like so
std::slice::from_raw_parts((my_struct as *const _) as *const u8, std::mem::size_of::<MyStruct>())
You already invited:

Nora

Upvotes from:

for both big and little endian, you should be writing the struct field by field
Don’t rely on the as *const _ technique as the compiler can rearrange fields, even beyond reordering and alignment - the Rust compiler optimises memory layout much more than a C or C++ compiler would
Take a look at the byteorder crate, which has helper functions for writing fields in little or big endian format

If you wanna answer this question please Login or Register