Extensions for UnsignedEnum trait
This commit is contained in:
parent
f47604346e
commit
60bf876dd3
@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
|
# [v0.10.0] 2024-02-17
|
||||||
|
|
||||||
|
## Added
|
||||||
|
|
||||||
|
- Added `value` and `to_vec` methods for the `UnsignedEnum` trait. The value is returned as
|
||||||
|
as `u64`. Renamed former `value` method on `GenericUnsignedByteField` to `value_typed`.
|
||||||
|
- Added `value_const` const function for `UnsignedByteField` type.
|
||||||
|
- Added `value_typed` const functions for `GenericUnsignedByteField` and `GenericEcssEnumWrapper`.
|
||||||
|
|
||||||
# [v0.9.0] 2024-02-07
|
# [v0.9.0] 2024-02-07
|
||||||
|
|
||||||
## Added
|
## Added
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "spacepackets"
|
name = "spacepackets"
|
||||||
version = "0.9.0"
|
version = "0.10.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.61"
|
rust-version = "1.61"
|
||||||
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
|
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
|
||||||
|
@ -581,7 +581,7 @@ mod tests {
|
|||||||
assert_eq!(finished_pdu_vec.len(), 12);
|
assert_eq!(finished_pdu_vec.len(), 12);
|
||||||
assert_eq!(finished_pdu_vec[9], TlvType::EntityId.into());
|
assert_eq!(finished_pdu_vec[9], TlvType::EntityId.into());
|
||||||
assert_eq!(finished_pdu_vec[10], 1);
|
assert_eq!(finished_pdu_vec[10], 1);
|
||||||
assert_eq!(finished_pdu_vec[11], TEST_DEST_ID.value());
|
assert_eq!(finished_pdu_vec[11], TEST_DEST_ID.value_typed());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
finished_pdu.fault_location().unwrap().entity_id(),
|
finished_pdu.fault_location().unwrap().entity_id(),
|
||||||
&TEST_DEST_ID.into()
|
&TEST_DEST_ID.into()
|
||||||
|
@ -315,15 +315,19 @@ pub trait EcssEnumerationExt: EcssEnumeration + Debug + Copy + Clone + PartialEq
|
|||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
pub struct GenericEcssEnumWrapper<TYPE: Copy> {
|
pub struct GenericEcssEnumWrapper<TYPE: Copy + Into<u64>> {
|
||||||
field: GenericUnsignedByteField<TYPE>,
|
field: GenericUnsignedByteField<TYPE>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: Copy> GenericEcssEnumWrapper<TYPE> {
|
impl<TYPE: Copy + Into<u64>> GenericEcssEnumWrapper<TYPE> {
|
||||||
pub const fn ptc() -> PacketTypeCodes {
|
pub const fn ptc() -> PacketTypeCodes {
|
||||||
PacketTypeCodes::Enumerated
|
PacketTypeCodes::Enumerated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const fn value_typed(&self) -> TYPE {
|
||||||
|
self.field.value_typed()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(val: TYPE) -> Self {
|
pub fn new(val: TYPE) -> Self {
|
||||||
Self {
|
Self {
|
||||||
field: GenericUnsignedByteField::new(val),
|
field: GenericUnsignedByteField::new(val),
|
||||||
@ -331,7 +335,7 @@ impl<TYPE: Copy> GenericEcssEnumWrapper<TYPE> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: Copy + ToBeBytes> UnsignedEnum for GenericEcssEnumWrapper<TYPE> {
|
impl<TYPE: Copy + ToBeBytes + Into<u64>> UnsignedEnum for GenericEcssEnumWrapper<TYPE> {
|
||||||
fn size(&self) -> usize {
|
fn size(&self) -> usize {
|
||||||
(self.pfc() / 8) as usize
|
(self.pfc() / 8) as usize
|
||||||
}
|
}
|
||||||
@ -339,15 +343,19 @@ impl<TYPE: Copy + ToBeBytes> UnsignedEnum for GenericEcssEnumWrapper<TYPE> {
|
|||||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
||||||
self.field.write_to_be_bytes(buf)
|
self.field.write_to_be_bytes(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> u64 {
|
||||||
|
self.field.value()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: Copy + ToBeBytes> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
|
impl<TYPE: Copy + ToBeBytes + Into<u64>> EcssEnumeration for GenericEcssEnumWrapper<TYPE> {
|
||||||
fn pfc(&self) -> u8 {
|
fn pfc(&self) -> u8 {
|
||||||
size_of::<TYPE>() as u8 * 8_u8
|
size_of::<TYPE>() as u8 * 8_u8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: Debug + Copy + Clone + PartialEq + Eq + ToBeBytes> EcssEnumerationExt
|
impl<TYPE: Debug + Copy + Clone + PartialEq + Eq + ToBeBytes + Into<u64>> EcssEnumerationExt
|
||||||
for GenericEcssEnumWrapper<TYPE>
|
for GenericEcssEnumWrapper<TYPE>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -396,6 +404,8 @@ mod tests {
|
|||||||
.write_to_be_bytes(&mut buf[1..2])
|
.write_to_be_bytes(&mut buf[1..2])
|
||||||
.expect("To byte conversion of u8 failed");
|
.expect("To byte conversion of u8 failed");
|
||||||
assert_eq!(buf[1], 1);
|
assert_eq!(buf[1], 1);
|
||||||
|
assert_eq!(my_enum.value(), 1);
|
||||||
|
assert_eq!(my_enum.value_typed(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -409,6 +419,8 @@ mod tests {
|
|||||||
assert_eq!(my_enum.pfc(), 16);
|
assert_eq!(my_enum.pfc(), 16);
|
||||||
assert_eq!(buf[1], 0x1f);
|
assert_eq!(buf[1], 0x1f);
|
||||||
assert_eq!(buf[2], 0x2f);
|
assert_eq!(buf[2], 0x2f);
|
||||||
|
assert_eq!(my_enum.value(), 0x1f2f);
|
||||||
|
assert_eq!(my_enum.value_typed(), 0x1f2f);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -440,6 +452,8 @@ mod tests {
|
|||||||
assert_eq!(buf[2], 0x2f);
|
assert_eq!(buf[2], 0x2f);
|
||||||
assert_eq!(buf[3], 0x3f);
|
assert_eq!(buf[3], 0x3f);
|
||||||
assert_eq!(buf[4], 0x4f);
|
assert_eq!(buf[4], 0x4f);
|
||||||
|
assert_eq!(my_enum.value(), 0x1f2f3f4f);
|
||||||
|
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -460,6 +474,23 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_enum_u64() {
|
||||||
|
let mut buf = [0; 8];
|
||||||
|
let my_enum = EcssEnumU64::new(0x1f2f3f4f5f);
|
||||||
|
my_enum
|
||||||
|
.write_to_be_bytes(&mut buf)
|
||||||
|
.expect("To byte conversion of u64 failed");
|
||||||
|
assert_eq!(buf[3], 0x1f);
|
||||||
|
assert_eq!(buf[4], 0x2f);
|
||||||
|
assert_eq!(buf[5], 0x3f);
|
||||||
|
assert_eq!(buf[6], 0x4f);
|
||||||
|
assert_eq!(buf[7], 0x5f);
|
||||||
|
assert_eq!(my_enum.value(), 0x1f2f3f4f5f);
|
||||||
|
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f5f);
|
||||||
|
assert_eq!(u64::from_be_bytes(buf), 0x1f2f3f4f5f);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pus_error_display() {
|
fn test_pus_error_display() {
|
||||||
let unsupport_version = PusError::VersionNotSupported(super::PusVersion::EsaPus);
|
let unsupport_version = PusError::VersionNotSupported(super::PusVersion::EsaPus);
|
||||||
|
45
src/util.rs
45
src/util.rs
@ -73,6 +73,15 @@ pub trait UnsignedEnum {
|
|||||||
fn size(&self) -> usize;
|
fn size(&self) -> usize;
|
||||||
/// Write the unsigned enumeration to a raw buffer. Returns the written size on success.
|
/// Write the unsigned enumeration to a raw buffer. Returns the written size on success.
|
||||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError>;
|
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError>;
|
||||||
|
|
||||||
|
fn value(&self) -> u64;
|
||||||
|
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
|
fn to_vec(&self) -> alloc::vec::Vec<u8> {
|
||||||
|
let mut buf = alloc::vec![0; self.size()];
|
||||||
|
self.write_to_be_bytes(&mut buf).unwrap();
|
||||||
|
buf
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait UnsignedEnumExt: UnsignedEnum + Debug + Copy + Clone + PartialEq + Eq {}
|
pub trait UnsignedEnumExt: UnsignedEnum + Debug + Copy + Clone + PartialEq + Eq {}
|
||||||
@ -132,7 +141,7 @@ impl UnsignedByteField {
|
|||||||
Self { width, value }
|
Self { width, value }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(&self) -> u64 {
|
pub const fn value_const(&self) -> u64 {
|
||||||
self.value
|
self.value
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,6 +181,10 @@ impl UnsignedEnum for UnsignedByteField {
|
|||||||
self.width
|
self.width
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> u64 {
|
||||||
|
self.value_const()
|
||||||
|
}
|
||||||
|
|
||||||
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
|
||||||
if buf.len() < self.size() {
|
if buf.len() < self.size() {
|
||||||
return Err(ByteConversionError::ToSliceTooSmall {
|
return Err(ByteConversionError::ToSliceTooSmall {
|
||||||
@ -207,21 +220,21 @@ impl UnsignedEnum for UnsignedByteField {
|
|||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
pub struct GenericUnsignedByteField<TYPE: Copy> {
|
pub struct GenericUnsignedByteField<TYPE: Copy + Into<u64>> {
|
||||||
value: TYPE,
|
value: TYPE,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: Copy> GenericUnsignedByteField<TYPE> {
|
impl<TYPE: Copy + Into<u64>> GenericUnsignedByteField<TYPE> {
|
||||||
pub const fn new(val: TYPE) -> Self {
|
pub const fn new(val: TYPE) -> Self {
|
||||||
Self { value: val }
|
Self { value: val }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn value(&self) -> TYPE {
|
pub const fn value_typed(&self) -> TYPE {
|
||||||
self.value
|
self.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TYPE: Copy + ToBeBytes> UnsignedEnum for GenericUnsignedByteField<TYPE> {
|
impl<TYPE: Copy + ToBeBytes + Into<u64>> UnsignedEnum for GenericUnsignedByteField<TYPE> {
|
||||||
fn size(&self) -> usize {
|
fn size(&self) -> usize {
|
||||||
self.value.written_len()
|
self.value.written_len()
|
||||||
}
|
}
|
||||||
@ -233,9 +246,13 @@ impl<TYPE: Copy + ToBeBytes> UnsignedEnum for GenericUnsignedByteField<TYPE> {
|
|||||||
expected: self.size(),
|
expected: self.size(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
buf[0..self.size()].copy_from_slice(self.value.to_be_bytes().as_ref());
|
buf[..self.size()].copy_from_slice(self.value.to_be_bytes().as_ref());
|
||||||
Ok(self.value.written_len())
|
Ok(self.value.written_len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> u64 {
|
||||||
|
self.value_typed().into()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type UnsignedByteFieldEmpty = GenericUnsignedByteField<()>;
|
pub type UnsignedByteFieldEmpty = GenericUnsignedByteField<()>;
|
||||||
@ -351,6 +368,8 @@ pub mod tests {
|
|||||||
for val in buf.iter().skip(1) {
|
for val in buf.iter().skip(1) {
|
||||||
assert_eq!(*val, 0);
|
assert_eq!(*val, 0);
|
||||||
}
|
}
|
||||||
|
assert_eq!(u8.value_typed(), 5);
|
||||||
|
assert_eq!(u8.value(), 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -367,6 +386,8 @@ pub mod tests {
|
|||||||
for val in buf.iter().skip(2) {
|
for val in buf.iter().skip(2) {
|
||||||
assert_eq!(*val, 0);
|
assert_eq!(*val, 0);
|
||||||
}
|
}
|
||||||
|
assert_eq!(u16.value_typed(), 3823);
|
||||||
|
assert_eq!(u16.value(), 3823);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -383,6 +404,8 @@ pub mod tests {
|
|||||||
(4..8).for_each(|i| {
|
(4..8).for_each(|i| {
|
||||||
assert_eq!(buf[i], 0);
|
assert_eq!(buf[i], 0);
|
||||||
});
|
});
|
||||||
|
assert_eq!(u32.value_typed(), 80932);
|
||||||
|
assert_eq!(u32.value(), 80932);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -396,6 +419,8 @@ pub mod tests {
|
|||||||
assert_eq!(len, 8);
|
assert_eq!(len, 8);
|
||||||
let raw_val = u64::from_be_bytes(buf[0..8].try_into().unwrap());
|
let raw_val = u64::from_be_bytes(buf[0..8].try_into().unwrap());
|
||||||
assert_eq!(raw_val, 5999999);
|
assert_eq!(raw_val, 5999999);
|
||||||
|
assert_eq!(u64.value_typed(), 5999999);
|
||||||
|
assert_eq!(u64.value(), 5999999);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -534,9 +559,9 @@ pub mod tests {
|
|||||||
u8.write_to_be_bytes(&mut buf)
|
u8.write_to_be_bytes(&mut buf)
|
||||||
.expect("writing to raw buffer failed");
|
.expect("writing to raw buffer failed");
|
||||||
assert_eq!(buf[0], 5);
|
assert_eq!(buf[0], 5);
|
||||||
for i in 1..8 {
|
(1..8).for_each(|i| {
|
||||||
assert_eq!(buf[i], 0);
|
assert_eq!(buf[i], 0);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -562,9 +587,9 @@ pub mod tests {
|
|||||||
.expect("writing to raw buffer failed");
|
.expect("writing to raw buffer failed");
|
||||||
let raw_val = u32::from_be_bytes(buf[0..4].try_into().unwrap());
|
let raw_val = u32::from_be_bytes(buf[0..4].try_into().unwrap());
|
||||||
assert_eq!(raw_val, 80932);
|
assert_eq!(raw_val, 80932);
|
||||||
for i in 4..8 {
|
(4..8).for_each(|i| {
|
||||||
assert_eq!(buf[i], 0);
|
assert_eq!(buf[i], 0);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user