diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index 7134e8b..b4e1b32 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -343,12 +343,6 @@ impl> UnsignedEnum for GenericEcssEnumWrapper fn value(&self) -> u64 { self.field.value() } - - fn to_vec(&self) -> alloc::vec::Vec { - let mut buf = alloc::vec![0; self.size()]; - self.write_to_be_bytes(&mut buf).unwrap(); - buf - } } impl> EcssEnumeration for GenericEcssEnumWrapper { @@ -406,6 +400,7 @@ mod tests { .write_to_be_bytes(&mut buf[1..2]) .expect("To byte conversion of u8 failed"); assert_eq!(buf[1], 1); + assert_eq!(my_enum.value(), 1); } #[test] @@ -419,6 +414,7 @@ mod tests { assert_eq!(my_enum.pfc(), 16); assert_eq!(buf[1], 0x1f); assert_eq!(buf[2], 0x2f); + assert_eq!(my_enum.value(), 0x1f2f); } #[test] @@ -450,6 +446,7 @@ mod tests { assert_eq!(buf[2], 0x2f); assert_eq!(buf[3], 0x3f); assert_eq!(buf[4], 0x4f); + assert_eq!(my_enum.value(), 0x1f2f3f4f); } #[test] diff --git a/src/util.rs b/src/util.rs index 424568b..1b10b27 100644 --- a/src/util.rs +++ b/src/util.rs @@ -73,6 +73,7 @@ pub trait UnsignedEnum { fn size(&self) -> usize; /// Write the unsigned enumeration to a raw buffer. Returns the written size on success. fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result; + fn value(&self) -> u64; #[cfg(feature = "alloc")] @@ -140,6 +141,10 @@ impl UnsignedByteField { Self { width, value } } + pub const fn value_const(&self) -> u64 { + self.value + } + pub fn new_from_be_bytes(width: usize, buf: &[u8]) -> Result { if width > buf.len() { return Err(ByteConversionError::FromSliceTooSmall { @@ -177,7 +182,7 @@ impl UnsignedEnum for UnsignedByteField { } fn value(&self) -> u64 { - self.value + self.value_const() } fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result { @@ -363,6 +368,8 @@ pub mod tests { for val in buf.iter().skip(1) { assert_eq!(*val, 0); } + assert_eq!(u8.value_typed(), 5); + assert_eq!(u8.value(), 5); } #[test] @@ -379,6 +386,8 @@ pub mod tests { for val in buf.iter().skip(2) { assert_eq!(*val, 0); } + assert_eq!(u16.value_typed(), 3823); + assert_eq!(u16.value(), 3823); } #[test] @@ -395,6 +404,8 @@ pub mod tests { (4..8).for_each(|i| { assert_eq!(buf[i], 0); }); + assert_eq!(u32.value_typed(), 80932); + assert_eq!(u32.value(), 80932); } #[test] @@ -408,6 +419,8 @@ pub mod tests { assert_eq!(len, 8); let raw_val = u64::from_be_bytes(buf[0..8].try_into().unwrap()); assert_eq!(raw_val, 5999999); + assert_eq!(u64.value_typed(), 5999999); + assert_eq!(u64.value(), 5999999); } #[test]