pub enum Endian {
Little,
Big,
}
impl Endian {
pub fn read_u16(&self, bytes: [u8; 2]) -> u16 {
match self {
Endian::Little => u16::from_le_bytes(bytes),
Endian::Big => u16::from_be_bytes(bytes),
}
}
pub fn read_u32(&self, bytes: [u8; 4]) -> u32 {
match self {
Endian::Little => u32::from_le_bytes(bytes),
Endian::Big => u32::from_be_bytes(bytes),
}
}
pub fn read_u64(&self, bytes: [u8; 8]) -> u64 {
match self {
Endian::Little => u64::from_le_bytes(bytes),
Endian::Big => u64::from_be_bytes(bytes),
}
}
pub fn to_bytes_u16(&self, value: u16) -> [u8; 2] {
match self {
Endian::Little => value.to_le_bytes(),
Endian::Big => value.to_be_bytes(),
}
}
pub fn to_bytes_u32(&self, value: u32) -> [u8; 4] {
match self {
Endian::Little => value.to_le_bytes(),
Endian::Big => value.to_be_bytes(),
}
}
pub fn to_bytes_u64(&self, value: u64) -> [u8; 8] {
match self {
Endian::Little => value.to_le_bytes(),
Endian::Big => value.to_be_bytes(),
}
}
pub fn to_bytes_i32(&self, value: i32) -> [u8; 4] {
match self {
Endian::Little => value.to_le_bytes(),
Endian::Big => value.to_be_bytes(),
}
}
pub fn to_bytes_i64(&self, value: i64) -> [u8; 8] {
match self {
Endian::Little => value.to_le_bytes(),
Endian::Big => value.to_be_bytes(),
}
}
}