pub trait FromBytes: Sized {
type Bytes: ?Sized;
const FROM_BYTES_SIZE: usize = std::mem::size_of::<Self>();
fn from_bytes(bytes: &[u8]) -> Self;
}
macro_rules! impl_from_bytes {
($ty:ty, $len:expr) => {
impl FromBytes for $ty {
type Bytes = $ty;
fn from_bytes(bytes: &[u8]) -> Self {
let mut code_buf = [0u8; $len];
code_buf.copy_from_slice(bytes);
<$ty>::from_le_bytes(code_buf)
}
}
};
}
impl_from_bytes!(u128, 16);
impl_from_bytes!(u64, 8);
impl_from_bytes!(f64, 8);
impl_from_bytes!(i64, 8);
impl_from_bytes!(i32, 4);
impl_from_bytes!(u32, 4);
impl_from_bytes!(u16, 2);