Skip to content

Commit

Permalink
Merge pull request 'More useful conversions' (#68) from missing-ecss-…
Browse files Browse the repository at this point in the history
…enum-conversion into main

Reviewed-on: https://egit.irs.uni-stuttgart.de/rust/spacepackets/pulls/68
  • Loading branch information
robamu committed Mar 11, 2024
2 parents ea05a54 + bd1927c commit 6f5254b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

# [unreleased]

## Added

- `From<$EcssEnum$TY> from $TY` for the ECSS enum type definitions.

# [v0.11.0-rc.0] 2024-03-04

## Added
Expand Down
20 changes: 17 additions & 3 deletions src/ecss/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,20 @@ impl<TYPE: Debug + Copy + Clone + PartialEq + Eq + ToBeBytes + Into<u64>> EcssEn
{
}

impl<T: Copy + Into<u64>> From<T> for GenericEcssEnumWrapper<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}

macro_rules! generic_ecss_enum_typedefs_and_from_impls {
($($ty:ty => $Enum:ident),*) => {
$(
pub type $Enum = GenericEcssEnumWrapper<$ty>;

impl From<$ty> for $Enum {
fn from(value: $ty) -> Self {
Self::new(value)
impl From<$Enum> for $ty {
fn from(value: $Enum) -> Self {
value.value_typed()
}
}
)*
Expand Down Expand Up @@ -424,6 +430,8 @@ mod tests {
assert_eq!(buf[1], 1);
assert_eq!(my_enum.value(), 1);
assert_eq!(my_enum.value_typed(), 1);
let enum_as_u8: u8 = my_enum.into();
assert_eq!(enum_as_u8, 1);
let vec = my_enum.to_vec();
assert_eq!(vec, buf[1..2]);
}
Expand All @@ -441,6 +449,8 @@ mod tests {
assert_eq!(buf[2], 0x2f);
assert_eq!(my_enum.value(), 0x1f2f);
assert_eq!(my_enum.value_typed(), 0x1f2f);
let enum_as_raw: u16 = my_enum.into();
assert_eq!(enum_as_raw, 0x1f2f);
let vec = my_enum.to_vec();
assert_eq!(vec, buf[1..3]);
}
Expand Down Expand Up @@ -476,6 +486,8 @@ mod tests {
assert_eq!(buf[4], 0x4f);
assert_eq!(my_enum.value(), 0x1f2f3f4f);
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f);
let enum_as_raw: u32 = my_enum.into();
assert_eq!(enum_as_raw, 0x1f2f3f4f);
let vec = my_enum.to_vec();
assert_eq!(vec, buf[1..5]);
}
Expand Down Expand Up @@ -512,6 +524,8 @@ mod tests {
assert_eq!(buf[7], 0x5f);
assert_eq!(my_enum.value(), 0x1f2f3f4f5f);
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f5f);
let enum_as_raw: u64 = my_enum.into();
assert_eq!(enum_as_raw, 0x1f2f3f4f5f);
assert_eq!(u64::from_be_bytes(buf), 0x1f2f3f4f5f);
let vec = my_enum.to_vec();
assert_eq!(vec, buf);
Expand Down

0 comments on commit 6f5254b

Please sign in to comment.