peakrdl_rust/
encode.rs

1//! Types for enum-encoded field representations
2
3use core::error::Error;
4use core::fmt::{Debug, Display, Formatter};
5
6/// A bit pattern representing an unencoded enum variant.
7///
8/// This is typically obtained as an error value when reading an enum-encoded
9/// field, but the bit value of the field doesn't match any known enum variant.
10#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
11pub struct UnknownVariant<T>(pub T);
12
13impl<T: Copy> UnknownVariant<T> {
14    pub const fn new(value: T) -> Self {
15        Self(value)
16    }
17
18    /// The bit value of the unknown variant
19    pub const fn value(&self) -> T {
20        self.0
21    }
22}
23
24impl<T: Copy + Display> Display for UnknownVariant<T> {
25    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
26        write!(f, "Unknown enum variant: {}", self.0)
27    }
28}
29
30impl<T: Copy + Debug + Display> Error for UnknownVariant<T> {}