Crate num_bytes[−][src]
Expand description
A crate to allow converting generic from/into bytes.
Defines FromBytes
and IntoBytes
traits.
Implements them on all numerical primitives.
Usage is a little tricky:
let a = [8,0];
let b: i16 = from_generic(a);
assert_eq!(b,8);
fn from_generic<T: num_bytes::FromBytes<N>, const N: usize>(x: [u8;N]) -> T {
T::from_le_bytes(x)
}
let a = 8i16;
let b = into_generic(a);
assert_eq!(b,[8,0]);
fn into_generic<T: num_bytes::IntoBytes<N>, const N: usize>(x: T) -> [u8;N] {
x.into_le_bytes()
}