X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=rust%2Fsrc%2Fendian.rs;h=bb63ec518dd832bdc10b069065bb7ce95f33a040;hb=20e2af4ec687ccbdfe47fe30275dd0121ec3ec16;hp=6bd25ab95ac0408793983a34ed18ca8ad68f0db6;hpb=e404e91fe1237d5dcca500ae178afcecb7907664;p=pspp diff --git a/rust/src/endian.rs b/rust/src/endian.rs index 6bd25ab95a..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, @@ -37,6 +37,43 @@ 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 {