/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bitstream-io-1.10.0/src/read.rs
Line  | Count  | Source  | 
1  |  | // Copyright 2017 Brian Langenberger  | 
2  |  | //  | 
3  |  | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or  | 
4  |  | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license  | 
5  |  | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your  | 
6  |  | // option. This file may not be copied, modified, or distributed  | 
7  |  | // except according to those terms.  | 
8  |  |  | 
9  |  | //! Traits and implementations for reading bits from a stream.  | 
10  |  | //!  | 
11  |  | //! ## Example  | 
12  |  | //!  | 
13  |  | //! Reading the initial STREAMINFO block from a FLAC file,  | 
14  |  | //! as documented in its  | 
15  |  | //! [specification](https://xiph.org/flac/format.html#stream).  | 
16  |  | //!  | 
17  |  | //! ```  | 
18  |  | //! use std::io::{Cursor, Read}; | 
19  |  | //! use bitstream_io::{BigEndian, BitReader, BitRead, ByteReader, ByteRead, FromBitStream, FromByteStream, LittleEndian}; | 
20  |  | //!  | 
21  |  | //! let flac: Vec<u8> = vec![0x66,0x4c,0x61,0x43,0x00,0x00,0x00,0x22,  | 
22  |  | //!                          0x10,0x00,0x10,0x00,0x00,0x06,0x06,0x00,  | 
23  |  | //!                          0x21,0x62,0x0a,0xc4,0x42,0xf0,0x00,0x04,  | 
24  |  | //!                          0xa6,0xcc,0xfa,0xf2,0x69,0x2f,0xfd,0xec,  | 
25  |  | //!                          0x2d,0x5b,0x30,0x01,0x76,0xb4,0x62,0x88,  | 
26  |  | //!                          0x7d,0x92,0x04,0x00,0x00,0x7a,0x20,0x00,  | 
27  |  | //!                          0x00,0x00,0x72,0x65,0x66,0x65,0x72,0x65,  | 
28  |  | //!                          0x6e,0x63,0x65,0x20,0x6c,0x69,0x62,0x46,  | 
29  |  | //!                          0x4c,0x41,0x43,0x20,0x31,0x2e,0x31,0x2e,  | 
30  |  | //!                          0x34,0x20,0x32,0x30,0x30,0x37,0x30,0x32,  | 
31  |  | //!                          0x31,0x33,0x04,0x00,0x00,0x00,0x16,0x00,  | 
32  |  | //!                          0x00,0x00,0x74,0x69,0x74,0x6c,0x65,0x3d,  | 
33  |  | //!                          0x32,0x63,0x68,0x20,0x34,0x34,0x31,0x30,  | 
34  |  | //!                          0x30,0x20,0x20,0x31,0x36,0x62,0x69,0x74,  | 
35  |  | //!                          0x10,0x00,0x00,0x00,0x61,0x6c,0x62,0x75,  | 
36  |  | //!                          0x6d,0x3d,0x54,0x65,0x73,0x74,0x20,0x41,  | 
37  |  | //!                          0x6c,0x62,0x75,0x6d,0x0f,0x00,0x00,0x00,  | 
38  |  | //!                          0x61,0x72,0x74,0x69,0x73,0x74,0x3d,0x41,  | 
39  |  | //!                          0x73,0x73,0x6f,0x72,0x74,0x65,0x64,0x0d,  | 
40  |  | //!                          0x00,0x00,0x00,0x74,0x72,0x61,0x63,0x6b,  | 
41  |  | //!                          0x6e,0x75,0x6d,0x62,0x65,0x72,0x3d,0x31];  | 
42  |  | //!  | 
43  |  | //! #[derive(Debug, PartialEq, Eq)]  | 
44  |  | //! struct BlockHeader { | 
45  |  | //!     last_block: bool,  | 
46  |  | //!     block_type: u8,  | 
47  |  | //!     block_size: u32,  | 
48  |  | //! }  | 
49  |  | //!  | 
50  |  | //! impl FromBitStream for BlockHeader { | 
51  |  | //!     type Error = std::io::Error;  | 
52  |  | //!  | 
53  |  | //!     fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> std::io::Result<Self> { | 
54  |  | //!         Ok(Self { | 
55  |  | //!             last_block: r.read_bit()?,  | 
56  |  | //!             block_type: r.read(7)?,  | 
57  |  | //!             block_size: r.read(24)?,  | 
58  |  | //!         })  | 
59  |  | //!     }  | 
60  |  | //! }  | 
61  |  | //!  | 
62  |  | //! #[derive(Debug, PartialEq, Eq)]  | 
63  |  | //! struct Streaminfo { | 
64  |  | //!     minimum_block_size: u16,  | 
65  |  | //!     maximum_block_size: u16,  | 
66  |  | //!     minimum_frame_size: u32,  | 
67  |  | //!     maximum_frame_size: u32,  | 
68  |  | //!     sample_rate: u32,  | 
69  |  | //!     channels: u8,  | 
70  |  | //!     bits_per_sample: u8,  | 
71  |  | //!     total_samples: u64,  | 
72  |  | //!     md5: [u8; 16],  | 
73  |  | //! }  | 
74  |  | //!  | 
75  |  | //! impl FromBitStream for Streaminfo { | 
76  |  | //!     type Error = std::io::Error;  | 
77  |  | //!  | 
78  |  | //!     fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> std::io::Result<Self> { | 
79  |  | //!         Ok(Self { | 
80  |  | //!             minimum_block_size: r.read_to()?,  | 
81  |  | //!             maximum_block_size: r.read_to()?,  | 
82  |  | //!             minimum_frame_size: r.read(24)?,  | 
83  |  | //!             maximum_frame_size: r.read(24)?,  | 
84  |  | //!             sample_rate: r.read(20)?,  | 
85  |  | //!             channels: r.read::<u8>(3)? + 1,  | 
86  |  | //!             bits_per_sample: r.read::<u8>(5)? + 1,  | 
87  |  | //!             total_samples: r.read(36)?,  | 
88  |  | //!             md5: r.read_to()?,  | 
89  |  | //!         })  | 
90  |  | //!     }  | 
91  |  | //! }  | 
92  |  | //!  | 
93  |  | //! #[derive(Debug, PartialEq, Eq)]  | 
94  |  | //! struct VorbisComment { | 
95  |  | //!     vendor: String,  | 
96  |  | //!     comment: Vec<String>,  | 
97  |  | //! }  | 
98  |  | //!  | 
99  |  | //! impl FromByteStream for VorbisComment { | 
100  |  | //!    type Error = Box<dyn std::error::Error>;  | 
101  |  | //!  | 
102  |  | //!    fn from_reader<R: ByteRead + ?Sized>(r: &mut R) -> Result<Self, Self::Error> { | 
103  |  | //!        fn read_entry<R: ByteRead + ?Sized>(  | 
104  |  | //!            r: &mut R,  | 
105  |  | //!        ) -> Result<String, Box<dyn std::error::Error>> { | 
106  |  | //!            use std::convert::TryInto;  | 
107  |  | //!            let size = r.read::<u32>()?.try_into()?;  | 
108  |  | //!            Ok(String::from_utf8(r.read_to_vec(size)?)?)  | 
109  |  | //!        }  | 
110  |  | //!  | 
111  |  | //!        Ok(Self { | 
112  |  | //!            vendor: read_entry(r)?,  | 
113  |  | //!            comment: (0..r.read::<u32>()?)  | 
114  |  | //!                .map(|_| read_entry(r))  | 
115  |  | //!                .collect::<Result<Vec<_>, _>>()?,  | 
116  |  | //!        })  | 
117  |  | //!    }  | 
118  |  | //! }  | 
119  |  | //!  | 
120  |  | //! let mut cursor = Cursor::new(&flac);  | 
121  |  | //!  | 
122  |  | //! let mut reader = BitReader::endian(&mut cursor, BigEndian);  | 
123  |  | //!  | 
124  |  | //! // stream marker  | 
125  |  | //! assert_eq!(&reader.read_to::<[u8; 4]>().unwrap(), b"fLaC");  | 
126  |  | //!  | 
127  |  | //! // metadata block header  | 
128  |  | //! assert_eq!(  | 
129  |  | //!     reader.parse::<BlockHeader>().unwrap(),  | 
130  |  | //!     BlockHeader { last_block: false, block_type: 0, block_size: 34 } | 
131  |  | //! );  | 
132  |  | //!  | 
133  |  | //! // STREAMINFO block  | 
134  |  | //! assert_eq!(  | 
135  |  | //!     reader.parse::<Streaminfo>().unwrap(),  | 
136  |  | //!     Streaminfo { | 
137  |  | //!         minimum_block_size: 4096,  | 
138  |  | //!         maximum_block_size: 4096,  | 
139  |  | //!         minimum_frame_size: 1542,  | 
140  |  | //!         maximum_frame_size: 8546,  | 
141  |  | //!         sample_rate: 44100,  | 
142  |  | //!         channels: 2,  | 
143  |  | //!         bits_per_sample: 16,  | 
144  |  | //!         total_samples: 304844,  | 
145  |  | //!         md5: *b"\xFA\xF2\x69\x2F\xFD\xEC\x2D\x5B\x30\x01\x76\xB4\x62\x88\x7D\x92",  | 
146  |  | //!     }  | 
147  |  | //! );  | 
148  |  | //!  | 
149  |  | //! // metadata block header  | 
150  |  | //! assert_eq!(  | 
151  |  | //!     reader.parse::<BlockHeader>().unwrap(),  | 
152  |  | //!     BlockHeader { last_block: false, block_type: 4, block_size: 122 } | 
153  |  | //! );  | 
154  |  | //!  | 
155  |  | //! // VORBIS_COMMENT block (little endian)  | 
156  |  | //! assert_eq!(  | 
157  |  | //!    ByteReader::endian(reader.reader().unwrap(), LittleEndian).parse::<VorbisComment>().unwrap(),  | 
158  |  | //!    VorbisComment { | 
159  |  | //!        vendor: "reference libFLAC 1.1.4 20070213".to_string(),  | 
160  |  | //!        comment: vec![  | 
161  |  | //!            "title=2ch 44100  16bit".to_string(),  | 
162  |  | //!            "album=Test Album".to_string(),  | 
163  |  | //!            "artist=Assorted".to_string(),  | 
164  |  | //!            "tracknumber=1".to_string(),  | 
165  |  | //!        ],  | 
166  |  | //!    }  | 
167  |  | //! );  | 
168  |  |  | 
169  |  | #![warn(missing_docs)]  | 
170  |  |  | 
171  |  | use std::io;  | 
172  |  |  | 
173  |  | use super::{ | 
174  |  |     huffman::ReadHuffmanTree, BitQueue, Endianness, Numeric, PhantomData, Primitive, SignedNumeric,  | 
175  |  | };  | 
176  |  |  | 
177  |  | /// A trait for anything that can read a variable number of  | 
178  |  | /// potentially un-aligned values from an input stream  | 
179  |  | pub trait BitRead { | 
180  |  |     /// Reads a single bit from the stream.  | 
181  |  |     /// `true` indicates 1, `false` indicates 0  | 
182  |  |     ///  | 
183  |  |     /// # Errors  | 
184  |  |     ///  | 
185  |  |     /// Passes along any I/O error from the underlying stream.  | 
186  |  |     fn read_bit(&mut self) -> io::Result<bool>;  | 
187  |  |  | 
188  |  |     /// Reads an unsigned value from the stream with  | 
189  |  |     /// the given number of bits.  | 
190  |  |     ///  | 
191  |  |     /// # Errors  | 
192  |  |     ///  | 
193  |  |     /// Passes along any I/O error from the underlying stream.  | 
194  |  |     /// Also returns an error if the output type is too small  | 
195  |  |     /// to hold the requested number of bits.  | 
196  |  |     fn read<U>(&mut self, bits: u32) -> io::Result<U>  | 
197  |  |     where  | 
198  |  |         U: Numeric;  | 
199  |  |  | 
200  |  |     /// Reads a twos-complement signed value from the stream with  | 
201  |  |     /// the given number of bits.  | 
202  |  |     ///  | 
203  |  |     /// # Errors  | 
204  |  |     ///  | 
205  |  |     /// Passes along any I/O error from the underlying stream.  | 
206  |  |     /// Also returns an error if the output type is too small  | 
207  |  |     /// to hold the requested number of bits.  | 
208  |  |     fn read_signed<S>(&mut self, bits: u32) -> io::Result<S>  | 
209  |  |     where  | 
210  |  |         S: SignedNumeric;  | 
211  |  |  | 
212  |  |     /// Reads whole value from the stream whose size in bits is equal  | 
213  |  |     /// to its type's size.  | 
214  |  |     ///  | 
215  |  |     /// # Errors  | 
216  |  |     ///  | 
217  |  |     /// Passes along any I/O error from the underlying stream.  | 
218  |  |     fn read_to<V>(&mut self) -> io::Result<V>  | 
219  |  |     where  | 
220  |  |         V: Primitive;  | 
221  |  |  | 
222  |  |     /// Skips the given number of bits in the stream.  | 
223  |  |     /// Since this method does not need an accumulator,  | 
224  |  |     /// it may be slightly faster than reading to an empty variable.  | 
225  |  |     /// In addition, since there is no accumulator,  | 
226  |  |     /// there is no upper limit on the number of bits  | 
227  |  |     /// which may be skipped.  | 
228  |  |     /// These bits are still read from the stream, however,  | 
229  |  |     /// and are never skipped via a `seek` method.  | 
230  |  |     ///  | 
231  |  |     /// # Errors  | 
232  |  |     ///  | 
233  |  |     /// Passes along any I/O error from the underlying stream.  | 
234  |  |     fn skip(&mut self, bits: u32) -> io::Result<()>;  | 
235  |  |  | 
236  |  |     /// Completely fills the given buffer with whole bytes.  | 
237  |  |     /// If the stream is already byte-aligned, it will map  | 
238  |  |     /// to a faster `read_exact` call.  Otherwise it will read  | 
239  |  |     /// bytes individually in 8-bit increments.  | 
240  |  |     ///  | 
241  |  |     /// # Errors  | 
242  |  |     ///  | 
243  |  |     /// Passes along any I/O error from the underlying stream.  | 
244  | 0  |     fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> { | 
245  | 0  |         for b in buf.iter_mut() { | 
246  | 0  |             *b = self.read(8)?;  | 
247  |  |         }  | 
248  | 0  |         Ok(())  | 
249  | 0  |     }  | 
250  |  |  | 
251  |  |     /// Completely fills a whole buffer with bytes and returns it.  | 
252  |  |     /// If the stream is already byte-aligned, it will map  | 
253  |  |     /// to a faster `read_exact` call.  Otherwise it will read  | 
254  |  |     /// bytes individually in 8-bit increments.  | 
255  |  |     ///  | 
256  |  |     /// # Errors  | 
257  |  |     ///  | 
258  |  |     /// Passes along any I/O error from the underlying stream.  | 
259  |  |     #[inline(always)]  | 
260  |  |     #[deprecated(since = "1.8.0", note = "use read_to() method instead")]  | 
261  | 0  |     fn read_to_bytes<const SIZE: usize>(&mut self) -> io::Result<[u8; SIZE]> { | 
262  | 0  |         self.read_to()  | 
263  | 0  |     }  | 
264  |  |  | 
265  |  |     /// Completely fills a vector of bytes and returns it.  | 
266  |  |     /// If the stream is already byte-aligned, it will map  | 
267  |  |     /// to a faster `read_exact` call.  Otherwise it will read  | 
268  |  |     /// bytes individually in 8-bit increments.  | 
269  |  |     ///  | 
270  |  |     /// # Errors  | 
271  |  |     ///  | 
272  |  |     /// Passes along any I/O error from the underlying stream.  | 
273  | 0  |     fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> { | 
274  | 0  |         let mut buf = vec![0; bytes];  | 
275  | 0  |         self.read_bytes(&mut buf)?;  | 
276  | 0  |         Ok(buf)  | 
277  | 0  |     }  | 
278  |  |  | 
279  |  |     /// Counts the number of 1 bits in the stream until the next  | 
280  |  |     /// 0 bit and returns the amount read.  | 
281  |  |     /// Because this field is variably-sized and may be large,  | 
282  |  |     /// its output is always a `u32` type.  | 
283  |  |     ///  | 
284  |  |     /// # Errors  | 
285  |  |     ///  | 
286  |  |     /// Passes along any I/O error from the underlying stream.  | 
287  | 0  |     fn read_unary0(&mut self) -> io::Result<u32> { | 
288  | 0  |         let mut unary = 0;  | 
289  | 0  |         while self.read_bit()? { | 
290  | 0  |             unary += 1;  | 
291  | 0  |         }  | 
292  | 0  |         Ok(unary)  | 
293  | 0  |     }  | 
294  |  |  | 
295  |  |     /// Counts the number of 0 bits in the stream until the next  | 
296  |  |     /// 1 bit and returns the amount read.  | 
297  |  |     /// Because this field is variably-sized and may be large,  | 
298  |  |     /// its output is always a `u32` type.  | 
299  |  |     ///  | 
300  |  |     /// # Errors  | 
301  |  |     ///  | 
302  |  |     /// Passes along any I/O error from the underlying stream.  | 
303  | 0  |     fn read_unary1(&mut self) -> io::Result<u32> { | 
304  | 0  |         let mut unary = 0;  | 
305  | 0  |         while !(self.read_bit()?) { | 
306  | 0  |             unary += 1;  | 
307  | 0  |         }  | 
308  | 0  |         Ok(unary)  | 
309  | 0  |     }  | 
310  |  |  | 
311  |  |     /// Parses and returns complex type  | 
312  | 0  |     fn parse<F: FromBitStream>(&mut self) -> Result<F, F::Error> { | 
313  | 0  |         F::from_reader(self)  | 
314  | 0  |     }  | 
315  |  |  | 
316  |  |     /// Parses and returns complex type with context  | 
317  | 0  |     fn parse_with<F: FromBitStreamWith>(&mut self, context: &F::Context) -> Result<F, F::Error> { | 
318  | 0  |         F::from_reader(self, context)  | 
319  | 0  |     }  | 
320  |  |  | 
321  |  |     /// Returns true if the stream is aligned at a whole byte.  | 
322  |  |     fn byte_aligned(&self) -> bool;  | 
323  |  |  | 
324  |  |     /// Throws away all unread bit values until the next whole byte.  | 
325  |  |     /// Does nothing if the stream is already aligned.  | 
326  |  |     fn byte_align(&mut self);  | 
327  |  | }  | 
328  |  |  | 
329  |  | /// A trait for anything that can read Huffman codes  | 
330  |  | /// of a given endianness from an input stream  | 
331  |  | pub trait HuffmanRead<E: Endianness> { | 
332  |  |     /// Given a compiled Huffman tree, reads bits from the stream  | 
333  |  |     /// until the next symbol is encountered.  | 
334  |  |     ///  | 
335  |  |     /// # Errors  | 
336  |  |     ///  | 
337  |  |     /// Passes along any I/O error from the underlying stream.  | 
338  |  |     fn read_huffman<T>(&mut self, tree: &[ReadHuffmanTree<E, T>]) -> io::Result<T>  | 
339  |  |     where  | 
340  |  |         T: Clone;  | 
341  |  | }  | 
342  |  |  | 
343  |  | /// For reading non-aligned bits from a stream of bytes in a given endianness.  | 
344  |  | ///  | 
345  |  | /// This will read exactly as many whole bytes needed to return  | 
346  |  | /// the requested number of bits.  It may cache up to a single partial byte  | 
347  |  | /// but no more.  | 
348  |  | #[derive(Clone)]  | 
349  |  | pub struct BitReader<R: io::Read, E: Endianness> { | 
350  |  |     reader: R,  | 
351  |  |     bitqueue: BitQueue<E, u8>,  | 
352  |  | }  | 
353  |  |  | 
354  |  | impl<R: io::Read, E: Endianness> BitReader<R, E> { | 
355  |  |     /// Wraps a BitReader around something that implements `Read`  | 
356  | 36.7k  |     pub fn new(reader: R) -> BitReader<R, E> { | 
357  | 36.7k  |         BitReader { | 
358  | 36.7k  |             reader,  | 
359  | 36.7k  |             bitqueue: BitQueue::new(),  | 
360  | 36.7k  |         }  | 
361  | 36.7k  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian>>::new Line  | Count  | Source  |  356  | 3.60k  |     pub fn new(reader: R) -> BitReader<R, E> { |  357  | 3.60k  |         BitReader { |  358  | 3.60k  |             reader,  |  359  | 3.60k  |             bitqueue: BitQueue::new(),  |  360  | 3.60k  |         }  |  361  | 3.60k  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian>>::new Line  | Count  | Source  |  356  | 33.1k  |     pub fn new(reader: R) -> BitReader<R, E> { |  357  | 33.1k  |         BitReader { |  358  | 33.1k  |             reader,  |  359  | 33.1k  |             bitqueue: BitQueue::new(),  |  360  | 33.1k  |         }  |  361  | 33.1k  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _>>::new  | 
362  |  |  | 
363  |  |     /// Wraps a BitReader around something that implements `Read`  | 
364  |  |     /// with the given endianness.  | 
365  | 0  |     pub fn endian(reader: R, _endian: E) -> BitReader<R, E> { | 
366  | 0  |         BitReader { | 
367  | 0  |             reader,  | 
368  | 0  |             bitqueue: BitQueue::new(),  | 
369  | 0  |         }  | 
370  | 0  |     }  | 
371  |  |  | 
372  |  |     /// Unwraps internal reader and disposes of BitReader.  | 
373  |  |     ///  | 
374  |  |     /// # Warning  | 
375  |  |     ///  | 
376  |  |     /// Any unread partial bits are discarded.  | 
377  |  |     #[inline]  | 
378  | 13.6k  |     pub fn into_reader(self) -> R { | 
379  | 13.6k  |         self.reader  | 
380  | 13.6k  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian>>::into_reader Line  | Count  | Source  |  378  | 13.6k  |     pub fn into_reader(self) -> R { |  379  | 13.6k  |         self.reader  |  380  | 13.6k  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _>>::into_reader  | 
381  |  |  | 
382  |  |     /// If stream is byte-aligned, provides mutable reference  | 
383  |  |     /// to internal reader.  Otherwise returns `None`  | 
384  |  |     #[inline]  | 
385  | 0  |     pub fn reader(&mut self) -> Option<&mut R> { | 
386  | 0  |         if self.byte_aligned() { | 
387  | 0  |             Some(&mut self.reader)  | 
388  |  |         } else { | 
389  | 0  |             None  | 
390  |  |         }  | 
391  | 0  |     }  | 
392  |  |  | 
393  |  |     /// Converts `BitReader` to `ByteReader` in the same endianness.  | 
394  |  |     ///  | 
395  |  |     /// # Warning  | 
396  |  |     ///  | 
397  |  |     /// Any unread partial bits are discarded.  | 
398  |  |     #[inline]  | 
399  | 0  |     pub fn into_bytereader(self) -> ByteReader<R, E> { | 
400  | 0  |         ByteReader::new(self.into_reader())  | 
401  | 0  |     }  | 
402  |  |  | 
403  |  |     /// If stream is byte-aligned, provides temporary `ByteReader`  | 
404  |  |     /// in the same endianness.  Otherwise returns `None`  | 
405  |  |     ///  | 
406  |  |     /// # Warning  | 
407  |  |     ///  | 
408  |  |     /// Any reader bits left over when `ByteReader` is dropped are lost.  | 
409  |  |     #[inline]  | 
410  | 0  |     pub fn bytereader(&mut self) -> Option<ByteReader<&mut R, E>> { | 
411  | 0  |         self.reader().map(ByteReader::new)  | 
412  | 0  |     }  | 
413  |  |  | 
414  |  |     /// Consumes reader and returns any un-read partial byte  | 
415  |  |     /// as a `(bits, value)` tuple.  | 
416  |  |     ///  | 
417  |  |     /// # Examples  | 
418  |  |     /// ```  | 
419  |  |     /// use std::io::{Read, Cursor}; | 
420  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
421  |  |     /// let data = [0b1010_0101, 0b0101_1010];  | 
422  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
423  |  |     /// assert_eq!(reader.read::<u16>(9).unwrap(), 0b1010_0101_0);  | 
424  |  |     /// let (bits, value) = reader.into_unread();  | 
425  |  |     /// assert_eq!(bits, 7);  | 
426  |  |     /// assert_eq!(value, 0b101_1010);  | 
427  |  |     /// ```  | 
428  |  |     ///  | 
429  |  |     /// ```  | 
430  |  |     /// use std::io::{Read, Cursor}; | 
431  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
432  |  |     /// let data = [0b1010_0101, 0b0101_1010];  | 
433  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
434  |  |     /// assert_eq!(reader.read::<u16>(8).unwrap(), 0b1010_0101);  | 
435  |  |     /// let (bits, value) = reader.into_unread();  | 
436  |  |     /// assert_eq!(bits, 0);  | 
437  |  |     /// assert_eq!(value, 0);  | 
438  |  |     /// ```  | 
439  |  |     #[inline]  | 
440  | 0  |     pub fn into_unread(self) -> (u32, u8) { | 
441  | 0  |         (self.bitqueue.len(), self.bitqueue.value())  | 
442  | 0  |     }  | 
443  |  | }  | 
444  |  |  | 
445  |  | impl<R: io::Read, E: Endianness> BitRead for BitReader<R, E> { | 
446  |  |     /// # Examples  | 
447  |  |     ///  | 
448  |  |     /// ```  | 
449  |  |     /// use std::io::{Read, Cursor}; | 
450  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
451  |  |     /// let data = [0b10110111];  | 
452  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
453  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
454  |  |     /// assert_eq!(reader.read_bit().unwrap(), false);  | 
455  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
456  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
457  |  |     /// assert_eq!(reader.read_bit().unwrap(), false);  | 
458  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
459  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
460  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
461  |  |     /// ```  | 
462  |  |     ///  | 
463  |  |     /// ```  | 
464  |  |     /// use std::io::{Read, Cursor}; | 
465  |  |     /// use bitstream_io::{LittleEndian, BitReader, BitRead}; | 
466  |  |     /// let data = [0b10110111];  | 
467  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), LittleEndian);  | 
468  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
469  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
470  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
471  |  |     /// assert_eq!(reader.read_bit().unwrap(), false);  | 
472  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
473  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
474  |  |     /// assert_eq!(reader.read_bit().unwrap(), false);  | 
475  |  |     /// assert_eq!(reader.read_bit().unwrap(), true);  | 
476  |  |     /// ```  | 
477  |  |     #[inline(always)]  | 
478  | 3.80M  |     fn read_bit(&mut self) -> io::Result<bool> { | 
479  | 3.80M  |         if self.bitqueue.is_empty() { | 
480  | 539k  |             self.bitqueue.set(read_byte(&mut self.reader)?, 8);  | 
481  | 3.26M  |         }  | 
482  | 3.80M  |         Ok(self.bitqueue.pop(1) == 1)  | 
483  | 3.80M  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read_bit Line  | Count  | Source  |  478  | 3.59k  |     fn read_bit(&mut self) -> io::Result<bool> { |  479  | 3.59k  |         if self.bitqueue.is_empty() { |  480  | 0  |             self.bitqueue.set(read_byte(&mut self.reader)?, 8);  |  481  | 3.59k  |         }  |  482  | 3.59k  |         Ok(self.bitqueue.pop(1) == 1)  |  483  | 3.59k  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read_bit Line  | Count  | Source  |  478  | 3.80M  |     fn read_bit(&mut self) -> io::Result<bool> { |  479  | 3.80M  |         if self.bitqueue.is_empty() { |  480  | 539k  |             self.bitqueue.set(read_byte(&mut self.reader)?, 8);  |  481  | 3.26M  |         }  |  482  | 3.80M  |         Ok(self.bitqueue.pop(1) == 1)  |  483  | 3.80M  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _> as bitstream_io::read::BitRead>::read_bit  | 
484  |  |  | 
485  |  |     /// # Examples  | 
486  |  |     /// ```  | 
487  |  |     /// use std::io::{Read, Cursor}; | 
488  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
489  |  |     /// let data = [0b10110111];  | 
490  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
491  |  |     /// assert_eq!(reader.read::<u8>(1).unwrap(), 0b1);  | 
492  |  |     /// assert_eq!(reader.read::<u8>(2).unwrap(), 0b01);  | 
493  |  |     /// assert_eq!(reader.read::<u8>(5).unwrap(), 0b10111);  | 
494  |  |     /// ```  | 
495  |  |     ///  | 
496  |  |     /// ```  | 
497  |  |     /// use std::io::{Read, Cursor}; | 
498  |  |     /// use bitstream_io::{LittleEndian, BitReader, BitRead}; | 
499  |  |     /// let data = [0b10110111];  | 
500  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), LittleEndian);  | 
501  |  |     /// assert_eq!(reader.read::<u8>(1).unwrap(), 0b1);  | 
502  |  |     /// assert_eq!(reader.read::<u8>(2).unwrap(), 0b11);  | 
503  |  |     /// assert_eq!(reader.read::<u8>(5).unwrap(), 0b10110);  | 
504  |  |     /// ```  | 
505  |  |     ///  | 
506  |  |     /// ```  | 
507  |  |     /// use std::io::{Read, Cursor}; | 
508  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
509  |  |     /// let data = [0;10];  | 
510  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
511  |  |     /// assert!(reader.read::<u8>(9).is_err());    // can't read  9 bits to u8  | 
512  |  |     /// assert!(reader.read::<u16>(17).is_err());  // can't read 17 bits to u16  | 
513  |  |     /// assert!(reader.read::<u32>(33).is_err());  // can't read 33 bits to u32  | 
514  |  |     /// assert!(reader.read::<u64>(65).is_err());  // can't read 65 bits to u64  | 
515  |  |     /// ```  | 
516  | 4.46M  |     fn read<U>(&mut self, mut bits: u32) -> io::Result<U>  | 
517  | 4.46M  |     where  | 
518  | 4.46M  |         U: Numeric,  | 
519  |  |     { | 
520  | 4.46M  |         if bits <= U::BITS_SIZE { | 
521  | 4.46M  |             let bitqueue_len = self.bitqueue.len();  | 
522  | 4.46M  |             if bits <= bitqueue_len { | 
523  | 2.36M  |                 Ok(U::from_u8(self.bitqueue.pop(bits)))  | 
524  |  |             } else { | 
525  | 2.10M  |                 let mut acc =  | 
526  | 2.10M  |                     BitQueue::from_value(U::from_u8(self.bitqueue.pop_all()), bitqueue_len);  | 
527  | 2.10M  |                 bits -= bitqueue_len;  | 
528  |  |  | 
529  | 2.10M  |                 read_aligned(&mut self.reader, bits / 8, &mut acc)?;  | 
530  | 2.10M  |                 read_unaligned(&mut self.reader, bits % 8, &mut acc, &mut self.bitqueue)?;  | 
531  | 2.10M  |                 Ok(acc.value())  | 
532  |  |             }  | 
533  |  |         } else { | 
534  | 0  |             Err(io::Error::new(  | 
535  | 0  |                 io::ErrorKind::InvalidInput,  | 
536  | 0  |                 "excessive bits for type read",  | 
537  | 0  |             ))  | 
538  |  |         }  | 
539  | 4.46M  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read::<u8> Line  | Count  | Source  |  516  | 7.19k  |     fn read<U>(&mut self, mut bits: u32) -> io::Result<U>  |  517  | 7.19k  |     where  |  518  | 7.19k  |         U: Numeric,  |  519  |  |     { |  520  | 7.19k  |         if bits <= U::BITS_SIZE { |  521  | 7.19k  |             let bitqueue_len = self.bitqueue.len();  |  522  | 7.19k  |             if bits <= bitqueue_len { |  523  | 3.59k  |                 Ok(U::from_u8(self.bitqueue.pop(bits)))  |  524  |  |             } else { |  525  | 3.60k  |                 let mut acc =  |  526  | 3.60k  |                     BitQueue::from_value(U::from_u8(self.bitqueue.pop_all()), bitqueue_len);  |  527  | 3.60k  |                 bits -= bitqueue_len;  |  528  |  |  |  529  | 3.60k  |                 read_aligned(&mut self.reader, bits / 8, &mut acc)?;  |  530  | 3.60k  |                 read_unaligned(&mut self.reader, bits % 8, &mut acc, &mut self.bitqueue)?;  |  531  | 3.60k  |                 Ok(acc.value())  |  532  |  |             }  |  533  |  |         } else { |  534  | 0  |             Err(io::Error::new(  |  535  | 0  |                 io::ErrorKind::InvalidInput,  |  536  | 0  |                 "excessive bits for type read",  |  537  | 0  |             ))  |  538  |  |         }  |  539  | 7.19k  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read::<u16> Line  | Count  | Source  |  516  | 7.18k  |     fn read<U>(&mut self, mut bits: u32) -> io::Result<U>  |  517  | 7.18k  |     where  |  518  | 7.18k  |         U: Numeric,  |  519  |  |     { |  520  | 7.18k  |         if bits <= U::BITS_SIZE { |  521  | 7.18k  |             let bitqueue_len = self.bitqueue.len();  |  522  | 7.18k  |             if bits <= bitqueue_len { |  523  | 0  |                 Ok(U::from_u8(self.bitqueue.pop(bits)))  |  524  |  |             } else { |  525  | 7.18k  |                 let mut acc =  |  526  | 7.18k  |                     BitQueue::from_value(U::from_u8(self.bitqueue.pop_all()), bitqueue_len);  |  527  | 7.18k  |                 bits -= bitqueue_len;  |  528  |  |  |  529  | 7.18k  |                 read_aligned(&mut self.reader, bits / 8, &mut acc)?;  |  530  | 7.18k  |                 read_unaligned(&mut self.reader, bits % 8, &mut acc, &mut self.bitqueue)?;  |  531  | 7.18k  |                 Ok(acc.value())  |  532  |  |             }  |  533  |  |         } else { |  534  | 0  |             Err(io::Error::new(  |  535  | 0  |                 io::ErrorKind::InvalidInput,  |  536  | 0  |                 "excessive bits for type read",  |  537  | 0  |             ))  |  538  |  |         }  |  539  | 7.18k  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read::<u8> Line  | Count  | Source  |  516  | 1.27M  |     fn read<U>(&mut self, mut bits: u32) -> io::Result<U>  |  517  | 1.27M  |     where  |  518  | 1.27M  |         U: Numeric,  |  519  |  |     { |  520  | 1.27M  |         if bits <= U::BITS_SIZE { |  521  | 1.27M  |             let bitqueue_len = self.bitqueue.len();  |  522  | 1.27M  |             if bits <= bitqueue_len { |  523  | 580k  |                 Ok(U::from_u8(self.bitqueue.pop(bits)))  |  524  |  |             } else { |  525  | 691k  |                 let mut acc =  |  526  | 691k  |                     BitQueue::from_value(U::from_u8(self.bitqueue.pop_all()), bitqueue_len);  |  527  | 691k  |                 bits -= bitqueue_len;  |  528  |  |  |  529  | 691k  |                 read_aligned(&mut self.reader, bits / 8, &mut acc)?;  |  530  | 691k  |                 read_unaligned(&mut self.reader, bits % 8, &mut acc, &mut self.bitqueue)?;  |  531  | 690k  |                 Ok(acc.value())  |  532  |  |             }  |  533  |  |         } else { |  534  | 0  |             Err(io::Error::new(  |  535  | 0  |                 io::ErrorKind::InvalidInput,  |  536  | 0  |                 "excessive bits for type read",  |  537  | 0  |             ))  |  538  |  |         }  |  539  | 1.27M  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read::<u32> Line  | Count  | Source  |  516  | 2.97M  |     fn read<U>(&mut self, mut bits: u32) -> io::Result<U>  |  517  | 2.97M  |     where  |  518  | 2.97M  |         U: Numeric,  |  519  |  |     { |  520  | 2.97M  |         if bits <= U::BITS_SIZE { |  521  | 2.97M  |             let bitqueue_len = self.bitqueue.len();  |  522  | 2.97M  |             if bits <= bitqueue_len { |  523  | 1.77M  |                 Ok(U::from_u8(self.bitqueue.pop(bits)))  |  524  |  |             } else { |  525  | 1.20M  |                 let mut acc =  |  526  | 1.20M  |                     BitQueue::from_value(U::from_u8(self.bitqueue.pop_all()), bitqueue_len);  |  527  | 1.20M  |                 bits -= bitqueue_len;  |  528  |  |  |  529  | 1.20M  |                 read_aligned(&mut self.reader, bits / 8, &mut acc)?;  |  530  | 1.20M  |                 read_unaligned(&mut self.reader, bits % 8, &mut acc, &mut self.bitqueue)?;  |  531  | 1.20M  |                 Ok(acc.value())  |  532  |  |             }  |  533  |  |         } else { |  534  | 0  |             Err(io::Error::new(  |  535  | 0  |                 io::ErrorKind::InvalidInput,  |  536  | 0  |                 "excessive bits for type read",  |  537  | 0  |             ))  |  538  |  |         }  |  539  | 2.97M  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::read::<u16> Line  | Count  | Source  |  516  | 203k  |     fn read<U>(&mut self, mut bits: u32) -> io::Result<U>  |  517  | 203k  |     where  |  518  | 203k  |         U: Numeric,  |  519  |  |     { |  520  | 203k  |         if bits <= U::BITS_SIZE { |  521  | 203k  |             let bitqueue_len = self.bitqueue.len();  |  522  | 203k  |             if bits <= bitqueue_len { |  523  | 3.30k  |                 Ok(U::from_u8(self.bitqueue.pop(bits)))  |  524  |  |             } else { |  525  | 200k  |                 let mut acc =  |  526  | 200k  |                     BitQueue::from_value(U::from_u8(self.bitqueue.pop_all()), bitqueue_len);  |  527  | 200k  |                 bits -= bitqueue_len;  |  528  |  |  |  529  | 200k  |                 read_aligned(&mut self.reader, bits / 8, &mut acc)?;  |  530  | 200k  |                 read_unaligned(&mut self.reader, bits % 8, &mut acc, &mut self.bitqueue)?;  |  531  | 199k  |                 Ok(acc.value())  |  532  |  |             }  |  533  |  |         } else { |  534  | 0  |             Err(io::Error::new(  |  535  | 0  |                 io::ErrorKind::InvalidInput,  |  536  | 0  |                 "excessive bits for type read",  |  537  | 0  |             ))  |  538  |  |         }  |  539  | 203k  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _> as bitstream_io::read::BitRead>::read::<_>  | 
540  |  |  | 
541  |  |     /// # Examples  | 
542  |  |     /// ```  | 
543  |  |     /// use std::io::{Read, Cursor}; | 
544  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
545  |  |     /// let data = [0b10110111];  | 
546  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
547  |  |     /// assert_eq!(reader.read_signed::<i8>(4).unwrap(), -5);  | 
548  |  |     /// assert_eq!(reader.read_signed::<i8>(4).unwrap(), 7);  | 
549  |  |     /// ```  | 
550  |  |     ///  | 
551  |  |     /// ```  | 
552  |  |     /// use std::io::{Read, Cursor}; | 
553  |  |     /// use bitstream_io::{LittleEndian, BitReader, BitRead}; | 
554  |  |     /// let data = [0b10110111];  | 
555  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), LittleEndian);  | 
556  |  |     /// assert_eq!(reader.read_signed::<i8>(4).unwrap(), 7);  | 
557  |  |     /// assert_eq!(reader.read_signed::<i8>(4).unwrap(), -5);  | 
558  |  |     /// ```  | 
559  |  |     ///  | 
560  |  |     /// ```  | 
561  |  |     /// use std::io::{Read, Cursor}; | 
562  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
563  |  |     /// let data = [0;10];  | 
564  |  |     /// let mut r = BitReader::endian(Cursor::new(&data), BigEndian);  | 
565  |  |     /// assert!(r.read_signed::<i8>(9).is_err());   // can't read 9 bits to i8  | 
566  |  |     /// assert!(r.read_signed::<i16>(17).is_err()); // can't read 17 bits to i16  | 
567  |  |     /// assert!(r.read_signed::<i32>(33).is_err()); // can't read 33 bits to i32  | 
568  |  |     /// assert!(r.read_signed::<i64>(65).is_err()); // can't read 65 bits to i64  | 
569  |  |     /// ```  | 
570  |  |     #[inline]  | 
571  | 0  |     fn read_signed<S>(&mut self, bits: u32) -> io::Result<S>  | 
572  | 0  |     where  | 
573  | 0  |         S: SignedNumeric,  | 
574  |  |     { | 
575  | 0  |         E::read_signed(self, bits)  | 
576  | 0  |     }  | 
577  |  |  | 
578  |  |     #[inline]  | 
579  | 0  |     fn read_to<V>(&mut self) -> io::Result<V>  | 
580  | 0  |     where  | 
581  | 0  |         V: Primitive,  | 
582  |  |     { | 
583  | 0  |         E::read_primitive(self)  | 
584  | 0  |     }  | 
585  |  |  | 
586  |  |     /// # Examples  | 
587  |  |     /// ```  | 
588  |  |     /// use std::io::{Read, Cursor}; | 
589  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
590  |  |     /// let data = [0b10110111];  | 
591  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
592  |  |     /// assert!(reader.skip(3).is_ok());  | 
593  |  |     /// assert_eq!(reader.read::<u8>(5).unwrap(), 0b10111);  | 
594  |  |     /// ```  | 
595  |  |     ///  | 
596  |  |     /// ```  | 
597  |  |     /// use std::io::{Read, Cursor}; | 
598  |  |     /// use bitstream_io::{LittleEndian, BitReader, BitRead}; | 
599  |  |     /// let data = [0b10110111];  | 
600  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), LittleEndian);  | 
601  |  |     /// assert!(reader.skip(3).is_ok());  | 
602  |  |     /// assert_eq!(reader.read::<u8>(5).unwrap(), 0b10110);  | 
603  |  |     /// ```  | 
604  | 13.6k  |     fn skip(&mut self, mut bits: u32) -> io::Result<()> { | 
605  |  |         use std::cmp::min;  | 
606  |  |  | 
607  | 13.6k  |         let to_drop = min(self.bitqueue.len(), bits);  | 
608  | 13.6k  |         if to_drop != 0 { | 
609  | 0  |             self.bitqueue.drop(to_drop);  | 
610  | 0  |             bits -= to_drop;  | 
611  | 13.6k  |         }  | 
612  |  |  | 
613  | 13.6k  |         skip_aligned(&mut self.reader, bits / 8)?;  | 
614  | 13.6k  |         skip_unaligned(&mut self.reader, bits % 8, &mut self.bitqueue)  | 
615  | 13.6k  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::BitRead>::skip Line  | Count  | Source  |  604  | 13.6k  |     fn skip(&mut self, mut bits: u32) -> io::Result<()> { |  605  |  |         use std::cmp::min;  |  606  |  |  |  607  | 13.6k  |         let to_drop = min(self.bitqueue.len(), bits);  |  608  | 13.6k  |         if to_drop != 0 { |  609  | 0  |             self.bitqueue.drop(to_drop);  |  610  | 0  |             bits -= to_drop;  |  611  | 13.6k  |         }  |  612  |  |  |  613  | 13.6k  |         skip_aligned(&mut self.reader, bits / 8)?;  |  614  | 13.6k  |         skip_unaligned(&mut self.reader, bits % 8, &mut self.bitqueue)  |  615  | 13.6k  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _> as bitstream_io::read::BitRead>::skip  | 
616  |  |  | 
617  |  |     /// # Example  | 
618  |  |     /// ```  | 
619  |  |     /// use std::io::{Read, Cursor}; | 
620  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
621  |  |     /// let data = b"foobar";  | 
622  |  |     /// let mut reader = BitReader::endian(Cursor::new(data), BigEndian);  | 
623  |  |     /// assert!(reader.skip(24).is_ok());  | 
624  |  |     /// let mut buf = [0;3];  | 
625  |  |     /// assert!(reader.read_bytes(&mut buf).is_ok());  | 
626  |  |     /// assert_eq!(&buf, b"bar");  | 
627  |  |     /// ```  | 
628  | 0  |     fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> { | 
629  | 0  |         if self.byte_aligned() { | 
630  | 0  |             self.reader.read_exact(buf)  | 
631  |  |         } else { | 
632  | 0  |             for b in buf.iter_mut() { | 
633  | 0  |                 *b = self.read(8)?;  | 
634  |  |             }  | 
635  | 0  |             Ok(())  | 
636  |  |         }  | 
637  | 0  |     }  | 
638  |  |  | 
639  |  |     /// # Examples  | 
640  |  |     /// ```  | 
641  |  |     /// use std::io::{Read, Cursor}; | 
642  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
643  |  |     /// let data = [0b01110111, 0b11111110];  | 
644  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
645  |  |     /// assert_eq!(reader.read_unary0().unwrap(), 0);  | 
646  |  |     /// assert_eq!(reader.read_unary0().unwrap(), 3);  | 
647  |  |     /// assert_eq!(reader.read_unary0().unwrap(), 10);  | 
648  |  |     /// ```  | 
649  |  |     ///  | 
650  |  |     /// ```  | 
651  |  |     /// use std::io::{Read, Cursor}; | 
652  |  |     /// use bitstream_io::{LittleEndian, BitReader, BitRead}; | 
653  |  |     /// let data = [0b11101110, 0b01111111];  | 
654  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), LittleEndian);  | 
655  |  |     /// assert_eq!(reader.read_unary0().unwrap(), 0);  | 
656  |  |     /// assert_eq!(reader.read_unary0().unwrap(), 3);  | 
657  |  |     /// assert_eq!(reader.read_unary0().unwrap(), 10);  | 
658  |  |     /// ```  | 
659  | 0  |     fn read_unary0(&mut self) -> io::Result<u32> { | 
660  | 0  |         if self.bitqueue.is_empty() { | 
661  | 0  |             read_aligned_unary(&mut self.reader, 0b1111_1111, &mut self.bitqueue)  | 
662  | 0  |                 .map(|u| u + self.bitqueue.pop_1())  | 
663  | 0  |         } else if self.bitqueue.all_1() { | 
664  | 0  |             let base = self.bitqueue.len();  | 
665  | 0  |             self.bitqueue.clear();  | 
666  | 0  |             read_aligned_unary(&mut self.reader, 0b1111_1111, &mut self.bitqueue)  | 
667  | 0  |                 .map(|u| base + u + self.bitqueue.pop_1())  | 
668  |  |         } else { | 
669  | 0  |             Ok(self.bitqueue.pop_1())  | 
670  |  |         }  | 
671  | 0  |     }  | 
672  |  |  | 
673  |  |     /// # Examples  | 
674  |  |     /// ```  | 
675  |  |     /// use std::io::{Read, Cursor}; | 
676  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
677  |  |     /// let data = [0b10001000, 0b00000001];  | 
678  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
679  |  |     /// assert_eq!(reader.read_unary1().unwrap(), 0);  | 
680  |  |     /// assert_eq!(reader.read_unary1().unwrap(), 3);  | 
681  |  |     /// assert_eq!(reader.read_unary1().unwrap(), 10);  | 
682  |  |     /// ```  | 
683  |  |     ///  | 
684  |  |     /// ```  | 
685  |  |     /// use std::io::{Read, Cursor}; | 
686  |  |     /// use bitstream_io::{LittleEndian, BitReader, BitRead}; | 
687  |  |     /// let data = [0b00010001, 0b10000000];  | 
688  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), LittleEndian);  | 
689  |  |     /// assert_eq!(reader.read_unary1().unwrap(), 0);  | 
690  |  |     /// assert_eq!(reader.read_unary1().unwrap(), 3);  | 
691  |  |     /// assert_eq!(reader.read_unary1().unwrap(), 10);  | 
692  |  |     /// ```  | 
693  | 0  |     fn read_unary1(&mut self) -> io::Result<u32> { | 
694  | 0  |         if self.bitqueue.is_empty() { | 
695  | 0  |             read_aligned_unary(&mut self.reader, 0b0000_0000, &mut self.bitqueue)  | 
696  | 0  |                 .map(|u| u + self.bitqueue.pop_0())  | 
697  | 0  |         } else if self.bitqueue.all_0() { | 
698  | 0  |             let base = self.bitqueue.len();  | 
699  | 0  |             self.bitqueue.clear();  | 
700  | 0  |             read_aligned_unary(&mut self.reader, 0b0000_0000, &mut self.bitqueue)  | 
701  | 0  |                 .map(|u| base + u + self.bitqueue.pop_0())  | 
702  |  |         } else { | 
703  | 0  |             Ok(self.bitqueue.pop_0())  | 
704  |  |         }  | 
705  | 0  |     }  | 
706  |  |  | 
707  |  |     /// # Example  | 
708  |  |     /// ```  | 
709  |  |     /// use std::io::{Read, Cursor}; | 
710  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
711  |  |     /// let data = [0];  | 
712  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
713  |  |     /// assert_eq!(reader.byte_aligned(), true);  | 
714  |  |     /// assert!(reader.skip(1).is_ok());  | 
715  |  |     /// assert_eq!(reader.byte_aligned(), false);  | 
716  |  |     /// assert!(reader.skip(7).is_ok());  | 
717  |  |     /// assert_eq!(reader.byte_aligned(), true);  | 
718  |  |     /// ```  | 
719  |  |     #[inline]  | 
720  | 0  |     fn byte_aligned(&self) -> bool { | 
721  | 0  |         self.bitqueue.is_empty()  | 
722  | 0  |     }  | 
723  |  |  | 
724  |  |     /// # Example  | 
725  |  |     /// ```  | 
726  |  |     /// use std::io::{Read, Cursor}; | 
727  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
728  |  |     /// let data = [0x00, 0xFF];  | 
729  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
730  |  |     /// assert_eq!(reader.read::<u8>(4).unwrap(), 0);  | 
731  |  |     /// reader.byte_align();  | 
732  |  |     /// assert_eq!(reader.read::<u8>(8).unwrap(), 0xFF);  | 
733  |  |     /// ```  | 
734  |  |     #[inline]  | 
735  | 0  |     fn byte_align(&mut self) { | 
736  | 0  |         self.bitqueue.clear()  | 
737  | 0  |     }  | 
738  |  | }  | 
739  |  |  | 
740  |  | impl<R, E> BitReader<R, E>  | 
741  |  | where  | 
742  |  |     E: Endianness,  | 
743  |  |     R: io::Read + io::Seek,  | 
744  |  | { | 
745  |  |     /// # Example  | 
746  |  |     /// ```  | 
747  |  |     /// use std::io::{Read, Cursor, SeekFrom}; | 
748  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
749  |  |     /// let data = [0x00, 0xFF];  | 
750  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
751  |  |     /// assert_eq!(reader.position_in_bits().unwrap(), 0);  | 
752  |  |     ///  | 
753  |  |     /// let pos = reader.seek_bits(SeekFrom::Start(5)).unwrap();  | 
754  |  |     /// assert!(pos == 5 && 5 == reader.position_in_bits().unwrap());  | 
755  |  |     ///  | 
756  |  |     /// let pos = reader.seek_bits(SeekFrom::Current(-2)).unwrap();  | 
757  |  |     /// assert!(pos == 3 && 3 == reader.position_in_bits().unwrap());    ///  | 
758  |  |     ///  | 
759  |  |     /// let pos = reader.seek_bits(SeekFrom::End(5)).unwrap();  | 
760  |  |     /// assert!(pos == 11 && 11 == reader.position_in_bits().unwrap());  | 
761  |  |     /// ```  | 
762  | 0  |     pub fn seek_bits(&mut self, from: io::SeekFrom) -> io::Result<u64> { | 
763  | 0  |         match from { | 
764  | 0  |             io::SeekFrom::Start(from_start_pos) => { | 
765  | 0  |                 let (bytes, bits) = (from_start_pos / 8, (from_start_pos % 8) as u32);  | 
766  | 0  |                 self.byte_align();  | 
767  | 0  |                 self.reader.seek(io::SeekFrom::Start(bytes))?;  | 
768  | 0  |                 self.skip(bits)?;  | 
769  | 0  |                 Ok(from_start_pos)  | 
770  |  |             }  | 
771  | 0  |             io::SeekFrom::End(from_end_pos) => { | 
772  | 0  |                 let reader_end = self.reader.seek(io::SeekFrom::End(0))?;  | 
773  | 0  |                 let new_pos = (reader_end * 8) as i64 - from_end_pos;  | 
774  | 0  |                 assert!(new_pos >= 0, "The final position should be greater than 0");  | 
775  | 0  |                 self.seek_bits(io::SeekFrom::Start(new_pos as u64))  | 
776  |  |             }  | 
777  | 0  |             io::SeekFrom::Current(offset) => { | 
778  | 0  |                 let new_pos = self.position_in_bits()? as i64 + offset;  | 
779  | 0  |                 assert!(new_pos >= 0, "The final position should be greater than 0");  | 
780  | 0  |                 self.seek_bits(io::SeekFrom::Start(new_pos as u64))  | 
781  |  |             }  | 
782  |  |         }  | 
783  | 0  |     }  | 
784  |  |  | 
785  |  |     /// # Example  | 
786  |  |     /// ```  | 
787  |  |     /// use std::fs::read;  | 
788  |  |     /// use std::io::{Read, Cursor, SeekFrom}; | 
789  |  |     /// use bitstream_io::{BigEndian, BitReader, BitRead}; | 
790  |  |     /// let data = [0x00, 0xFF];  | 
791  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
792  |  |     /// assert_eq!(reader.position_in_bits().unwrap(), 0);  | 
793  |  |     ///  | 
794  |  |     /// let _: i32 = reader.read_signed(5).unwrap();  | 
795  |  |     /// assert_eq!(reader.position_in_bits().unwrap(), 5);  | 
796  |  |     ///  | 
797  |  |     /// reader.read_bit().unwrap();  | 
798  |  |     /// assert_eq!(reader.position_in_bits().unwrap(), 6);  | 
799  |  |     /// ```  | 
800  |  |     #[inline]  | 
801  | 54.7M  |     pub fn position_in_bits(&mut self) -> io::Result<u64> { | 
802  | 54.7M  |         let bytes = self.reader.stream_position()?;  | 
803  | 54.7M  |         Ok(bytes * 8 - (self.bitqueue.len() as u64))  | 
804  | 54.7M  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian>>::position_in_bits Line  | Count  | Source  |  801  | 54.7M  |     pub fn position_in_bits(&mut self) -> io::Result<u64> { |  802  | 54.7M  |         let bytes = self.reader.stream_position()?;  |  803  | 54.7M  |         Ok(bytes * 8 - (self.bitqueue.len() as u64))  |  804  | 54.7M  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _>>::position_in_bits  | 
805  |  | }  | 
806  |  |  | 
807  |  | impl<R: io::Read, E: Endianness> HuffmanRead<E> for BitReader<R, E> { | 
808  |  |     /// # Example  | 
809  |  |     /// ```  | 
810  |  |     /// use std::io::{Read, Cursor}; | 
811  |  |     /// use bitstream_io::{BigEndian, BitReader, HuffmanRead}; | 
812  |  |     /// use bitstream_io::huffman::compile_read_tree;  | 
813  |  |     /// let tree = compile_read_tree(  | 
814  |  |     ///     vec![('a', vec![0]), | 
815  |  |     ///          ('b', vec![1, 0]), | 
816  |  |     ///          ('c', vec![1, 1, 0]), | 
817  |  |     ///          ('d', vec![1, 1, 1])]).unwrap(); | 
818  |  |     /// let data = [0b10110111];  | 
819  |  |     /// let mut reader = BitReader::endian(Cursor::new(&data), BigEndian);  | 
820  |  |     /// assert_eq!(reader.read_huffman(&tree).unwrap(), 'b');  | 
821  |  |     /// assert_eq!(reader.read_huffman(&tree).unwrap(), 'c');  | 
822  |  |     /// assert_eq!(reader.read_huffman(&tree).unwrap(), 'd');  | 
823  |  |     /// ```  | 
824  | 178M  |     fn read_huffman<T>(&mut self, tree: &[ReadHuffmanTree<E, T>]) -> io::Result<T>  | 
825  | 178M  |     where  | 
826  | 178M  |         T: Clone,  | 
827  |  |     { | 
828  | 178M  |         let mut result: &ReadHuffmanTree<E, T> = &tree[self.bitqueue.to_state()];  | 
829  |  |         loop { | 
830  | 194M  |             match result { | 
831  | 178M  |                 ReadHuffmanTree::Done(ref value, ref queue_val, ref queue_bits, _) => { | 
832  | 178M  |                     self.bitqueue.set(*queue_val, *queue_bits);  | 
833  | 178M  |                     return Ok(value.clone());  | 
834  |  |                 }  | 
835  | 15.9M  |                 ReadHuffmanTree::Continue(ref tree) => { | 
836  | 15.9M  |                     result = &tree[read_byte(&mut self.reader)? as usize];  | 
837  |  |                 }  | 
838  |  |                 ReadHuffmanTree::InvalidState => { | 
839  | 0  |                     panic!("invalid state"); | 
840  |  |                 }  | 
841  |  |             }  | 
842  |  |         }  | 
843  | 178M  |     } <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::HuffmanRead<bitstream_io::LittleEndian>>::read_huffman::<u8> Line  | Count  | Source  |  824  | 130M  |     fn read_huffman<T>(&mut self, tree: &[ReadHuffmanTree<E, T>]) -> io::Result<T>  |  825  | 130M  |     where  |  826  | 130M  |         T: Clone,  |  827  |  |     { |  828  | 130M  |         let mut result: &ReadHuffmanTree<E, T> = &tree[self.bitqueue.to_state()];  |  829  |  |         loop { |  830  | 133M  |             match result { |  831  | 130M  |                 ReadHuffmanTree::Done(ref value, ref queue_val, ref queue_bits, _) => { |  832  | 130M  |                     self.bitqueue.set(*queue_val, *queue_bits);  |  833  | 130M  |                     return Ok(value.clone());  |  834  |  |                 }  |  835  | 3.21M  |                 ReadHuffmanTree::Continue(ref tree) => { |  836  | 3.21M  |                     result = &tree[read_byte(&mut self.reader)? as usize];  |  837  |  |                 }  |  838  |  |                 ReadHuffmanTree::InvalidState => { |  839  | 0  |                     panic!("invalid state"); |  840  |  |                 }  |  841  |  |             }  |  842  |  |         }  |  843  | 130M  |     }  |  
 <bitstream_io::read::BitReader<std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> as bitstream_io::read::HuffmanRead<bitstream_io::LittleEndian>>::read_huffman::<u16> Line  | Count  | Source  |  824  | 48.5M  |     fn read_huffman<T>(&mut self, tree: &[ReadHuffmanTree<E, T>]) -> io::Result<T>  |  825  | 48.5M  |     where  |  826  | 48.5M  |         T: Clone,  |  827  |  |     { |  828  | 48.5M  |         let mut result: &ReadHuffmanTree<E, T> = &tree[self.bitqueue.to_state()];  |  829  |  |         loop { |  830  | 61.3M  |             match result { |  831  | 48.5M  |                 ReadHuffmanTree::Done(ref value, ref queue_val, ref queue_bits, _) => { |  832  | 48.5M  |                     self.bitqueue.set(*queue_val, *queue_bits);  |  833  | 48.5M  |                     return Ok(value.clone());  |  834  |  |                 }  |  835  | 12.7M  |                 ReadHuffmanTree::Continue(ref tree) => { |  836  | 12.7M  |                     result = &tree[read_byte(&mut self.reader)? as usize];  |  837  |  |                 }  |  838  |  |                 ReadHuffmanTree::InvalidState => { |  839  | 0  |                     panic!("invalid state"); |  840  |  |                 }  |  841  |  |             }  |  842  |  |         }  |  843  | 48.5M  |     }  |  
 Unexecuted instantiation: <bitstream_io::read::BitReader<_, _> as bitstream_io::read::HuffmanRead<_>>::read_huffman::<_>  | 
844  |  | }  | 
845  |  |  | 
846  |  | #[inline]  | 
847  | 18.1M  | fn read_byte<R>(mut reader: R) -> io::Result<u8>  | 
848  | 18.1M  | where  | 
849  | 18.1M  |     R: io::Read,  | 
850  |  | { | 
851  | 18.1M  |     let mut byte = 0;  | 
852  | 18.1M  |     reader  | 
853  | 18.1M  |         .read_exact(std::slice::from_mut(&mut byte))  | 
854  | 18.1M  |         .map(|()| byte)  | 
855  | 18.1M  | } bitstream_io::read::read_byte::<&mut std::io::cursor::Cursor<[u8; 5]>> Line  | Count  | Source  |  847  | 7.18k  | fn read_byte<R>(mut reader: R) -> io::Result<u8>  |  848  | 7.18k  | where  |  849  | 7.18k  |     R: io::Read,  |  850  |  | { |  851  | 7.18k  |     let mut byte = 0;  |  852  | 7.18k  |     reader  |  853  | 7.18k  |         .read_exact(std::slice::from_mut(&mut byte))  |  854  | 7.18k  |         .map(|()| byte)  |  855  | 7.18k  | }  |  
 bitstream_io::read::read_byte::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>> Line  | Count  | Source  |  847  | 18.1M  | fn read_byte<R>(mut reader: R) -> io::Result<u8>  |  848  | 18.1M  | where  |  849  | 18.1M  |     R: io::Read,  |  850  |  | { |  851  | 18.1M  |     let mut byte = 0;  |  852  | 18.1M  |     reader  |  853  | 18.1M  |         .read_exact(std::slice::from_mut(&mut byte))  |  854  | 18.1M  |         .map(|()| byte)  |  855  | 18.1M  | }  |  
 Unexecuted instantiation: bitstream_io::read::read_byte::<_>  | 
856  |  |  | 
857  | 2.10M  | fn read_aligned<R, E, N>(mut reader: R, bytes: u32, acc: &mut BitQueue<E, N>) -> io::Result<()>  | 
858  | 2.10M  | where  | 
859  | 2.10M  |     R: io::Read,  | 
860  | 2.10M  |     E: Endianness,  | 
861  | 2.10M  |     N: Numeric,  | 
862  |  | { | 
863  | 2.10M  |     if bytes > 0 { | 
864  | 758k  |         let mut buf = N::buffer();  | 
865  | 758k  |         reader.read_exact(&mut buf.as_mut()[0..bytes as usize])?;  | 
866  | 761k  |         for b in &buf.as_ref()[0..bytes as usize] { | 
867  | 761k  |             acc.push(8, N::from_u8(*b));  | 
868  | 761k  |         }  | 
869  | 1.35M  |     }  | 
870  | 2.10M  |     Ok(())  | 
871  | 2.10M  | } bitstream_io::read::read_aligned::<&mut std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian, u8> Line  | Count  | Source  |  857  | 3.60k  | fn read_aligned<R, E, N>(mut reader: R, bytes: u32, acc: &mut BitQueue<E, N>) -> io::Result<()>  |  858  | 3.60k  | where  |  859  | 3.60k  |     R: io::Read,  |  860  | 3.60k  |     E: Endianness,  |  861  | 3.60k  |     N: Numeric,  |  862  |  | { |  863  | 3.60k  |     if bytes > 0 { |  864  | 3.60k  |         let mut buf = N::buffer();  |  865  | 3.60k  |         reader.read_exact(&mut buf.as_mut()[0..bytes as usize])?;  |  866  | 3.60k  |         for b in &buf.as_ref()[0..bytes as usize] { |  867  | 3.60k  |             acc.push(8, N::from_u8(*b));  |  868  | 3.60k  |         }  |  869  | 0  |     }  |  870  | 3.60k  |     Ok(())  |  871  | 3.60k  | }  |  
 bitstream_io::read::read_aligned::<&mut std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian, u16> Line  | Count  | Source  |  857  | 7.18k  | fn read_aligned<R, E, N>(mut reader: R, bytes: u32, acc: &mut BitQueue<E, N>) -> io::Result<()>  |  858  | 7.18k  | where  |  859  | 7.18k  |     R: io::Read,  |  860  | 7.18k  |     E: Endianness,  |  861  | 7.18k  |     N: Numeric,  |  862  |  | { |  863  | 7.18k  |     if bytes > 0 { |  864  | 7.18k  |         let mut buf = N::buffer();  |  865  | 7.18k  |         reader.read_exact(&mut buf.as_mut()[0..bytes as usize])?;  |  866  | 7.18k  |         for b in &buf.as_ref()[0..bytes as usize] { |  867  | 7.18k  |             acc.push(8, N::from_u8(*b));  |  868  | 7.18k  |         }  |  869  | 0  |     }  |  870  | 7.18k  |     Ok(())  |  871  | 7.18k  | }  |  
 bitstream_io::read::read_aligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian, u8> Line  | Count  | Source  |  857  | 691k  | fn read_aligned<R, E, N>(mut reader: R, bytes: u32, acc: &mut BitQueue<E, N>) -> io::Result<()>  |  858  | 691k  | where  |  859  | 691k  |     R: io::Read,  |  860  | 691k  |     E: Endianness,  |  861  | 691k  |     N: Numeric,  |  862  |  | { |  863  | 691k  |     if bytes > 0 { |  864  | 258k  |         let mut buf = N::buffer();  |  865  | 258k  |         reader.read_exact(&mut buf.as_mut()[0..bytes as usize])?;  |  866  | 258k  |         for b in &buf.as_ref()[0..bytes as usize] { |  867  | 258k  |             acc.push(8, N::from_u8(*b));  |  868  | 258k  |         }  |  869  | 433k  |     }  |  870  | 691k  |     Ok(())  |  871  | 691k  | }  |  
 bitstream_io::read::read_aligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian, u32> Line  | Count  | Source  |  857  | 1.20M  | fn read_aligned<R, E, N>(mut reader: R, bytes: u32, acc: &mut BitQueue<E, N>) -> io::Result<()>  |  858  | 1.20M  | where  |  859  | 1.20M  |     R: io::Read,  |  860  | 1.20M  |     E: Endianness,  |  861  | 1.20M  |     N: Numeric,  |  862  |  | { |  863  | 1.20M  |     if bytes > 0 { |  864  | 424k  |         let mut buf = N::buffer();  |  865  | 424k  |         reader.read_exact(&mut buf.as_mut()[0..bytes as usize])?;  |  866  | 427k  |         for b in &buf.as_ref()[0..bytes as usize] { |  867  | 427k  |             acc.push(8, N::from_u8(*b));  |  868  | 427k  |         }  |  869  | 781k  |     }  |  870  | 1.20M  |     Ok(())  |  871  | 1.20M  | }  |  
 bitstream_io::read::read_aligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian, u16> Line  | Count  | Source  |  857  | 200k  | fn read_aligned<R, E, N>(mut reader: R, bytes: u32, acc: &mut BitQueue<E, N>) -> io::Result<()>  |  858  | 200k  | where  |  859  | 200k  |     R: io::Read,  |  860  | 200k  |     E: Endianness,  |  861  | 200k  |     N: Numeric,  |  862  |  | { |  863  | 200k  |     if bytes > 0 { |  864  | 65.0k  |         let mut buf = N::buffer();  |  865  | 65.0k  |         reader.read_exact(&mut buf.as_mut()[0..bytes as usize])?;  |  866  | 65.2k  |         for b in &buf.as_ref()[0..bytes as usize] { |  867  | 65.2k  |             acc.push(8, N::from_u8(*b));  |  868  | 65.2k  |         }  |  869  | 135k  |     }  |  870  | 200k  |     Ok(())  |  871  | 200k  | }  |  
 Unexecuted instantiation: bitstream_io::read::read_aligned::<_, _, _>  | 
872  |  |  | 
873  | 13.6k  | fn skip_aligned<R>(mut reader: R, mut bytes: u32) -> io::Result<()>  | 
874  | 13.6k  | where  | 
875  | 13.6k  |     R: io::Read,  | 
876  |  | { | 
877  |  |     use std::cmp::min;  | 
878  |  |  | 
879  |  |     /*skip up to 8 bytes at a time  | 
880  |  |     (unlike with read_aligned, "bytes" may be larger than any native type)*/  | 
881  | 13.6k  |     let mut buf = [0; 8];  | 
882  | 13.6k  |     while bytes > 0 { | 
883  | 0  |         let to_read = min(8, bytes);  | 
884  | 0  |         reader.read_exact(&mut buf[0..to_read as usize])?;  | 
885  | 0  |         bytes -= to_read;  | 
886  |  |     }  | 
887  | 13.6k  |     Ok(())  | 
888  | 13.6k  | } bitstream_io::read::skip_aligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>> Line  | Count  | Source  |  873  | 13.6k  | fn skip_aligned<R>(mut reader: R, mut bytes: u32) -> io::Result<()>  |  874  | 13.6k  | where  |  875  | 13.6k  |     R: io::Read,  |  876  |  | { |  877  |  |     use std::cmp::min;  |  878  |  |  |  879  |  |     /*skip up to 8 bytes at a time  |  880  |  |     (unlike with read_aligned, "bytes" may be larger than any native type)*/  |  881  | 13.6k  |     let mut buf = [0; 8];  |  882  | 13.6k  |     while bytes > 0 { |  883  | 0  |         let to_read = min(8, bytes);  |  884  | 0  |         reader.read_exact(&mut buf[0..to_read as usize])?;  |  885  | 0  |         bytes -= to_read;  |  886  |  |     }  |  887  | 13.6k  |     Ok(())  |  888  | 13.6k  | }  |  
 Unexecuted instantiation: bitstream_io::read::skip_aligned::<_>  | 
889  |  |  | 
890  |  | #[inline]  | 
891  | 2.10M  | fn read_unaligned<R, E, N>(  | 
892  | 2.10M  |     reader: R,  | 
893  | 2.10M  |     bits: u32,  | 
894  | 2.10M  |     acc: &mut BitQueue<E, N>,  | 
895  | 2.10M  |     rem: &mut BitQueue<E, u8>,  | 
896  | 2.10M  | ) -> io::Result<()>  | 
897  | 2.10M  | where  | 
898  | 2.10M  |     R: io::Read,  | 
899  | 2.10M  |     E: Endianness,  | 
900  | 2.10M  |     N: Numeric,  | 
901  |  | { | 
902  | 2.10M  |     debug_assert!(bits <= 8);  | 
903  |  |  | 
904  | 2.10M  |     if bits > 0 { | 
905  | 1.65M  |         rem.set(read_byte(reader)?, 8);  | 
906  | 1.65M  |         acc.push(bits, N::from_u8(rem.pop(bits)));  | 
907  | 453k  |     }  | 
908  | 2.10M  |     Ok(())  | 
909  | 2.10M  | } bitstream_io::read::read_unaligned::<&mut std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian, u8> Line  | Count  | Source  |  891  | 3.60k  | fn read_unaligned<R, E, N>(  |  892  | 3.60k  |     reader: R,  |  893  | 3.60k  |     bits: u32,  |  894  | 3.60k  |     acc: &mut BitQueue<E, N>,  |  895  | 3.60k  |     rem: &mut BitQueue<E, u8>,  |  896  | 3.60k  | ) -> io::Result<()>  |  897  | 3.60k  | where  |  898  | 3.60k  |     R: io::Read,  |  899  | 3.60k  |     E: Endianness,  |  900  | 3.60k  |     N: Numeric,  |  901  |  | { |  902  | 3.60k  |     debug_assert!(bits <= 8);  |  903  |  |  |  904  | 3.60k  |     if bits > 0 { |  905  | 0  |         rem.set(read_byte(reader)?, 8);  |  906  | 0  |         acc.push(bits, N::from_u8(rem.pop(bits)));  |  907  | 3.60k  |     }  |  908  | 3.60k  |     Ok(())  |  909  | 3.60k  | }  |  
 bitstream_io::read::read_unaligned::<&mut std::io::cursor::Cursor<[u8; 5]>, bitstream_io::LittleEndian, u16> Line  | Count  | Source  |  891  | 7.18k  | fn read_unaligned<R, E, N>(  |  892  | 7.18k  |     reader: R,  |  893  | 7.18k  |     bits: u32,  |  894  | 7.18k  |     acc: &mut BitQueue<E, N>,  |  895  | 7.18k  |     rem: &mut BitQueue<E, u8>,  |  896  | 7.18k  | ) -> io::Result<()>  |  897  | 7.18k  | where  |  898  | 7.18k  |     R: io::Read,  |  899  | 7.18k  |     E: Endianness,  |  900  | 7.18k  |     N: Numeric,  |  901  |  | { |  902  | 7.18k  |     debug_assert!(bits <= 8);  |  903  |  |  |  904  | 7.18k  |     if bits > 0 { |  905  | 7.18k  |         rem.set(read_byte(reader)?, 8);  |  906  | 7.18k  |         acc.push(bits, N::from_u8(rem.pop(bits)));  |  907  | 0  |     }  |  908  | 7.18k  |     Ok(())  |  909  | 7.18k  | }  |  
 bitstream_io::read::read_unaligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian, u8> Line  | Count  | Source  |  891  | 691k  | fn read_unaligned<R, E, N>(  |  892  | 691k  |     reader: R,  |  893  | 691k  |     bits: u32,  |  894  | 691k  |     acc: &mut BitQueue<E, N>,  |  895  | 691k  |     rem: &mut BitQueue<E, u8>,  |  896  | 691k  | ) -> io::Result<()>  |  897  | 691k  | where  |  898  | 691k  |     R: io::Read,  |  899  | 691k  |     E: Endianness,  |  900  | 691k  |     N: Numeric,  |  901  |  | { |  902  | 691k  |     debug_assert!(bits <= 8);  |  903  |  |  |  904  | 691k  |     if bits > 0 { |  905  | 433k  |         rem.set(read_byte(reader)?, 8);  |  906  | 432k  |         acc.push(bits, N::from_u8(rem.pop(bits)));  |  907  | 258k  |     }  |  908  | 690k  |     Ok(())  |  909  | 691k  | }  |  
 bitstream_io::read::read_unaligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian, u32> Line  | Count  | Source  |  891  | 1.20M  | fn read_unaligned<R, E, N>(  |  892  | 1.20M  |     reader: R,  |  893  | 1.20M  |     bits: u32,  |  894  | 1.20M  |     acc: &mut BitQueue<E, N>,  |  895  | 1.20M  |     rem: &mut BitQueue<E, u8>,  |  896  | 1.20M  | ) -> io::Result<()>  |  897  | 1.20M  | where  |  898  | 1.20M  |     R: io::Read,  |  899  | 1.20M  |     E: Endianness,  |  900  | 1.20M  |     N: Numeric,  |  901  |  | { |  902  | 1.20M  |     debug_assert!(bits <= 8);  |  903  |  |  |  904  | 1.20M  |     if bits > 0 { |  905  | 1.07M  |         rem.set(read_byte(reader)?, 8);  |  906  | 1.07M  |         acc.push(bits, N::from_u8(rem.pop(bits)));  |  907  | 126k  |     }  |  908  | 1.20M  |     Ok(())  |  909  | 1.20M  | }  |  
 bitstream_io::read::read_unaligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian, u16> Line  | Count  | Source  |  891  | 200k  | fn read_unaligned<R, E, N>(  |  892  | 200k  |     reader: R,  |  893  | 200k  |     bits: u32,  |  894  | 200k  |     acc: &mut BitQueue<E, N>,  |  895  | 200k  |     rem: &mut BitQueue<E, u8>,  |  896  | 200k  | ) -> io::Result<()>  |  897  | 200k  | where  |  898  | 200k  |     R: io::Read,  |  899  | 200k  |     E: Endianness,  |  900  | 200k  |     N: Numeric,  |  901  |  | { |  902  | 200k  |     debug_assert!(bits <= 8);  |  903  |  |  |  904  | 200k  |     if bits > 0 { |  905  | 135k  |         rem.set(read_byte(reader)?, 8);  |  906  | 134k  |         acc.push(bits, N::from_u8(rem.pop(bits)));  |  907  | 64.9k  |     }  |  908  | 199k  |     Ok(())  |  909  | 200k  | }  |  
 Unexecuted instantiation: bitstream_io::read::read_unaligned::<_, _, _>  | 
910  |  |  | 
911  |  | #[inline]  | 
912  | 13.6k  | fn skip_unaligned<R, E>(reader: R, bits: u32, rem: &mut BitQueue<E, u8>) -> io::Result<()>  | 
913  | 13.6k  | where  | 
914  | 13.6k  |     R: io::Read,  | 
915  | 13.6k  |     E: Endianness,  | 
916  |  | { | 
917  | 13.6k  |     debug_assert!(bits <= 8);  | 
918  |  |  | 
919  | 13.6k  |     if bits > 0 { | 
920  | 5.63k  |         rem.set(read_byte(reader)?, 8);  | 
921  | 5.63k  |         rem.pop(bits);  | 
922  | 8.05k  |     }  | 
923  | 13.6k  |     Ok(())  | 
924  | 13.6k  | } bitstream_io::read::skip_unaligned::<&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>>, bitstream_io::LittleEndian> Line  | Count  | Source  |  912  | 13.6k  | fn skip_unaligned<R, E>(reader: R, bits: u32, rem: &mut BitQueue<E, u8>) -> io::Result<()>  |  913  | 13.6k  | where  |  914  | 13.6k  |     R: io::Read,  |  915  | 13.6k  |     E: Endianness,  |  916  |  | { |  917  | 13.6k  |     debug_assert!(bits <= 8);  |  918  |  |  |  919  | 13.6k  |     if bits > 0 { |  920  | 5.63k  |         rem.set(read_byte(reader)?, 8);  |  921  | 5.63k  |         rem.pop(bits);  |  922  | 8.05k  |     }  |  923  | 13.6k  |     Ok(())  |  924  | 13.6k  | }  |  
 Unexecuted instantiation: bitstream_io::read::skip_unaligned::<_, _>  | 
925  |  |  | 
926  |  | #[inline]  | 
927  | 0  | fn read_aligned_unary<R, E>(  | 
928  | 0  |     mut reader: R,  | 
929  | 0  |     continue_val: u8,  | 
930  | 0  |     rem: &mut BitQueue<E, u8>,  | 
931  | 0  | ) -> io::Result<u32>  | 
932  | 0  | where  | 
933  | 0  |     R: io::Read,  | 
934  | 0  |     E: Endianness,  | 
935  |  | { | 
936  | 0  |     let mut acc = 0;  | 
937  | 0  |     let mut byte = read_byte(reader.by_ref())?;  | 
938  | 0  |     while byte == continue_val { | 
939  | 0  |         acc += 8;  | 
940  | 0  |         byte = read_byte(reader.by_ref())?;  | 
941  |  |     }  | 
942  | 0  |     rem.set(byte, 8);  | 
943  | 0  |     Ok(acc)  | 
944  | 0  | }  | 
945  |  |  | 
946  |  | /// A trait for anything that can read aligned values from an input stream  | 
947  |  | pub trait ByteRead { | 
948  |  |     /// Reads whole numeric value from stream  | 
949  |  |     ///  | 
950  |  |     /// # Errors  | 
951  |  |     ///  | 
952  |  |     /// Passes along any I/O error from the underlying stream.  | 
953  |  |     ///  | 
954  |  |     /// # Examples  | 
955  |  |     /// ```  | 
956  |  |     /// use std::io::{Read, Cursor}; | 
957  |  |     /// use bitstream_io::{BigEndian, ByteReader, ByteRead}; | 
958  |  |     /// let data = [0b00000000, 0b11111111];  | 
959  |  |     /// let mut reader = ByteReader::endian(Cursor::new(&data), BigEndian);  | 
960  |  |     /// assert_eq!(reader.read::<u16>().unwrap(), 0b0000000011111111);  | 
961  |  |     /// ```  | 
962  |  |     ///  | 
963  |  |     /// ```  | 
964  |  |     /// use std::io::{Read, Cursor}; | 
965  |  |     /// use bitstream_io::{LittleEndian, ByteReader, ByteRead}; | 
966  |  |     /// let data = [0b00000000, 0b11111111];  | 
967  |  |     /// let mut reader = ByteReader::endian(Cursor::new(&data), LittleEndian);  | 
968  |  |     /// assert_eq!(reader.read::<u16>().unwrap(), 0b1111111100000000);  | 
969  |  |     /// ```  | 
970  |  |     fn read<V: Primitive>(&mut self) -> Result<V, io::Error>;  | 
971  |  |  | 
972  |  |     /// Completely fills the given buffer with whole bytes.  | 
973  |  |     ///  | 
974  |  |     /// # Errors  | 
975  |  |     ///  | 
976  |  |     /// Passes along any I/O error from the underlying stream.  | 
977  | 0  |     fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> { | 
978  | 0  |         for b in buf.iter_mut() { | 
979  | 0  |             *b = self.read()?;  | 
980  |  |         }  | 
981  | 0  |         Ok(())  | 
982  | 0  |     }  | 
983  |  |  | 
984  |  |     /// Completely fills a whole buffer with bytes and returns it.  | 
985  |  |     ///  | 
986  |  |     /// # Errors  | 
987  |  |     ///  | 
988  |  |     /// Passes along any I/O error from the underlying stream.  | 
989  |  |     #[inline(always)]  | 
990  |  |     #[deprecated(since = "1.8.0", note = "use read() method instead")]  | 
991  | 0  |     fn read_to_bytes<const SIZE: usize>(&mut self) -> io::Result<[u8; SIZE]> { | 
992  | 0  |         self.read()  | 
993  | 0  |     }  | 
994  |  |  | 
995  |  |     /// Completely fills a vector of bytes and returns it.  | 
996  |  |     ///  | 
997  |  |     /// # Errors  | 
998  |  |     ///  | 
999  |  |     /// Passes along any I/O error from the underlying stream.  | 
1000  | 0  |     fn read_to_vec(&mut self, bytes: usize) -> io::Result<Vec<u8>> { | 
1001  | 0  |         let mut buf = vec![0; bytes];  | 
1002  | 0  |         self.read_bytes(&mut buf)?;  | 
1003  | 0  |         Ok(buf)  | 
1004  | 0  |     }  | 
1005  |  |  | 
1006  |  |     /// Skips the given number of bytes in the stream.  | 
1007  |  |     ///  | 
1008  |  |     /// # Errors  | 
1009  |  |     ///  | 
1010  |  |     /// Passes along any I/O error from the underlying stream.  | 
1011  |  |     fn skip(&mut self, bytes: u32) -> io::Result<()>;  | 
1012  |  |  | 
1013  |  |     /// Parses and returns complex type  | 
1014  | 0  |     fn parse<F: FromByteStream>(&mut self) -> Result<F, F::Error> { | 
1015  | 0  |         F::from_reader(self)  | 
1016  | 0  |     }  | 
1017  |  |  | 
1018  |  |     /// Parses and returns complex type with context  | 
1019  | 0  |     fn parse_with<F: FromByteStreamWith>(&mut self, context: &F::Context) -> Result<F, F::Error> { | 
1020  | 0  |         F::from_reader(self, context)  | 
1021  | 0  |     }  | 
1022  |  |  | 
1023  |  |     /// Returns mutable reference to underlying reader  | 
1024  |  |     fn reader_ref(&mut self) -> &mut dyn io::Read;  | 
1025  |  | }  | 
1026  |  |  | 
1027  |  | /// For reading aligned bytes from a stream of bytes in a given endianness.  | 
1028  |  | ///  | 
1029  |  | /// This only reads aligned values and maintains no internal state.  | 
1030  |  | pub struct ByteReader<R: io::Read, E: Endianness> { | 
1031  |  |     phantom: PhantomData<E>,  | 
1032  |  |     reader: R,  | 
1033  |  | }  | 
1034  |  |  | 
1035  |  | impl<R: io::Read, E: Endianness> ByteReader<R, E> { | 
1036  |  |     /// Wraps a ByteReader around something that implements `Read`  | 
1037  | 0  |     pub fn new(reader: R) -> ByteReader<R, E> { | 
1038  | 0  |         ByteReader { | 
1039  | 0  |             phantom: PhantomData,  | 
1040  | 0  |             reader,  | 
1041  | 0  |         }  | 
1042  | 0  |     }  | 
1043  |  |  | 
1044  |  |     /// Wraps a ByteReader around something that implements `Read`  | 
1045  |  |     /// with the given endianness.  | 
1046  | 0  |     pub fn endian(reader: R, _endian: E) -> ByteReader<R, E> { | 
1047  | 0  |         ByteReader { | 
1048  | 0  |             phantom: PhantomData,  | 
1049  | 0  |             reader,  | 
1050  | 0  |         }  | 
1051  | 0  |     }  | 
1052  |  |  | 
1053  |  |     /// Unwraps internal reader and disposes of `ByteReader`.  | 
1054  |  |     #[inline]  | 
1055  | 0  |     pub fn into_reader(self) -> R { | 
1056  | 0  |         self.reader  | 
1057  | 0  |     }  | 
1058  |  |  | 
1059  |  |     /// Provides mutable reference to internal reader  | 
1060  |  |     #[inline]  | 
1061  | 0  |     pub fn reader(&mut self) -> &mut R { | 
1062  | 0  |         &mut self.reader  | 
1063  | 0  |     }  | 
1064  |  |  | 
1065  |  |     /// Converts `ByteReader` to `BitReader` in the same endianness.  | 
1066  |  |     #[inline]  | 
1067  | 0  |     pub fn into_bitreader(self) -> BitReader<R, E> { | 
1068  | 0  |         BitReader::new(self.into_reader())  | 
1069  | 0  |     }  | 
1070  |  |  | 
1071  |  |     /// Provides temporary `BitReader` in the same endianness.  | 
1072  |  |     ///  | 
1073  |  |     /// # Warning  | 
1074  |  |     ///  | 
1075  |  |     /// Any unread bits left over when `BitReader` is dropped are lost.  | 
1076  |  |     #[inline]  | 
1077  | 0  |     pub fn bitreader(&mut self) -> BitReader<&mut R, E> { | 
1078  | 0  |         BitReader::new(self.reader())  | 
1079  | 0  |     }  | 
1080  |  | }  | 
1081  |  |  | 
1082  |  | impl<R: io::Read, E: Endianness> ByteRead for ByteReader<R, E> { | 
1083  |  |     #[inline]  | 
1084  | 0  |     fn read<V: Primitive>(&mut self) -> Result<V, io::Error> { | 
1085  | 0  |         E::read_numeric(&mut self.reader)  | 
1086  | 0  |     }  | 
1087  |  |  | 
1088  |  |     #[inline]  | 
1089  | 0  |     fn read_bytes(&mut self, buf: &mut [u8]) -> io::Result<()> { | 
1090  | 0  |         self.reader.read_exact(buf)  | 
1091  | 0  |     }  | 
1092  |  |  | 
1093  |  |     #[inline]  | 
1094  | 0  |     fn skip(&mut self, bytes: u32) -> io::Result<()> { | 
1095  | 0  |         skip_aligned(&mut self.reader, bytes)  | 
1096  | 0  |     }  | 
1097  |  |  | 
1098  |  |     #[inline]  | 
1099  | 0  |     fn reader_ref(&mut self) -> &mut dyn io::Read { | 
1100  | 0  |         &mut self.reader  | 
1101  | 0  |     }  | 
1102  |  | }  | 
1103  |  |  | 
1104  |  | /// Implemented by complex types that don't require any additional context  | 
1105  |  | /// to parse themselves from a reader.  Analagous to `FromStr`.  | 
1106  |  | ///  | 
1107  |  | /// # Example  | 
1108  |  | /// ```  | 
1109  |  | /// use std::io::{Cursor, Read}; | 
1110  |  | /// use bitstream_io::{BigEndian, BitRead, BitReader, FromBitStream}; | 
1111  |  | ///  | 
1112  |  | /// #[derive(Debug, PartialEq, Eq)]  | 
1113  |  | /// struct BlockHeader { | 
1114  |  | ///     last_block: bool,  | 
1115  |  | ///     block_type: u8,  | 
1116  |  | ///     block_size: u32,  | 
1117  |  | /// }  | 
1118  |  | ///  | 
1119  |  | /// impl FromBitStream for BlockHeader { | 
1120  |  | ///     type Error = std::io::Error;  | 
1121  |  | ///  | 
1122  |  | ///     fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> std::io::Result<Self> { | 
1123  |  | ///         Ok(Self { | 
1124  |  | ///             last_block: r.read_bit()?,  | 
1125  |  | ///             block_type: r.read(7)?,  | 
1126  |  | ///             block_size: r.read(24)?,  | 
1127  |  | ///         })  | 
1128  |  | ///     }  | 
1129  |  | /// }  | 
1130  |  | ///  | 
1131  |  | /// let mut reader = BitReader::endian(Cursor::new(b"\x04\x00\x00\x7A"), BigEndian);  | 
1132  |  | /// assert_eq!(  | 
1133  |  | ///     reader.parse::<BlockHeader>().unwrap(),  | 
1134  |  | ///     BlockHeader { last_block: false, block_type: 4, block_size: 122 } | 
1135  |  | /// );  | 
1136  |  | /// ```  | 
1137  |  | pub trait FromBitStream { | 
1138  |  |     /// Error generated during parsing, such as `io::Error`  | 
1139  |  |     type Error;  | 
1140  |  |  | 
1141  |  |     /// Parse Self from reader  | 
1142  |  |     fn from_reader<R: BitRead + ?Sized>(r: &mut R) -> Result<Self, Self::Error>  | 
1143  |  |     where  | 
1144  |  |         Self: Sized;  | 
1145  |  | }  | 
1146  |  |  | 
1147  |  | /// Implemented by complex types that require some immutable context  | 
1148  |  | /// to parse themselves from a reader.  | 
1149  |  | ///  | 
1150  |  | /// # Example  | 
1151  |  | /// ```  | 
1152  |  | /// use std::io::{Cursor, Read}; | 
1153  |  | /// use bitstream_io::{BigEndian, BitRead, BitReader, FromBitStreamWith}; | 
1154  |  | ///  | 
1155  |  | /// #[derive(Default)]  | 
1156  |  | /// struct Streaminfo { | 
1157  |  | ///     minimum_block_size: u16,  | 
1158  |  | ///     maximum_block_size: u16,  | 
1159  |  | ///     minimum_frame_size: u32,  | 
1160  |  | ///     maximum_frame_size: u32,  | 
1161  |  | ///     sample_rate: u32,  | 
1162  |  | ///     channels: u8,  | 
1163  |  | ///     bits_per_sample: u8,  | 
1164  |  | ///     total_samples: u64,  | 
1165  |  | ///     md5: [u8; 16],  | 
1166  |  | /// }  | 
1167  |  | ///  | 
1168  |  | /// #[derive(Debug, PartialEq, Eq)]  | 
1169  |  | /// struct FrameHeader { | 
1170  |  | ///     variable_block_size: bool,  | 
1171  |  | ///     block_size: u32,  | 
1172  |  | ///     sample_rate: u32,  | 
1173  |  | ///     channel_assignment: u8,  | 
1174  |  | ///     sample_size: u8,  | 
1175  |  | ///     frame_number: u64,  | 
1176  |  | ///     crc8: u8,  | 
1177  |  | /// }  | 
1178  |  | ///  | 
1179  |  | /// impl FromBitStreamWith for FrameHeader { | 
1180  |  | ///     type Context = Streaminfo;  | 
1181  |  | ///  | 
1182  |  | ///     type Error = FrameHeaderError;  | 
1183  |  | ///  | 
1184  |  | ///     fn from_reader<R: BitRead + ?Sized>(  | 
1185  |  | ///         r: &mut R,  | 
1186  |  | ///         streaminfo: &Streaminfo,  | 
1187  |  | ///     ) -> Result<Self, Self::Error> { | 
1188  |  | ///         if r.read::<u16>(14)? != 0b11111111111110 { | 
1189  |  | ///             return Err(FrameHeaderError::InvalidSync);  | 
1190  |  | ///         }  | 
1191  |  | ///  | 
1192  |  | ///         if r.read_bit()? != false { | 
1193  |  | ///             return Err(FrameHeaderError::InvalidReservedBit);  | 
1194  |  | ///         }  | 
1195  |  | ///  | 
1196  |  | ///         let variable_block_size = r.read_bit()?;  | 
1197  |  | ///  | 
1198  |  | ///         let block_size_bits = r.read::<u8>(4)?;  | 
1199  |  | ///  | 
1200  |  | ///         let sample_rate_bits = r.read::<u8>(4)?;  | 
1201  |  | ///  | 
1202  |  | ///         let channel_assignment = r.read::<u8>(4)?;  | 
1203  |  | ///  | 
1204  |  | ///         let sample_size = match r.read::<u8>(3)? { | 
1205  |  | ///             0b000 => streaminfo.bits_per_sample,  | 
1206  |  | ///             0b001 => 8,  | 
1207  |  | ///             0b010 => 12,  | 
1208  |  | ///             0b011 => return Err(FrameHeaderError::InvalidSampleSize),  | 
1209  |  | ///             0b100 => 16,  | 
1210  |  | ///             0b101 => 20,  | 
1211  |  | ///             0b110 => 24,  | 
1212  |  | ///             0b111 => 32,  | 
1213  |  | ///             _ => unreachable!(),  | 
1214  |  | ///         };  | 
1215  |  | ///  | 
1216  |  | ///         if r.read_bit()? != false { | 
1217  |  | ///             return Err(FrameHeaderError::InvalidReservedBit);  | 
1218  |  | ///         }  | 
1219  |  | ///  | 
1220  |  | ///         let frame_number = read_utf8(r)?;  | 
1221  |  | ///  | 
1222  |  | ///         Ok(FrameHeader { | 
1223  |  | ///             variable_block_size,  | 
1224  |  | ///             block_size: match block_size_bits { | 
1225  |  | ///                 0b0000 => return Err(FrameHeaderError::InvalidBlockSize),  | 
1226  |  | ///                 0b0001 => 192,  | 
1227  |  | ///                 n @ 0b010..=0b0101 => 576 * (1 << (n - 2)),  | 
1228  |  | ///                 0b0110 => r.read::<u32>(8)? + 1,  | 
1229  |  | ///                 0b0111 => r.read::<u32>(16)? + 1,  | 
1230  |  | ///                 n @ 0b1000..=0b1111 => 256 * (1 << (n - 8)),  | 
1231  |  | ///                 _ => unreachable!(),  | 
1232  |  | ///             },  | 
1233  |  | ///             sample_rate: match sample_rate_bits { | 
1234  |  | ///                 0b0000 => streaminfo.sample_rate,  | 
1235  |  | ///                 0b0001 => 88200,  | 
1236  |  | ///                 0b0010 => 176400,  | 
1237  |  | ///                 0b0011 => 192000,  | 
1238  |  | ///                 0b0100 => 8000,  | 
1239  |  | ///                 0b0101 => 16000,  | 
1240  |  | ///                 0b0110 => 22050,  | 
1241  |  | ///                 0b0111 => 24000,  | 
1242  |  | ///                 0b1000 => 32000,  | 
1243  |  | ///                 0b1001 => 44100,  | 
1244  |  | ///                 0b1010 => 48000,  | 
1245  |  | ///                 0b1011 => 96000,  | 
1246  |  | ///                 0b1100 => r.read::<u32>(8)? * 1000,  | 
1247  |  | ///                 0b1101 => r.read::<u32>(16)?,  | 
1248  |  | ///                 0b1110 => r.read::<u32>(16)? * 10,  | 
1249  |  | ///                 0b1111 => return Err(FrameHeaderError::InvalidSampleRate),  | 
1250  |  | ///                 _ => unreachable!(),  | 
1251  |  | ///             },  | 
1252  |  | ///             channel_assignment,  | 
1253  |  | ///             sample_size,  | 
1254  |  | ///             frame_number,  | 
1255  |  | ///             crc8: r.read(8)?  | 
1256  |  | ///         })  | 
1257  |  | ///     }  | 
1258  |  | /// }  | 
1259  |  | ///  | 
1260  |  | /// #[derive(Debug)]  | 
1261  |  | /// enum FrameHeaderError { | 
1262  |  | ///     Io(std::io::Error),  | 
1263  |  | ///     InvalidSync,  | 
1264  |  | ///     InvalidReservedBit,  | 
1265  |  | ///     InvalidSampleSize,  | 
1266  |  | ///     InvalidBlockSize,  | 
1267  |  | ///     InvalidSampleRate,  | 
1268  |  | /// }  | 
1269  |  | ///  | 
1270  |  | /// impl From<std::io::Error> for FrameHeaderError { | 
1271  |  | ///     fn from(err: std::io::Error) -> Self { | 
1272  |  | ///         Self::Io(err)  | 
1273  |  | ///     }  | 
1274  |  | /// }  | 
1275  |  | ///  | 
1276  |  | /// fn read_utf8<R: BitRead + ?Sized>(r: &mut R) -> Result<u64, std::io::Error> { | 
1277  |  | ///     r.read(8)  // left unimplimented in this example  | 
1278  |  | /// }  | 
1279  |  | ///  | 
1280  |  | /// let mut reader = BitReader::endian(Cursor::new(b"\xFF\xF8\xC9\x18\x00\xC2"), BigEndian);  | 
1281  |  | /// assert_eq!(  | 
1282  |  | ///     reader.parse_with::<FrameHeader>(&Streaminfo::default()).unwrap(),  | 
1283  |  | ///     FrameHeader { | 
1284  |  | ///         variable_block_size: false,  | 
1285  |  | ///         block_size: 4096,  | 
1286  |  | ///         sample_rate: 44100,  | 
1287  |  | ///         channel_assignment: 1,  | 
1288  |  | ///         sample_size: 16,  | 
1289  |  | ///         frame_number: 0,  | 
1290  |  | ///         crc8: 0xC2,  | 
1291  |  | ///     }  | 
1292  |  | /// );  | 
1293  |  | /// ```  | 
1294  |  | pub trait FromBitStreamWith { | 
1295  |  |     /// Some context to use when parsing  | 
1296  |  |     type Context;  | 
1297  |  |  | 
1298  |  |     /// Error generated during parsing, such as `io::Error`  | 
1299  |  |     type Error;  | 
1300  |  |  | 
1301  |  |     /// Parse Self from reader with the given context  | 
1302  |  |     fn from_reader<R: BitRead + ?Sized>(  | 
1303  |  |         r: &mut R,  | 
1304  |  |         context: &Self::Context,  | 
1305  |  |     ) -> Result<Self, Self::Error>  | 
1306  |  |     where  | 
1307  |  |         Self: Sized;  | 
1308  |  | }  | 
1309  |  |  | 
1310  |  | /// Implemented by complex types that don't require any additional context  | 
1311  |  | /// to parse themselves from a reader.  Analagous to `FromStr`.  | 
1312  |  | pub trait FromByteStream { | 
1313  |  |     /// Error generated during parsing, such as `io::Error`  | 
1314  |  |     type Error;  | 
1315  |  |  | 
1316  |  |     /// Parse Self from reader  | 
1317  |  |     fn from_reader<R: ByteRead + ?Sized>(r: &mut R) -> Result<Self, Self::Error>  | 
1318  |  |     where  | 
1319  |  |         Self: Sized;  | 
1320  |  | }  | 
1321  |  |  | 
1322  |  | /// Implemented by complex types that require some additional context  | 
1323  |  | /// to parse themselves from a reader.  Analagous to `FromStr`.  | 
1324  |  | pub trait FromByteStreamWith { | 
1325  |  |     /// Some context to use when parsing  | 
1326  |  |     type Context;  | 
1327  |  |  | 
1328  |  |     /// Error generated during parsing, such as `io::Error`  | 
1329  |  |     type Error;  | 
1330  |  |  | 
1331  |  |     /// Parse Self from reader  | 
1332  |  |     fn from_reader<R: ByteRead + ?Sized>(  | 
1333  |  |         r: &mut R,  | 
1334  |  |         context: &Self::Context,  | 
1335  |  |     ) -> Result<Self, Self::Error>  | 
1336  |  |     where  | 
1337  |  |         Self: Sized;  | 
1338  |  | }  |