X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=rust%2Fsrc%2Fendian.rs;h=bb63ec518dd832bdc10b069065bb7ce95f33a040;hb=20e2af4ec687ccbdfe47fe30275dd0121ec3ec16;hp=51952137b7b657d123d9394413388f6340f65642;hpb=e62271c65d61e9e84a6eb97a9db4673e710761c4;p=pspp diff --git a/rust/src/endian.rs b/rust/src/endian.rs index 51952137b7..bb63ec518d 100644 --- a/rust/src/endian.rs +++ b/rust/src/endian.rs @@ -1,9 +1,9 @@ -#[derive(Copy, Clone, Debug)] /// The endianness for integer and floating-point numbers in SPSS system files. /// /// SPSS system files can declare IBM 370 and DEC VAX floating-point /// representations, but no file that uses either of these has ever been found /// in the wild, so this code does not handle them. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Endian { /// Big-endian: MSB at lowest address. Big, @@ -34,6 +34,55 @@ impl Endian { } } +pub trait ToBytes { + fn to_bytes(self, value: T) -> [u8; N]; +} +impl ToBytes for Endian { + fn to_bytes(self, value: i64) -> [u8; 8] { + match self { + Endian::Big => i64::to_be_bytes(value), + Endian::Little => i64::to_le_bytes(value), + } + } +} +impl ToBytes for Endian { + fn to_bytes(self, value: u32) -> [u8; 4] { + match self { + Endian::Big => u32::to_be_bytes(value), + Endian::Little => u32::to_le_bytes(value), + } + } +} +impl ToBytes for Endian { + fn to_bytes(self, value: i32) -> [u8; 4] { + match self { + Endian::Big => i32::to_be_bytes(value), + Endian::Little => i32::to_le_bytes(value), + } + } +} +impl ToBytes for Endian { + fn to_bytes(self, value: u16) -> [u8; 2] { + match self { + Endian::Big => u16::to_be_bytes(value), + Endian::Little => u16::to_le_bytes(value), + } + } +} +impl ToBytes for Endian { + fn to_bytes(self, value: u8) -> [u8; 1] { + [value] + } +} +impl ToBytes for Endian { + fn to_bytes(self, value: f64) -> [u8; 8] { + match self { + Endian::Big => f64::to_be_bytes(value), + Endian::Little => f64::to_le_bytes(value), + } + } +} + /// Parses an `N`-byte slice in one of the supported formats into native format /// as type `T`. pub trait Parse {