m17core/
bits.rs

1use core::ops::Deref;
2
3pub(crate) struct BitsBase<T>(T)
4where
5    T: AsRef<[u8]>;
6
7impl<T> BitsBase<T>
8where
9    T: AsRef<[u8]>,
10{
11    pub(crate) fn get_bit(&self, idx: usize) -> u8 {
12        self.0.as_ref()[idx / 8] >> (7 - (idx % 8)) & 0x01
13    }
14
15    pub(crate) fn iter(&self) -> BitsIterator<T> {
16        BitsIterator { bits: self, idx: 0 }
17    }
18}
19
20pub(crate) struct BitsIterator<'a, T>
21where
22    T: AsRef<[u8]>,
23{
24    bits: &'a BitsBase<T>,
25    idx: usize,
26}
27
28impl<T> Iterator for BitsIterator<'_, T>
29where
30    T: AsRef<[u8]>,
31{
32    type Item = u8;
33
34    fn next(&mut self) -> Option<Self::Item> {
35        if self.idx >= self.bits.0.as_ref().len() * 8 {
36            return None;
37        }
38        let bit = self.bits.get_bit(self.idx);
39        self.idx += 1;
40        Some(bit)
41    }
42}
43
44pub(crate) struct Bits<'a>(BitsBase<&'a [u8]>);
45
46impl<'a> Bits<'a> {
47    pub(crate) fn new(data: &'a [u8]) -> Self {
48        Self(BitsBase(data))
49    }
50}
51
52impl<'a> Deref for Bits<'a> {
53    type Target = BitsBase<&'a [u8]>;
54
55    fn deref(&self) -> &Self::Target {
56        &self.0
57    }
58}
59
60pub(crate) struct BitsMut<'a>(BitsBase<&'a mut [u8]>);
61
62impl<'a> Deref for BitsMut<'a> {
63    type Target = BitsBase<&'a mut [u8]>;
64
65    fn deref(&self) -> &Self::Target {
66        &self.0
67    }
68}
69
70impl<'a> BitsMut<'a> {
71    pub(crate) fn new(data: &'a mut [u8]) -> Self {
72        Self(BitsBase(data))
73    }
74
75    pub(crate) fn set_bit(&mut self, idx: usize, value: u8) {
76        let slice = &mut self.0 .0;
77        let existing = slice[idx / 8];
78        if value == 0 {
79            slice[idx / 8] = existing & !(1 << (7 - (idx % 8)));
80        } else {
81            slice[idx / 8] = existing | (1 << (7 - (idx % 8)));
82        }
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn bits_readonly() {
92        let data: [u8; 2] = [0b00001111, 0b10101010];
93        let bits = Bits::new(&data);
94        assert_eq!(bits.get_bit(0), 0);
95        assert_eq!(bits.get_bit(1), 0);
96        assert_eq!(bits.get_bit(4), 1);
97        assert_eq!(bits.get_bit(8), 1);
98        assert_eq!(bits.get_bit(9), 0);
99    }
100
101    #[test]
102    fn bits_modifying() {
103        let mut data: [u8; 2] = [0b00001111, 0b10101010];
104        let mut bits = BitsMut::new(&mut data);
105
106        assert_eq!(bits.get_bit(0), 0);
107        bits.set_bit(0, 1);
108        assert_eq!(bits.get_bit(0), 1);
109
110        assert_eq!(bits.get_bit(4), 1);
111        bits.set_bit(4, 0);
112        assert_eq!(bits.get_bit(4), 0);
113
114        assert_eq!(bits.get_bit(9), 0);
115        bits.set_bit(9, 1);
116        assert_eq!(bits.get_bit(9), 1);
117
118        assert_eq!(data, [0b10000111, 0b11101010]);
119    }
120
121    #[test]
122    fn bits_iter() {
123        let data: [u8; 2] = [0b00110111, 0b10101010];
124        let bits = Bits::new(&data);
125        let mut it = bits.iter();
126        assert_eq!(it.next(), Some(0));
127        assert_eq!(it.next(), Some(0));
128        assert_eq!(it.next(), Some(1));
129        assert_eq!(it.next(), Some(1));
130        assert_eq!(it.next(), Some(0));
131        assert_eq!(it.next(), Some(1));
132        for _ in 0..8 {
133            let _ = it.next();
134        }
135        assert_eq!(it.next(), Some(1));
136        assert_eq!(it.next(), Some(0));
137        assert_eq!(it.next(), None);
138    }
139}