X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=rust%2Fsrc%2Fendian.rs;h=3692180dbaa098d847e005b9df3b5aa4de47d847;hb=a8331d2f67af24ce1f9f5da99641b8d1cdc21300;hp=6bd25ab95ac0408793983a34ed18ca8ad68f0db6;hpb=e404e91fe1237d5dcca500ae178afcecb7907664;p=pspp diff --git a/rust/src/endian.rs b/rust/src/endian.rs index 6bd25ab95a..3692180dba 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, @@ -19,7 +19,7 @@ impl Endian { match (as_big == expected_value, as_little == expected_value) { (true, false) => Some(Endian::Big), (false, true) => Some(Endian::Little), - _ => None + _ => None, } } @@ -29,7 +29,7 @@ impl Endian { match (as_big == expected_value, as_little == expected_value) { (true, false) => Some(Endian::Big), (false, true) => Some(Endian::Little), - _ => None + _ => None, } } } @@ -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 { @@ -124,4 +161,3 @@ impl Parse for Endian { } } } -