From 77862868d565d48d6bee8e302b5d5aebf8f14218 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 11 Mar 2024 14:28:18 +0100 Subject: [PATCH] these conversions are also useful --- CHANGELOG.md | 4 ++++ src/ecss/mod.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a21074..6578ca4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/ecss/mod.rs b/src/ecss/mod.rs index dd2351c..8a33d03 100644 --- a/src/ecss/mod.rs +++ b/src/ecss/mod.rs @@ -370,6 +370,12 @@ macro_rules! generic_ecss_enum_typedefs_and_from_impls { Self::new(value) } } + + impl From<$Enum> for $ty { + fn from(value: $Enum) -> Self { + value.value_typed() + } + } )* }; } @@ -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]); } @@ -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]); } @@ -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]); } @@ -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);