Say I have a byte array like
let x: &[u8; 4] = &[...];
and I want to interpret it as a big-endian u32. My approach is
let x_parsed = u32::from_be(mem::transmute(*x))
My question is: is there a faster way to do this, or is there a safe way that will optimize the same? I’d rather not turn off the type checker with
transmute, since for example
x (a pointer) would be accepted on a 32-bit system (though obviously be wrong).
I’ve seen things like
byteorder, but I don’t know if they incur a performance penalty with unnecessary bounds checking (since I’m working on fixed-length arrays I don’t need runtime checking).