bits_io/
bit_read_exts.rs

1use crate::byte_order::{BigEndian, ByteOrder};
2use crate::prelude::*;
3use nsw_types::*;
4
5#[allow(non_snake_case)]
6fn read_uN<R: BitRead + ?Sized, B: ByteOrder>(reader: &mut R, n: usize) -> std::io::Result<u32> {
7    let mut tmp = BitVec::repeat(false, n);
8    reader.read_bits_exact(tmp.as_mut_bitslice())?;
9    Ok(B::read_u32_from_bits(tmp.as_bitslice()))
10}
11
12/// A trait which extends BitRead to add explicit read methods for non-standard-width types
13pub trait BitReadExts: BitRead {
14    fn read_bool(&mut self) -> std::io::Result<bool> {
15        Ok(self.read_u1()? != 0)
16    }
17
18    fn read_u1(&mut self) -> std::io::Result<u1> {
19        read_uN::<_, BigEndian>(self, 1).map(|v| u1::new(v as u8))
20    }
21
22    fn read_u2(&mut self) -> std::io::Result<u2> {
23        read_uN::<_, BigEndian>(self, 2).map(|v| u2::new(v as u8))
24    }
25
26    fn read_u3(&mut self) -> std::io::Result<u3> {
27        read_uN::<_, BigEndian>(self, 3).map(|v| u3::new(v as u8))
28    }
29
30    fn read_u4(&mut self) -> std::io::Result<u4> {
31        read_uN::<_, BigEndian>(self, 4).map(|v| u4::new(v as u8))
32    }
33
34    fn read_u5(&mut self) -> std::io::Result<u5> {
35        read_uN::<_, BigEndian>(self, 5).map(|v| u5::new(v as u8))
36    }
37
38    fn read_u6(&mut self) -> std::io::Result<u6> {
39        read_uN::<_, BigEndian>(self, 6).map(|v| u6::new(v as u8))
40    }
41
42    fn read_u7(&mut self) -> std::io::Result<u7> {
43        read_uN::<_, BigEndian>(self, 7).map(|v| u7::new(v as u8))
44    }
45
46    fn read_u8(&mut self) -> std::io::Result<u8> {
47        read_uN::<_, BigEndian>(self, 8).map(|v| v as u8)
48    }
49
50    fn read_u9<B: ByteOrder>(&mut self) -> std::io::Result<u9> {
51        read_uN::<_, B>(self, 9).map(|v| u9::new(v as u16))
52    }
53
54    fn read_u10<B: ByteOrder>(&mut self) -> std::io::Result<u10> {
55        read_uN::<_, B>(self, 10).map(|v| u10::new(v as u16))
56    }
57
58    fn read_u11<B: ByteOrder>(&mut self) -> std::io::Result<u11> {
59        read_uN::<_, B>(self, 11).map(|v| u11::new(v as u16))
60    }
61
62    fn read_u12<B: ByteOrder>(&mut self) -> std::io::Result<u12> {
63        read_uN::<_, B>(self, 12).map(|v| u12::new(v as u16))
64    }
65
66    fn read_u13<B: ByteOrder>(&mut self) -> std::io::Result<u13> {
67        read_uN::<_, B>(self, 13).map(|v| u13::new(v as u16))
68    }
69
70    fn read_u14<B: ByteOrder>(&mut self) -> std::io::Result<u14> {
71        read_uN::<_, B>(self, 14).map(|v| u14::new(v as u16))
72    }
73
74    fn read_u15<B: ByteOrder>(&mut self) -> std::io::Result<u15> {
75        read_uN::<_, B>(self, 15).map(|v| u15::new(v as u16))
76    }
77
78    fn read_u16<B: ByteOrder>(&mut self) -> std::io::Result<u16> {
79        read_uN::<_, B>(self, 16).map(|v| v as u16)
80    }
81
82    fn read_u17<B: ByteOrder>(&mut self) -> std::io::Result<u17> {
83        read_uN::<_, B>(self, 17).map(u17::new)
84    }
85
86    fn read_u18<B: ByteOrder>(&mut self) -> std::io::Result<u18> {
87        read_uN::<_, B>(self, 18).map(u18::new)
88    }
89
90    fn read_u19<B: ByteOrder>(&mut self) -> std::io::Result<u19> {
91        read_uN::<_, B>(self, 19).map(u19::new)
92    }
93
94    fn read_u20<B: ByteOrder>(&mut self) -> std::io::Result<u20> {
95        read_uN::<_, B>(self, 20).map(u20::new)
96    }
97
98    fn read_u21<B: ByteOrder>(&mut self) -> std::io::Result<u21> {
99        read_uN::<_, B>(self, 21).map(u21::new)
100    }
101
102    fn read_u22<B: ByteOrder>(&mut self) -> std::io::Result<u22> {
103        read_uN::<_, B>(self, 22).map(u22::new)
104    }
105
106    fn read_u23<B: ByteOrder>(&mut self) -> std::io::Result<u23> {
107        read_uN::<_, B>(self, 23).map(u23::new)
108    }
109
110    fn read_u24<B: ByteOrder>(&mut self) -> std::io::Result<u24> {
111        read_uN::<_, B>(self, 24).map(u24::new)
112    }
113
114    fn read_u25<B: ByteOrder>(&mut self) -> std::io::Result<u25> {
115        read_uN::<_, B>(self, 25).map(u25::new)
116    }
117
118    fn read_u26<B: ByteOrder>(&mut self) -> std::io::Result<u26> {
119        read_uN::<_, B>(self, 26).map(u26::new)
120    }
121
122    fn read_u27<B: ByteOrder>(&mut self) -> std::io::Result<u27> {
123        read_uN::<_, B>(self, 27).map(u27::new)
124    }
125
126    fn read_u28<B: ByteOrder>(&mut self) -> std::io::Result<u28> {
127        read_uN::<_, B>(self, 28).map(u28::new)
128    }
129
130    fn read_u29<B: ByteOrder>(&mut self) -> std::io::Result<u29> {
131        read_uN::<_, B>(self, 29).map(u29::new)
132    }
133
134    fn read_u30<B: ByteOrder>(&mut self) -> std::io::Result<u30> {
135        read_uN::<_, B>(self, 30).map(u30::new)
136    }
137
138    fn read_u31<B: ByteOrder>(&mut self) -> std::io::Result<u31> {
139        read_uN::<_, B>(self, 31).map(u31::new)
140    }
141
142    fn read_u32<B: ByteOrder>(&mut self) -> std::io::Result<u32> {
143        read_uN::<_, B>(self, 32)
144    }
145}
146
147impl<T: BitRead + ?Sized> BitReadExts for T {}