modular_bitfield/
error.rs

1//! Errors that can occur while operating on modular bitfields.
2
3use core::fmt::Debug;
4
5/// The given value was out of range for the bitfield.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub struct OutOfBounds;
8
9impl core::fmt::Display for OutOfBounds {
10    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11        write!(f, "encountered an out of bounds value")
12    }
13}
14
15/// The bitfield contained an invalid bit pattern.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct InvalidBitPattern<Bytes> {
18    /// The invalid bits.
19    invalid_bytes: Bytes,
20}
21
22impl<Bytes> core::fmt::Display for InvalidBitPattern<Bytes>
23where
24    Bytes: Debug,
25{
26    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
27        write!(
28            f,
29            "encountered an invalid bit pattern: 0x{:X?}",
30            self.invalid_bytes
31        )
32    }
33}
34
35impl<Bytes> InvalidBitPattern<Bytes> {
36    /// Creates a new invalid bit pattern error.
37    #[inline]
38    pub fn new(invalid_bytes: Bytes) -> Self {
39        Self { invalid_bytes }
40    }
41
42    /// Returns the invalid bit pattern.
43    #[inline]
44    pub fn invalid_bytes(self) -> Bytes {
45        self.invalid_bytes
46    }
47}