use bytemuck::{Pod, Zeroable}; use core::fmt::Debug; use core::marker::PhantomData; use serde::Serialize; #[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)] #[repr(C, packed)] pub struct BigEndian; #[derive(Pod, Zeroable, Clone, Copy, Debug, Serialize)] #[repr(C, packed)] pub struct LittleEndian; pub trait Decode: Copy { type Native: Debug + Copy; fn to_native(self) -> Self::Native; } #[derive(Pod, Zeroable, Clone, Copy)] #[repr(C, packed)] pub struct U64 { raw: [u8; 8], _endian: PhantomData, } #[derive(Pod, Zeroable, Clone, Copy)] #[repr(C, packed)] pub struct U32 { raw: [u8; 4], _endian: PhantomData, } #[derive(Pod, Zeroable, Clone, Copy)] #[repr(C, packed)] pub struct U16 { raw: [u8; 2], _endian: PhantomData, } impl Decode for U64 { type Native = u64; fn to_native(self) -> Self::Native { u64::from_be_bytes(self.raw) } } impl Decode for U64 { type Native = u64; fn to_native(self) -> Self::Native { u64::from_le_bytes(self.raw) } } impl Decode for U32 { type Native = u32; fn to_native(self) -> Self::Native { u32::from_be_bytes(self.raw) } } impl Decode for U32 { type Native = u32; fn to_native(self) -> Self::Native { u32::from_le_bytes(self.raw) } } impl Decode for U16 { type Native = u16; fn to_native(self) -> Self::Native { u16::from_be_bytes(self.raw) } } impl Decode for U16 { type Native = u16; fn to_native(self) -> Self::Native { u16::from_le_bytes(self.raw) } } impl Debug for U64 where Self: Decode, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&self.to_native(), f) } } impl Debug for U32 where Self: Decode, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&self.to_native(), f) } } impl Debug for U16 where Self: Decode, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&self.to_native(), f) } } impl Serialize for U64 where Self: Decode, { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { format!("{:?}", self.to_native()).serialize(serializer) } } impl Serialize for U32 where Self: Decode, { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { format!("{:?}", self.to_native()).serialize(serializer) } } impl Serialize for U16 where Self: Decode, { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { format!("{:?}", self.to_native()).serialize(serializer) } }