Skip to main content

ps_ecc/long/header/overlap_factor/methods/
from_u8.rs

1use super::super::OverlapFactor;
2
3impl OverlapFactor {
4    /// Extracts the overlap factor from the high two bits of a parity byte.
5    ///
6    /// The low six bits are ignored, so any byte maps to a valid factor.
7    #[inline]
8    #[must_use]
9    pub const fn from_u8(byte: u8) -> Self {
10        match byte & 0xC0 {
11            0x00 => Self::Simple,
12            0x40 => Self::Double,
13            0x80 => Self::Triple,
14            0xC0 => Self::Quadruple,
15            // `& 0xC0` yields only the four values above
16            _ => unreachable!(),
17        }
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use crate::long::OverlapFactor;
24
25    #[test]
26    fn test_from_u8_high_bits() {
27        assert_eq!(OverlapFactor::from_u8(0x00), OverlapFactor::Simple);
28        assert_eq!(OverlapFactor::from_u8(0x40), OverlapFactor::Double);
29        assert_eq!(OverlapFactor::from_u8(0x80), OverlapFactor::Triple);
30        assert_eq!(OverlapFactor::from_u8(0xC0), OverlapFactor::Quadruple);
31    }
32
33    #[test]
34    fn test_from_u8_ignores_low_bits() {
35        assert_eq!(OverlapFactor::from_u8(0x3F), OverlapFactor::Simple);
36        assert_eq!(OverlapFactor::from_u8(0x7F), OverlapFactor::Double);
37        assert_eq!(OverlapFactor::from_u8(0xBF), OverlapFactor::Triple);
38        assert_eq!(OverlapFactor::from_u8(0xFF), OverlapFactor::Quadruple);
39    }
40}