use simple_endian::*;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct Header {
magic: u32be,
version: u16le,
flags: u16be,
}
fn main() {
println!("=== Explicit-endian struct ===\n");
let mut h = Header {
magic: 0xfeed_faceu32.into(),
version: 1u16.into(),
flags: 0u16.into(),
};
println!("magic = 0x{:08x}", h.magic.to_native());
println!("version= {}", h.version.to_native());
println!("flags = 0x{:04x}", h.flags.to_native());
let next_version: u16 = h.version.to_native() + 1;
h.version = next_version.into();
h.flags |= 0x0001u16.into();
println!("\nafter update:");
println!("version= {}", h.version.to_native());
println!("flags = 0x{:04x}", h.flags.to_native());
let raw: &[u8; core::mem::size_of::<Header>()] = unsafe { core::mem::transmute(&h) };
println!("\nraw bytes ({}): {:02x?}", raw.len(), raw);
}