bits_io/buf/
bit_buf_exts.rs

1use funty::Integral;
2
3use super::bit_buf::BitBuf;
4use crate::prelude::*;
5
6pub trait BitBufExts: BitBuf {
7    #[allow(non_snake_case)]
8    fn get_uN<O: ByteOrder, const N: usize, U, V: Integral>(&mut self) -> std::io::Result<U>
9    where
10        U: TryFrom<V>,
11        U::Error: std::fmt::Debug,
12    {
13        // Create a bitvec in which to load the value
14        let mut bits = BitVec::repeat(false, N);
15        let slice = bits.as_mut_bitslice();
16        // Copy the raw bits into the slice
17        self.try_copy_to_bit_slice(slice)?;
18        // Now 'load' the value from that slice according to the given ByteOrder.
19        let value: V = O::load(slice);
20
21        Ok(U::try_from(value).map_err(|_| std::io::ErrorKind::InvalidData)?)
22    }
23
24    fn get_bool(&mut self) -> std::io::Result<bool> {
25        Ok(self.get_u1()?.into())
26    }
27
28    fn get_u1(&mut self) -> std::io::Result<u1> {
29        self.get_uN::<BigEndian, 1, u1, u8>()
30    }
31
32    fn get_u2(&mut self) -> std::io::Result<u2> {
33        self.get_uN::<BigEndian, 2, u2, u8>()
34    }
35
36    fn get_u3(&mut self) -> std::io::Result<u3> {
37        self.get_uN::<BigEndian, 3, u3, u8>()
38    }
39
40    fn get_u4(&mut self) -> std::io::Result<u4> {
41        self.get_uN::<BigEndian, 4, u4, u8>()
42    }
43
44    fn get_u5(&mut self) -> std::io::Result<u5> {
45        self.get_uN::<BigEndian, 5, u5, u8>()
46    }
47
48    fn get_u6(&mut self) -> std::io::Result<u6> {
49        self.get_uN::<BigEndian, 6, u6, u8>()
50    }
51
52    fn get_u7(&mut self) -> std::io::Result<u7> {
53        self.get_uN::<BigEndian, 7, u7, u8>()
54    }
55
56    fn get_u8(&mut self) -> std::io::Result<u8> {
57        self.get_uN::<BigEndian, 8, u8, u8>()
58    }
59
60    fn get_u9<O: ByteOrder>(&mut self) -> std::io::Result<u9> {
61        self.get_uN::<O, 9, u9, u16>()
62    }
63    fn get_u10<O: ByteOrder>(&mut self) -> std::io::Result<u10> {
64        self.get_uN::<O, 10, u10, u16>()
65    }
66    fn get_u11<O: ByteOrder>(&mut self) -> std::io::Result<u11> {
67        self.get_uN::<O, 11, u11, u16>()
68    }
69    fn get_u12<O: ByteOrder>(&mut self) -> std::io::Result<u12> {
70        self.get_uN::<O, 12, u12, u16>()
71    }
72    fn get_u13<O: ByteOrder>(&mut self) -> std::io::Result<u13> {
73        self.get_uN::<O, 13, u13, u16>()
74    }
75    fn get_u14<O: ByteOrder>(&mut self) -> std::io::Result<u14> {
76        self.get_uN::<O, 14, u14, u16>()
77    }
78    fn get_u15<O: ByteOrder>(&mut self) -> std::io::Result<u15> {
79        self.get_uN::<O, 15, u15, u16>()
80    }
81    fn get_u16<O: ByteOrder>(&mut self) -> std::io::Result<u16> {
82        self.get_uN::<O, 16, u16, u16>()
83    }
84    fn get_u17<O: ByteOrder>(&mut self) -> std::io::Result<u17> {
85        self.get_uN::<O, 17, u17, u32>()
86    }
87    fn get_u18<O: ByteOrder>(&mut self) -> std::io::Result<u18> {
88        self.get_uN::<O, 18, u18, u32>()
89    }
90    fn get_u19<O: ByteOrder>(&mut self) -> std::io::Result<u19> {
91        self.get_uN::<O, 19, u19, u32>()
92    }
93    fn get_u20<O: ByteOrder>(&mut self) -> std::io::Result<u20> {
94        self.get_uN::<O, 20, u20, u32>()
95    }
96    fn get_u21<O: ByteOrder>(&mut self) -> std::io::Result<u21> {
97        self.get_uN::<O, 21, u21, u32>()
98    }
99    fn get_u22<O: ByteOrder>(&mut self) -> std::io::Result<u22> {
100        self.get_uN::<O, 22, u22, u32>()
101    }
102    fn get_u23<O: ByteOrder>(&mut self) -> std::io::Result<u23> {
103        self.get_uN::<O, 23, u23, u32>()
104    }
105    fn get_u24<O: ByteOrder>(&mut self) -> std::io::Result<u24> {
106        self.get_uN::<O, 24, u24, u32>()
107    }
108    fn get_u25<O: ByteOrder>(&mut self) -> std::io::Result<u25> {
109        self.get_uN::<O, 25, u25, u32>()
110    }
111    fn get_u26<O: ByteOrder>(&mut self) -> std::io::Result<u26> {
112        self.get_uN::<O, 26, u26, u32>()
113    }
114    fn get_u27<O: ByteOrder>(&mut self) -> std::io::Result<u27> {
115        self.get_uN::<O, 27, u27, u32>()
116    }
117    fn get_u28<O: ByteOrder>(&mut self) -> std::io::Result<u28> {
118        self.get_uN::<O, 28, u28, u32>()
119    }
120    fn get_u29<O: ByteOrder>(&mut self) -> std::io::Result<u29> {
121        self.get_uN::<O, 29, u29, u32>()
122    }
123    fn get_u30<O: ByteOrder>(&mut self) -> std::io::Result<u30> {
124        self.get_uN::<O, 30, u30, u32>()
125    }
126    fn get_u31<O: ByteOrder>(&mut self) -> std::io::Result<u31> {
127        self.get_uN::<O, 31, u31, u32>()
128    }
129    fn get_u32<O: ByteOrder>(&mut self) -> std::io::Result<u32> {
130        self.get_uN::<O, 32, u32, u32>()
131    }
132}
133
134impl<T: BitBuf + ?Sized> BitBufExts for T {}
135
136#[cfg(test)]
137mod tests {
138    use crate::buf::bits::Bits;
139
140    use super::*;
141
142    #[test]
143    fn test_bit_buf_exts() {
144        let mut bits = Bits::copy_from_bit_slice(bits![0, 1, 1, 0, 0, 1, 1, 1]);
145
146        let value = bits.get_u4().unwrap();
147        assert_eq!(value, u4::new(0b0110));
148        let value = bits.get_u1().unwrap();
149        assert_eq!(value, u1::new(0));
150        let value = bits.get_u3().unwrap();
151        assert_eq!(value, u3::new(0b111));
152    }
153
154    #[test]
155    fn test_get_big_endian() {
156        let u9_data = bits![1, 0, 1, 0, 1, 0, 1, 0, 1];
157        let mut bits = Bits::copy_from_bit_slice(u9_data);
158        assert_eq!(bits.get_u9::<BigEndian>().unwrap(), u9::new(0b101010101));
159
160        let u10_data = bits![1, 0, 1, 0, 1, 0, 1, 0, 1, 1];
161        let mut bits = Bits::copy_from_bit_slice(u10_data);
162        assert_eq!(bits.get_u10::<BigEndian>().unwrap(), u10::new(0b1010101011));
163
164        let u11_data = bits![0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0];
165        let mut bits = Bits::copy_from_bit_slice(u11_data);
166        assert_eq!(
167            bits.get_u11::<BigEndian>().unwrap(),
168            u11::new(0b00110011000)
169        );
170
171        let u12_data = bits![0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0];
172        let mut bits = Bits::copy_from_bit_slice(u12_data);
173        assert_eq!(
174            bits.get_u12::<BigEndian>().unwrap(),
175            u12::new(0b011111111000)
176        );
177    }
178
179    #[test]
180    fn test_get_little_endian() {
181        // Little-endian form of 1_10101011
182        let u9_data = bits![1, 0, 1, 0, 1, 0, 1, 1, 1];
183        let mut bits = Bits::copy_from_bit_slice(u9_data);
184        assert_eq!(
185            bits.get_u9::<LittleEndian>().unwrap(),
186            u9::new(0b1_10101011)
187        );
188
189        // Little-endian form of 11_10101011
190        let u10_data = bits![1, 0, 1, 0, 1, 0, 1, 1, 1, 1];
191        let mut bits = Bits::copy_from_bit_slice(u10_data);
192        assert_eq!(
193            bits.get_u10::<LittleEndian>().unwrap(),
194            u10::new(0b11_10101011)
195        );
196
197        // Little-endian form of 110_10101011
198        let u11_data = bits![1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0];
199        let mut bits = Bits::copy_from_bit_slice(u11_data);
200        assert_eq!(
201            bits.get_u11::<LittleEndian>().unwrap(),
202            u11::new(0b110_10101011)
203        );
204
205        // Little-endian form of 11001101_11101111
206        let u16_data = bits![1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1];
207        let mut bits = Bits::copy_from_bit_slice(u16_data);
208        assert_eq!(
209            bits.get_u16::<LittleEndian>().unwrap(),
210            0b11001101_11101111u16
211        );
212
213        // Little-endian form of 11001101_11101111
214        let u20_data = bits![1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0];
215        let mut bits = Bits::copy_from_bit_slice(u20_data);
216        assert_eq!(
217            bits.get_u20::<LittleEndian>().unwrap(),
218            u20::new(0b1010_10111100_11011110)
219        );
220    }
221}