From experimenting with Compiler Explorer, I found std::mem::transmute() to result in the smallest machine code. The functions u128::to_le_bytes and u128::from_le_bytes do exactly this, and by replacing the tuple type (u32, u32, u32, u32) with the array type [u32; 4], we can guarantee that the compiler will not reorder the memory so that it is safe to call transmute.
From experimenting with Compiler Explorer, I found
std::mem::transmute()
to result in the smallest machine code. The functionsu128::to_le_bytes
andu128::from_le_bytes
do exactly this, and by replacing the tuple type(u32, u32, u32, u32)
with the array type[u32; 4]
, we can guarantee that the compiler will not reorder the memory so that it is safe to calltransmute
.