/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bitstream-io-4.9.0/src/lib.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 helpers for bitstream handling functionality |
10 | | //! |
11 | | //! Bitstream readers are for reading signed and unsigned integer |
12 | | //! values from a stream whose sizes may not be whole bytes. |
13 | | //! Bitstream writers are for writing signed and unsigned integer |
14 | | //! values to a stream, also potentially un-aligned at a whole byte. |
15 | | //! |
16 | | //! Both big-endian and little-endian streams are supported. |
17 | | //! |
18 | | //! The only requirement for wrapped reader streams is that they must |
19 | | //! implement the [`io::Read`] trait, and the only requirement |
20 | | //! for writer streams is that they must implement the [`io::Write`] trait. |
21 | | //! |
22 | | //! In addition, reader streams do not consume any more bytes |
23 | | //! from the underlying reader than necessary, buffering only a |
24 | | //! single partial byte as needed. |
25 | | //! Writer streams also write out all whole bytes as they are accumulated. |
26 | | //! |
27 | | //! Readers and writers are also designed to work with integer |
28 | | //! types of any possible size. |
29 | | //! Many of Rust's built-in integer types are supported by default. |
30 | | |
31 | | //! # Minimum Compiler Version |
32 | | //! |
33 | | //! Beginning with version 2.4, the minimum compiler version has been |
34 | | //! updated to Rust 1.79. |
35 | | //! |
36 | | //! The issue is that reading an excessive number of |
37 | | //! bits to a type which is too small to hold them, |
38 | | //! or writing an excessive number of bits from too small of a type, |
39 | | //! are always errors: |
40 | | //! ``` |
41 | | //! use std::io::{Read, Cursor}; |
42 | | //! use bitstream_io::{BigEndian, BitReader, BitRead}; |
43 | | //! let data = [0; 10]; |
44 | | //! let mut r = BitReader::endian(Cursor::new(&data), BigEndian); |
45 | | //! let x: Result<u32, _> = r.read_var(64); // reading 64 bits to u32 always fails at runtime |
46 | | //! assert!(x.is_err()); |
47 | | //! ``` |
48 | | //! but those errors will not be caught until the program runs, |
49 | | //! which is less than ideal for the common case in which |
50 | | //! the number of bits is already known at compile-time. |
51 | | //! |
52 | | //! But starting with Rust 1.79, we can now have read and write methods |
53 | | //! which take a constant number of bits and can validate the number of bits |
54 | | //! are small enough for the type being read/written at compile-time: |
55 | | //! ```rust,compile_fail |
56 | | //! use std::io::{Read, Cursor}; |
57 | | //! use bitstream_io::{BigEndian, BitReader, BitRead}; |
58 | | //! let data = [0; 10]; |
59 | | //! let mut r = BitReader::endian(Cursor::new(&data), BigEndian); |
60 | | //! let x: Result<u32, _> = r.read::<64, _>(); // doesn't compile at all |
61 | | //! ``` |
62 | | //! Since catching potential bugs at compile-time is preferable |
63 | | //! to encountering errors at runtime, this will hopefully be |
64 | | //! an improvement in the long run. |
65 | | |
66 | | //! # Changes From 3.X.X |
67 | | //! |
68 | | //! Version 4.0.0 features significant optimizations to the [`BitRecorder`] |
69 | | //! and deprecates the [`BitCounter`] in favor of [`BitsWritten`], |
70 | | //! which no longer requires specifying an endianness. |
71 | | //! |
72 | | //! In addition, the [`BitRead::read_bytes`] and [`BitWrite::write_bytes`] |
73 | | //! methods are significantly optimized in the case of non-aligned |
74 | | //! reads and writes. |
75 | | //! |
76 | | //! Finally, the [`Endianness`] traits have been sealed so as not |
77 | | //! to be implemented by other packages. Given that other endianness |
78 | | //! types are extremely rare in file formats and end users should not |
79 | | //! have to implement this trait themselves, this should not be a |
80 | | //! concern. |
81 | | //! |
82 | | //! # Changes From 2.X.X |
83 | | //! |
84 | | //! Version 3.0.0 has made many breaking changes to the [`BitRead`] and |
85 | | //! [`BitWrite`] traits. |
86 | | //! |
87 | | //! The [`BitRead::read`] method takes a constant number of bits, |
88 | | //! and the [`BitRead::read_var`] method takes a variable number of bits |
89 | | //! (reversing the older [`BitRead2::read_in`] and [`BitRead2::read`] |
90 | | //! calling methods to emphasize using the constant-based one, |
91 | | //! which can do more validation at compile-time). |
92 | | //! A new [`BitRead2`] trait uses the older calling convention |
93 | | //! for compatibility with existing code and is available |
94 | | //! for anything implementing [`BitRead`]. |
95 | | //! |
96 | | //! In addition, the main reading methods return primitive types which |
97 | | //! implement a new [`Integer`] trait, |
98 | | //! which delegates to [`BitRead::read_unsigned`] |
99 | | //! or [`BitRead::read_signed`] depending on whether the output |
100 | | //! is an unsigned or signed type. |
101 | | //! |
102 | | //! [`BitWrite::write`] and [`BitWrite::write_var`] work |
103 | | //! similarly to the reader's `read` methods, taking anything |
104 | | //! that implements [`Integer`] and writing an unsigned or |
105 | | //! signed value to [`BitWrite::write_unsigned`] or |
106 | | //! [`BitWrite::write_signed`] as appropriate. |
107 | | //! |
108 | | //! And as with reading, a [`BitWrite2`] trait is offered |
109 | | //! for compatibility. |
110 | | //! |
111 | | //! In addition, the Huffman code handling has been rewritten |
112 | | //! to use a small amount of macro magic to write |
113 | | //! code to read and write symbols at compile-time. |
114 | | //! This is significantly faster than the older version |
115 | | //! and can no longer fail to compile at runtime. |
116 | | //! |
117 | | //! Lastly, there's a new [`BitCount`] struct which wraps a humble |
118 | | //! `u32` but encodes the maximum possible number of bits |
119 | | //! at the type level. |
120 | | //! This is intended for file formats which encode the number |
121 | | //! of bits to be read in the format itself. |
122 | | //! For example, FLAC's predictor coefficient precision |
123 | | //! is a 4 bit value which indicates how large each predictor |
124 | | //! coefficient is in bits |
125 | | //! (each coefficient might be an `i32` type). |
126 | | //! By keeping track of the maximum value at compile time |
127 | | //! (4 bits' worth, in this case), we can eliminate |
128 | | //! any need to check that coefficients aren't too large |
129 | | //! for an `i32` at runtime. |
130 | | //! This is accomplished by using [`BitRead::read_count`] to |
131 | | //! read a [`BitCount`] and then reading final values with |
132 | | //! that number of bits using [`BitRead::read_counted`]. |
133 | | |
134 | | //! # Migrating From Pre 1.0.0 |
135 | | //! |
136 | | //! There are now [`BitRead`] and [`BitWrite`] traits for bitstream |
137 | | //! reading and writing (analogous to the standard library's |
138 | | //! `Read` and `Write` traits) which you will also need to import. |
139 | | //! The upside to this approach is that library consumers |
140 | | //! can now make functions and methods generic over any sort |
141 | | //! of bit reader or bit writer, regardless of the underlying |
142 | | //! stream byte source or endianness. |
143 | | |
144 | | #![cfg_attr(docsrs, feature(doc_cfg))] |
145 | | #![warn(missing_docs)] |
146 | | #![forbid(unsafe_code)] |
147 | | #![no_std] |
148 | | |
149 | | #[cfg(feature = "alloc")] |
150 | | extern crate alloc; |
151 | | #[cfg(feature = "std")] |
152 | | extern crate std; |
153 | | |
154 | | #[cfg(not(feature = "std"))] |
155 | | use core2::io; |
156 | | |
157 | | use core::convert::TryInto; |
158 | | use core::num::NonZero; |
159 | | use core::ops::{ |
160 | | BitAnd, BitOr, BitOrAssign, BitXor, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, |
161 | | }; |
162 | | use core::{fmt::Debug, marker::PhantomData, mem}; |
163 | | #[cfg(feature = "std")] |
164 | | use std::io; |
165 | | |
166 | | pub mod huffman; |
167 | | pub mod read; |
168 | | pub mod write; |
169 | | pub use read::{ |
170 | | BitRead, BitRead2, BitReader, ByteRead, ByteReader, FromBitStream, FromBitStreamUsing, |
171 | | FromBitStreamWith, FromByteStream, FromByteStreamUsing, FromByteStreamWith, |
172 | | }; |
173 | | #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
174 | | #[cfg(feature = "alloc")] |
175 | | pub use write::BitRecorder; |
176 | | pub use write::{ |
177 | | BitWrite, BitWrite2, BitWriter, BitsWritten, ByteWrite, ByteWriter, ToBitStream, |
178 | | ToBitStreamUsing, ToBitStreamWith, ToByteStream, ToByteStreamUsing, ToByteStreamWith, |
179 | | }; |
180 | | |
181 | | #[allow(deprecated)] |
182 | | pub use write::BitCounter; |
183 | | |
184 | | /// A trait intended for simple fixed-length primitives (such as ints and floats) |
185 | | /// which allows them to be read and written to streams of |
186 | | /// different endiannesses verbatim. |
187 | | pub trait Primitive { |
188 | | /// The raw byte representation of this numeric type |
189 | | type Bytes: AsRef<[u8]> + AsMut<[u8]>; |
190 | | |
191 | | /// An empty buffer of this type's size |
192 | | fn buffer() -> Self::Bytes; |
193 | | |
194 | | /// Our value in big-endian bytes |
195 | | fn to_be_bytes(self) -> Self::Bytes; |
196 | | |
197 | | /// Our value in little-endian bytes |
198 | | fn to_le_bytes(self) -> Self::Bytes; |
199 | | |
200 | | /// Convert big-endian bytes to our value |
201 | | fn from_be_bytes(bytes: Self::Bytes) -> Self; |
202 | | |
203 | | /// Convert little-endian bytes to our value |
204 | | fn from_le_bytes(bytes: Self::Bytes) -> Self; |
205 | | } |
206 | | |
207 | | macro_rules! define_primitive_numeric { |
208 | | ($t:ty) => { |
209 | | impl Primitive for $t { |
210 | | type Bytes = [u8; mem::size_of::<$t>()]; |
211 | | |
212 | | #[inline(always)] |
213 | 0 | fn buffer() -> Self::Bytes { |
214 | 0 | [0; mem::size_of::<$t>()] |
215 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <i32 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <i64 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <i128 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <f32 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <f64 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <u64 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <u128 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <i8 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <i16 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <u8 as bitstream_io::Primitive>::buffer Unexecuted instantiation: <u16 as bitstream_io::Primitive>::buffer |
216 | | #[inline(always)] |
217 | 0 | fn to_be_bytes(self) -> Self::Bytes { |
218 | 0 | self.to_be_bytes() |
219 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <u64 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <u8 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <u16 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <i32 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <i64 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <i128 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <f32 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <f64 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <u128 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <i8 as bitstream_io::Primitive>::to_be_bytes Unexecuted instantiation: <i16 as bitstream_io::Primitive>::to_be_bytes |
220 | | #[inline(always)] |
221 | 0 | fn to_le_bytes(self) -> Self::Bytes { |
222 | 0 | self.to_le_bytes() |
223 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <u32 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <i32 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <i64 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <i128 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <f32 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <f64 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <u128 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <i8 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <i16 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <u8 as bitstream_io::Primitive>::to_le_bytes Unexecuted instantiation: <u16 as bitstream_io::Primitive>::to_le_bytes |
224 | | #[inline(always)] |
225 | 0 | fn from_be_bytes(bytes: Self::Bytes) -> Self { |
226 | 0 | <$t>::from_be_bytes(bytes) |
227 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <i32 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <i64 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <i128 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <f32 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <f64 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <u64 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <u128 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <i8 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <i16 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <u8 as bitstream_io::Primitive>::from_be_bytes Unexecuted instantiation: <u16 as bitstream_io::Primitive>::from_be_bytes |
228 | | #[inline(always)] |
229 | 0 | fn from_le_bytes(bytes: Self::Bytes) -> Self { |
230 | 0 | <$t>::from_le_bytes(bytes) |
231 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <i32 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <i64 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <i128 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <f32 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <f64 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <u64 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <u128 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <i8 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <i16 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <u8 as bitstream_io::Primitive>::from_le_bytes Unexecuted instantiation: <u16 as bitstream_io::Primitive>::from_le_bytes |
232 | | } |
233 | | }; |
234 | | } |
235 | | |
236 | | impl<const N: usize> Primitive for [u8; N] { |
237 | | type Bytes = [u8; N]; |
238 | | |
239 | | #[inline(always)] |
240 | 0 | fn buffer() -> Self::Bytes { |
241 | 0 | [0; N] |
242 | 0 | } |
243 | | |
244 | | #[inline(always)] |
245 | 0 | fn to_be_bytes(self) -> Self::Bytes { |
246 | 0 | self |
247 | 0 | } |
248 | | |
249 | | #[inline(always)] |
250 | 0 | fn to_le_bytes(self) -> Self::Bytes { |
251 | 0 | self |
252 | 0 | } |
253 | | |
254 | | #[inline(always)] |
255 | 0 | fn from_be_bytes(bytes: Self::Bytes) -> Self { |
256 | 0 | bytes |
257 | 0 | } |
258 | | |
259 | | #[inline(always)] |
260 | 0 | fn from_le_bytes(bytes: Self::Bytes) -> Self { |
261 | 0 | bytes |
262 | 0 | } |
263 | | } |
264 | | |
265 | | /// This trait is for integer types which can be read or written |
266 | | /// to a bit stream as a partial amount of bits. |
267 | | /// |
268 | | /// It unifies signed and unsigned integer types by delegating |
269 | | /// reads and writes to the signed and unsigned reading |
270 | | /// and writing methods as appropriate. |
271 | | pub trait Integer { |
272 | | /// Reads a value of ourself from the stream |
273 | | /// with the given number of bits. |
274 | | /// |
275 | | /// # Errors |
276 | | /// |
277 | | /// Passes along any I/O error from the underlying stream. |
278 | | /// A compile-time error occurs if the given number of bits |
279 | | /// is larger than our type. |
280 | | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
281 | | where |
282 | | Self: Sized; |
283 | | |
284 | | /// Reads a value of ourself from the stream |
285 | | /// with the given number of bits. |
286 | | /// |
287 | | /// # Errors |
288 | | /// |
289 | | /// Passes along any I/O error from the underlying stream. |
290 | | /// Also returns an error if our type is too small |
291 | | /// to hold the requested number of bits. |
292 | | fn read_var<const MAX: u32, R>(reader: &mut R, bits: BitCount<MAX>) -> io::Result<Self> |
293 | | where |
294 | | R: BitRead + ?Sized, |
295 | | Self: Sized; |
296 | | |
297 | | /// Writes ourself to the stream using the given const number of bits. |
298 | | /// |
299 | | /// # Errors |
300 | | /// |
301 | | /// Passes along any I/O error from the underlying stream. |
302 | | /// Returns an error if our value is too large |
303 | | /// to fit the given number of bits. |
304 | | /// A compile-time error occurs if the given number of bits |
305 | | /// is larger than our type. |
306 | | fn write<const BITS: u32, W: BitWrite + ?Sized>(self, writer: &mut W) -> io::Result<()>; |
307 | | |
308 | | /// Writes ourself to the stream using the given number of bits. |
309 | | /// |
310 | | /// # Errors |
311 | | /// |
312 | | /// Passes along any I/O error from the underlying stream. |
313 | | /// Returns an error if our value is too small |
314 | | /// to hold the given number of bits. |
315 | | /// Returns an error if our value is too large |
316 | | /// to fit the given number of bits. |
317 | | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
318 | | self, |
319 | | writer: &mut W, |
320 | | bits: BitCount<MAX>, |
321 | | ) -> io::Result<()>; |
322 | | } |
323 | | |
324 | | /// This trait is for integer types which can be read or written |
325 | | /// to a bit stream as variable-width integers. |
326 | | /// |
327 | | /// It unifies signed and unsigned integer types by delegating |
328 | | /// reads and write to the signed and unsigned vbr reading and |
329 | | /// writing methods as appropriate. |
330 | | pub trait VBRInteger: Integer { |
331 | | /// Reads a value of ourself from the stream using a variable width integer. |
332 | | /// |
333 | | /// # Errors |
334 | | /// |
335 | | /// Passes along any I/O error from the underlying stream. |
336 | | fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self> |
337 | | where |
338 | | R: BitRead + ?Sized, |
339 | | Self: Sized; |
340 | | |
341 | | /// Writes ourself to the stream using a variable width integer. |
342 | | /// |
343 | | /// # Errors |
344 | | /// |
345 | | /// Passes along any I/O error from the underlying stream. |
346 | | fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>( |
347 | | self, |
348 | | writer: &mut W, |
349 | | ) -> io::Result<()>; |
350 | | } |
351 | | |
352 | | /// Reading and writing booleans as `Integer` requires the number of bits to be 1. |
353 | | /// |
354 | | /// This is more useful when combined with the fixed array target |
355 | | /// for reading blocks of bit flags. |
356 | | /// |
357 | | /// # Example |
358 | | /// ``` |
359 | | /// use bitstream_io::{BitReader, BitRead, BigEndian}; |
360 | | /// |
361 | | /// #[derive(Debug, PartialEq, Eq)] |
362 | | /// struct Flags { |
363 | | /// a: bool, |
364 | | /// b: bool, |
365 | | /// c: bool, |
366 | | /// d: bool, |
367 | | /// } |
368 | | /// |
369 | | /// let data: &[u8] = &[0b1011_0000]; |
370 | | /// let mut r = BitReader::endian(data, BigEndian); |
371 | | /// // note the number of bits must be 1 per read |
372 | | /// // while the quantity of flags is indicated by the array length |
373 | | /// let flags = r.read::<1, [bool; 4]>().map(|[a, b, c, d]| Flags { a, b, c, d }).unwrap(); |
374 | | /// assert_eq!(flags, Flags { a: true, b: false, c: true, d: true }); |
375 | | /// ``` |
376 | | impl Integer for bool { |
377 | | #[inline(always)] |
378 | 0 | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
379 | 0 | where |
380 | 0 | Self: Sized, |
381 | | { |
382 | | const { |
383 | | assert!(BITS == 1, "booleans require exactly 1 bit"); |
384 | | } |
385 | | |
386 | 0 | reader.read_bit() |
387 | 0 | } |
388 | | |
389 | 0 | fn read_var<const MAX: u32, R>( |
390 | 0 | reader: &mut R, |
391 | 0 | BitCount { bits }: BitCount<MAX>, |
392 | 0 | ) -> io::Result<Self> |
393 | 0 | where |
394 | 0 | R: BitRead + ?Sized, |
395 | 0 | Self: Sized, |
396 | | { |
397 | 0 | if bits == 1 { |
398 | 0 | reader.read_bit() |
399 | | } else { |
400 | 0 | Err(io::Error::new( |
401 | 0 | io::ErrorKind::InvalidInput, |
402 | 0 | "booleans require exactly 1 bit", |
403 | 0 | )) |
404 | | } |
405 | 0 | } |
406 | | |
407 | | #[inline(always)] |
408 | 0 | fn write<const BITS: u32, W: BitWrite + ?Sized>(self, writer: &mut W) -> io::Result<()> { |
409 | | const { |
410 | | assert!(BITS == 1, "booleans require exactly 1 bit"); |
411 | | } |
412 | | |
413 | 0 | writer.write_bit(self) |
414 | 0 | } |
415 | | |
416 | 0 | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
417 | 0 | self, |
418 | 0 | writer: &mut W, |
419 | 0 | BitCount { bits }: BitCount<MAX>, |
420 | 0 | ) -> io::Result<()> { |
421 | 0 | if bits == 1 { |
422 | 0 | writer.write_bit(self) |
423 | | } else { |
424 | 0 | Err(io::Error::new( |
425 | 0 | io::ErrorKind::InvalidInput, |
426 | 0 | "booleans require exactly 1 bit", |
427 | 0 | )) |
428 | | } |
429 | 0 | } |
430 | | } |
431 | | |
432 | | impl<const SIZE: usize, I: Integer + Copy + Default> Integer for [I; SIZE] { |
433 | | #[inline] |
434 | 0 | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
435 | 0 | where |
436 | 0 | Self: Sized, |
437 | | { |
438 | 0 | let mut a = [I::default(); SIZE]; |
439 | | |
440 | 0 | a.iter_mut().try_for_each(|v| { |
441 | 0 | *v = reader.read::<BITS, I>()?; |
442 | 0 | Ok::<(), io::Error>(()) |
443 | 0 | })?; |
444 | | |
445 | 0 | Ok(a) |
446 | 0 | } |
447 | | |
448 | | #[inline] |
449 | 0 | fn read_var<const MAX: u32, R>(reader: &mut R, count: BitCount<MAX>) -> io::Result<Self> |
450 | 0 | where |
451 | 0 | R: BitRead + ?Sized, |
452 | 0 | Self: Sized, |
453 | | { |
454 | 0 | let mut a = [I::default(); SIZE]; |
455 | | |
456 | 0 | a.iter_mut().try_for_each(|v| { |
457 | 0 | *v = reader.read_counted(count)?; |
458 | 0 | Ok::<(), io::Error>(()) |
459 | 0 | })?; |
460 | | |
461 | 0 | Ok(a) |
462 | 0 | } |
463 | | |
464 | | #[inline] |
465 | 0 | fn write<const BITS: u32, W: BitWrite + ?Sized>(self, writer: &mut W) -> io::Result<()> { |
466 | 0 | IntoIterator::into_iter(self).try_for_each(|v| writer.write::<BITS, I>(v)) |
467 | 0 | } |
468 | | |
469 | | #[inline] |
470 | 0 | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
471 | 0 | self, |
472 | 0 | writer: &mut W, |
473 | 0 | count: BitCount<MAX>, |
474 | 0 | ) -> io::Result<()> { |
475 | 0 | IntoIterator::into_iter(self).try_for_each(|v| writer.write_counted(count, v)) |
476 | 0 | } |
477 | | } |
478 | | |
479 | | impl<const SIZE: usize, I: VBRInteger + Copy + Default> VBRInteger for [I; SIZE] { |
480 | 0 | fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self> |
481 | 0 | where |
482 | 0 | R: BitRead + ?Sized, |
483 | 0 | Self: Sized, |
484 | | { |
485 | 0 | let mut a = [I::default(); SIZE]; |
486 | | |
487 | 0 | a.iter_mut().try_for_each(|v| { |
488 | 0 | I::read_vbr::<FIELD_SIZE, R>(reader).map(|item| { |
489 | 0 | *v = item; |
490 | 0 | }) |
491 | 0 | })?; |
492 | | |
493 | 0 | Ok(a) |
494 | 0 | } |
495 | | |
496 | 0 | fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>( |
497 | 0 | self, |
498 | 0 | writer: &mut W, |
499 | 0 | ) -> io::Result<()> { |
500 | 0 | IntoIterator::into_iter(self).try_for_each(|v| I::write_vbr::<FIELD_SIZE, W>(v, writer)) |
501 | 0 | } |
502 | | } |
503 | | |
504 | | /// This trait extends many common integer types (both unsigned and signed) |
505 | | /// with a few trivial methods so that they can be used |
506 | | /// with the bitstream handling traits. |
507 | | pub trait Numeric: |
508 | | Primitive |
509 | | + Sized |
510 | | + Copy |
511 | | + Default |
512 | | + Debug |
513 | | + PartialOrd |
514 | | + Shl<u32, Output = Self> |
515 | | + ShlAssign<u32> |
516 | | + Shr<u32, Output = Self> |
517 | | + ShrAssign<u32> |
518 | | + Rem<Self, Output = Self> |
519 | | + RemAssign<Self> |
520 | | + BitAnd<Self, Output = Self> |
521 | | + BitOr<Self, Output = Self> |
522 | | + BitOrAssign<Self> |
523 | | + BitXor<Self, Output = Self> |
524 | | + Not<Output = Self> |
525 | | + Sub<Self, Output = Self> |
526 | | { |
527 | | /// Size of type in bits |
528 | | const BITS_SIZE: u32; |
529 | | |
530 | | /// The value of 0 in this type |
531 | | const ZERO: Self; |
532 | | |
533 | | /// The value of 1 in this type |
534 | | const ONE: Self; |
535 | | |
536 | | /// Returns a `u8` value in this type |
537 | | fn from_u8(u: u8) -> Self; |
538 | | |
539 | | /// Assuming 0 <= value < 256, returns this value as a `u8` type |
540 | | fn to_u8(self) -> u8; |
541 | | } |
542 | | |
543 | | macro_rules! define_numeric { |
544 | | ($t:ty) => { |
545 | | define_primitive_numeric!($t); |
546 | | |
547 | | impl Numeric for $t { |
548 | | const BITS_SIZE: u32 = mem::size_of::<$t>() as u32 * 8; |
549 | | |
550 | | const ZERO: Self = 0; |
551 | | |
552 | | const ONE: Self = 1; |
553 | | |
554 | | #[inline(always)] |
555 | 0 | fn from_u8(u: u8) -> Self { |
556 | 0 | u as $t |
557 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <u32 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <i32 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <i64 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <i128 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <u128 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <i8 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <i16 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <u8 as bitstream_io::Numeric>::from_u8 Unexecuted instantiation: <u16 as bitstream_io::Numeric>::from_u8 |
558 | | #[inline(always)] |
559 | 0 | fn to_u8(self) -> u8 { |
560 | 0 | self as u8 |
561 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <u64 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <u8 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <u16 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <i32 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <i64 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <i128 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <u128 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <i8 as bitstream_io::Numeric>::to_u8 Unexecuted instantiation: <i16 as bitstream_io::Numeric>::to_u8 |
562 | | } |
563 | | }; |
564 | | } |
565 | | |
566 | | /// This trait extends many common unsigned integer types |
567 | | /// so that they can be used with the bitstream handling traits. |
568 | | pub trait UnsignedInteger: Numeric { |
569 | | /// This type's most-significant bit |
570 | | const MSB_BIT: Self; |
571 | | |
572 | | /// This type's least significant bit |
573 | | const LSB_BIT: Self; |
574 | | |
575 | | /// This type with all bits set |
576 | | const ALL: Self; |
577 | | |
578 | | /// The signed variant of ourself |
579 | | type Signed: SignedInteger<Unsigned = Self>; |
580 | | |
581 | | /// Given a twos-complement value, |
582 | | /// return this value is a non-negative signed number. |
583 | | /// The location of the sign bit depends on the stream's endianness |
584 | | /// and is not stored in the result. |
585 | | /// |
586 | | /// # Example |
587 | | /// ``` |
588 | | /// use bitstream_io::UnsignedInteger; |
589 | | /// assert_eq!(0b00000001u8.as_non_negative(), 1i8); |
590 | | /// ``` |
591 | | fn as_non_negative(self) -> Self::Signed; |
592 | | |
593 | | /// Given a two-complement positive value and certain number of bits, |
594 | | /// returns this value as a negative signed number. |
595 | | /// The location of the sign bit depends on the stream's endianness |
596 | | /// and is not stored in the result. |
597 | | /// |
598 | | /// # Example |
599 | | /// ``` |
600 | | /// use bitstream_io::UnsignedInteger; |
601 | | /// assert_eq!(0b01111111u8.as_negative(8), -1i8); |
602 | | /// ``` |
603 | | fn as_negative(self, bits: u32) -> Self::Signed; |
604 | | |
605 | | /// Given a two-complement positive value and certain number of bits, |
606 | | /// returns this value as a negative number. |
607 | | /// |
608 | | /// # Example |
609 | | /// ``` |
610 | | /// use bitstream_io::UnsignedInteger; |
611 | | /// assert_eq!(0b01111111u8.as_negative_fixed::<8>(), -1i8); |
612 | | /// ``` |
613 | | fn as_negative_fixed<const BITS: u32>(self) -> Self::Signed; |
614 | | |
615 | | /// Checked shift left |
616 | | fn checked_shl(self, rhs: u32) -> Option<Self>; |
617 | | |
618 | | /// Checked shift right |
619 | | fn checked_shr(self, rhs: u32) -> Option<Self>; |
620 | | |
621 | | /// Shift left up to our length in bits |
622 | | /// |
623 | | /// If rhs equals our length in bits, returns default |
624 | 0 | fn shl_default(self, rhs: u32) -> Self { |
625 | 0 | self.checked_shl(rhs).unwrap_or(Self::ZERO) |
626 | 0 | } Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::shl_default Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::shl_default Unexecuted instantiation: <_ as bitstream_io::UnsignedInteger>::shl_default |
627 | | |
628 | | /// Shift left up to our length in bits |
629 | | /// |
630 | | /// If rhs equals our length in bits, returns zero |
631 | 0 | fn shr_default(self, rhs: u32) -> Self { |
632 | 0 | self.checked_shr(rhs).unwrap_or(Self::ZERO) |
633 | 0 | } Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::shr_default Unexecuted instantiation: <u32 as bitstream_io::UnsignedInteger>::shr_default Unexecuted instantiation: <u16 as bitstream_io::UnsignedInteger>::shr_default Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::shr_default Unexecuted instantiation: <_ as bitstream_io::UnsignedInteger>::shr_default |
634 | | } |
635 | | |
636 | | macro_rules! define_unsigned_integer { |
637 | | ($t:ty, $s:ty) => { |
638 | | define_numeric!($t); |
639 | | |
640 | | impl UnsignedInteger for $t { |
641 | | type Signed = $s; |
642 | | |
643 | | const MSB_BIT: Self = 1 << (Self::BITS_SIZE - 1); |
644 | | |
645 | | const LSB_BIT: Self = 1; |
646 | | |
647 | | const ALL: Self = <$t>::MAX; |
648 | | |
649 | | #[inline(always)] |
650 | 0 | fn as_non_negative(self) -> Self::Signed { |
651 | 0 | self as $s |
652 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::as_non_negative Unexecuted instantiation: <u128 as bitstream_io::UnsignedInteger>::as_non_negative Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::as_non_negative Unexecuted instantiation: <u16 as bitstream_io::UnsignedInteger>::as_non_negative Unexecuted instantiation: <u32 as bitstream_io::UnsignedInteger>::as_non_negative |
653 | | #[inline(always)] |
654 | 0 | fn as_negative(self, bits: u32) -> Self::Signed { |
655 | 0 | (self as $s) + (-1 << (bits - 1)) |
656 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::as_negative Unexecuted instantiation: <u128 as bitstream_io::UnsignedInteger>::as_negative Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::as_negative Unexecuted instantiation: <u16 as bitstream_io::UnsignedInteger>::as_negative Unexecuted instantiation: <u32 as bitstream_io::UnsignedInteger>::as_negative |
657 | | #[inline(always)] |
658 | 0 | fn as_negative_fixed<const BITS: u32>(self) -> Self::Signed { |
659 | 0 | (self as $s) + (-1 << (BITS - 1)) |
660 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <u128 as bitstream_io::UnsignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <u16 as bitstream_io::UnsignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <u32 as bitstream_io::UnsignedInteger>::as_negative_fixed::<_> |
661 | | #[inline(always)] |
662 | 0 | fn checked_shl(self, rhs: u32) -> Option<Self> { |
663 | 0 | self.checked_shl(rhs) |
664 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::checked_shl Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::checked_shl Unexecuted instantiation: <u128 as bitstream_io::UnsignedInteger>::checked_shl Unexecuted instantiation: <u16 as bitstream_io::UnsignedInteger>::checked_shl Unexecuted instantiation: <u32 as bitstream_io::UnsignedInteger>::checked_shl |
665 | | #[inline(always)] |
666 | 0 | fn checked_shr(self, rhs: u32) -> Option<Self> { |
667 | 0 | self.checked_shr(rhs) |
668 | 0 | } Unexecuted instantiation: <u64 as bitstream_io::UnsignedInteger>::checked_shr Unexecuted instantiation: <u8 as bitstream_io::UnsignedInteger>::checked_shr Unexecuted instantiation: <u16 as bitstream_io::UnsignedInteger>::checked_shr Unexecuted instantiation: <u32 as bitstream_io::UnsignedInteger>::checked_shr Unexecuted instantiation: <u128 as bitstream_io::UnsignedInteger>::checked_shr |
669 | | // TODO - enable these in the future |
670 | | // #[inline(always)] |
671 | | // fn shl_default(self, rhs: u32) -> Self { |
672 | | // self.unbounded_shl(rhs) |
673 | | // } |
674 | | // #[inline(always)] |
675 | | // fn shr_default(self, rhs: u32) -> Self { |
676 | | // self.unbounded_shr(rhs) |
677 | | // } |
678 | | } |
679 | | |
680 | | impl Integer for $t { |
681 | | #[inline(always)] |
682 | 0 | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
683 | 0 | where |
684 | 0 | Self: Sized, |
685 | | { |
686 | 0 | reader.read_unsigned::<BITS, _>() |
687 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <u64 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <u128 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <u8 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <u16 as bitstream_io::Integer>::read::<_, _> |
688 | | |
689 | | #[inline(always)] |
690 | 0 | fn read_var<const MAX: u32, R>(reader: &mut R, bits: BitCount<MAX>) -> io::Result<Self> |
691 | 0 | where |
692 | 0 | R: BitRead + ?Sized, |
693 | 0 | Self: Sized, |
694 | | { |
695 | 0 | reader.read_unsigned_counted::<MAX, _>(bits) |
696 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <u64 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <u128 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <u8 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <u16 as bitstream_io::Integer>::read_var::<_, _> |
697 | | |
698 | | #[inline(always)] |
699 | 0 | fn write<const BITS: u32, W: BitWrite + ?Sized>( |
700 | 0 | self, |
701 | 0 | writer: &mut W, |
702 | 0 | ) -> io::Result<()> { |
703 | 0 | writer.write_unsigned::<BITS, _>(self) |
704 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Integer>::write::<16, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u32 as bitstream_io::Integer>::write::<32, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u32 as bitstream_io::Integer>::write::<2, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u32 as bitstream_io::Integer>::write::<3, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u32 as bitstream_io::Integer>::write::<4, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u64 as bitstream_io::Integer>::write::<32, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<1, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<2, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<3, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<4, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<5, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<6, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<7, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<8, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write::<16, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write::<1, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write::<9, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write::<12, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u32 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <u64 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <u128 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write::<_, _> |
705 | | |
706 | | #[inline(always)] |
707 | 0 | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
708 | 0 | self, |
709 | 0 | writer: &mut W, |
710 | 0 | bits: BitCount<MAX>, |
711 | 0 | ) -> io::Result<()> { |
712 | 0 | writer.write_unsigned_counted(bits, self) |
713 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::Integer>::write_var::<4294967295, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u64 as bitstream_io::Integer>::write_var::<4294967295, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::LittleEndian>> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write_var::<4294967295, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write_var::<4294967295, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <u32 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <u64 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <u128 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <u8 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <u16 as bitstream_io::Integer>::write_var::<_, _> |
714 | | } |
715 | | |
716 | | impl VBRInteger for $t { |
717 | | #[inline(always)] |
718 | 0 | fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self> |
719 | 0 | where |
720 | 0 | R: BitRead + ?Sized, |
721 | 0 | Self: Sized, |
722 | | { |
723 | 0 | reader.read_unsigned_vbr::<FIELD_SIZE, _>() |
724 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <u64 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <u128 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <u8 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <u16 as bitstream_io::VBRInteger>::read_vbr::<_, _> |
725 | | |
726 | | #[inline(always)] |
727 | 0 | fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>( |
728 | 0 | self, |
729 | 0 | writer: &mut W, |
730 | 0 | ) -> io::Result<()> { |
731 | 0 | writer.write_unsigned_vbr::<FIELD_SIZE, _>(self) |
732 | 0 | } Unexecuted instantiation: <u32 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <u64 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <u128 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <u8 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <u16 as bitstream_io::VBRInteger>::write_vbr::<_, _> |
733 | | } |
734 | | |
735 | | /// Unsigned NonZero types increment their value by 1 |
736 | | /// when being read and decrement it by 1 |
737 | | /// when being written. |
738 | | /// |
739 | | /// # Examples |
740 | | /// ``` |
741 | | /// use bitstream_io::{BitReader, BitRead, BigEndian}; |
742 | | /// use core::num::NonZero; |
743 | | /// |
744 | | /// let data: &[u8] = &[0b001_00000]; |
745 | | /// // reading a regular u8 in 3 bits yields 1 |
746 | | /// assert_eq!(BitReader::endian(data, BigEndian).read::<3, u8>().unwrap(), 1); |
747 | | /// // reading a NonZero<u8> in 3 bits of the same data yields 2 |
748 | | /// assert_eq!(BitReader::endian(data, BigEndian).read::<3, NonZero<u8>>().unwrap().get(), 2); |
749 | | /// ``` |
750 | | /// |
751 | | /// ``` |
752 | | /// use bitstream_io::{BitWriter, BitWrite, BigEndian}; |
753 | | /// use core::num::NonZero; |
754 | | /// |
755 | | /// let mut w = BitWriter::endian(vec![], BigEndian); |
756 | | /// // writing 1 as a regular u8 in 3 bits |
757 | | /// w.write::<3, u8>(1).unwrap(); |
758 | | /// w.byte_align(); |
759 | | /// assert_eq!(w.into_writer(), &[0b001_00000]); |
760 | | /// |
761 | | /// let mut w = BitWriter::endian(vec![], BigEndian); |
762 | | /// // writing 1 as a NonZero<u8> in 3 bits |
763 | | /// w.write::<3, NonZero<u8>>(NonZero::new(1).unwrap()).unwrap(); |
764 | | /// w.byte_align(); |
765 | | /// assert_eq!(w.into_writer(), &[0b000_00000]); |
766 | | /// ``` |
767 | | impl Integer for NonZero<$t> { |
768 | | #[inline] |
769 | 0 | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
770 | 0 | where |
771 | 0 | Self: Sized, |
772 | | { |
773 | | const { |
774 | | assert!( |
775 | | BITS < <$t>::BITS_SIZE, |
776 | | "BITS must be less than the type's size in bits" |
777 | | ); |
778 | | } |
779 | | |
780 | 0 | <$t as Integer>::read::<BITS, R>(reader).map(|u| NonZero::new(u + 1).unwrap()) Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::Integer>::read::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::Integer>::read::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::Integer>::read::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::Integer>::read::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::Integer>::read::<_, _>::{closure#0} |
781 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::Integer>::read::<_, _> |
782 | | |
783 | | #[inline] |
784 | 0 | fn read_var<const MAX: u32, R>( |
785 | 0 | reader: &mut R, |
786 | 0 | count @ BitCount { bits }: BitCount<MAX>, |
787 | 0 | ) -> io::Result<Self> |
788 | 0 | where |
789 | 0 | R: BitRead + ?Sized, |
790 | 0 | Self: Sized, |
791 | | { |
792 | 0 | if MAX < <$t>::BITS_SIZE || bits < <$t>::BITS_SIZE { |
793 | 0 | <$t as Integer>::read_var::<MAX, R>(reader, count) |
794 | 0 | .map(|u| NonZero::new(u + 1).unwrap()) Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::Integer>::read_var::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::Integer>::read_var::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::Integer>::read_var::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::Integer>::read_var::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::Integer>::read_var::<_, _>::{closure#0} |
795 | | } else { |
796 | 0 | Err(io::Error::new( |
797 | 0 | io::ErrorKind::InvalidInput, |
798 | 0 | "bit count must be less than the type's size in bits", |
799 | 0 | )) |
800 | | } |
801 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::Integer>::read_var::<_, _> |
802 | | |
803 | | #[inline] |
804 | 0 | fn write<const BITS: u32, W: BitWrite + ?Sized>( |
805 | 0 | self, |
806 | 0 | writer: &mut W, |
807 | 0 | ) -> io::Result<()> { |
808 | | const { |
809 | | assert!( |
810 | | BITS < <$t>::BITS_SIZE, |
811 | | "BITS must be less than the type's size in bits" |
812 | | ); |
813 | | } |
814 | | |
815 | 0 | <$t as Integer>::write::<BITS, W>(self.get() - 1, writer) |
816 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::Integer>::write::<_, _> |
817 | | |
818 | | #[inline] |
819 | 0 | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
820 | 0 | self, |
821 | 0 | writer: &mut W, |
822 | 0 | count @ BitCount { bits }: BitCount<MAX>, |
823 | 0 | ) -> io::Result<()> { |
824 | 0 | if MAX < <$t>::BITS_SIZE || bits < <$t>::BITS_SIZE { |
825 | 0 | <$t as Integer>::write_var::<MAX, W>(self.get() - 1, writer, count) |
826 | | } else { |
827 | 0 | Err(io::Error::new( |
828 | 0 | io::ErrorKind::InvalidInput, |
829 | 0 | "bit count must be less than the type's size in bits", |
830 | 0 | )) |
831 | | } |
832 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::Integer>::write_var::<_, _> |
833 | | } |
834 | | |
835 | | impl VBRInteger for NonZero<$t> { |
836 | | #[inline] |
837 | 0 | fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self> |
838 | 0 | where |
839 | 0 | R: BitRead + ?Sized, |
840 | 0 | Self: Sized, |
841 | | { |
842 | 0 | <$t as VBRInteger>::read_vbr::<FIELD_SIZE, R>(reader) |
843 | 0 | .map(|u| NonZero::new(u + 1).unwrap()) Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::VBRInteger>::read_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::VBRInteger>::read_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::VBRInteger>::read_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::VBRInteger>::read_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::VBRInteger>::read_vbr::<_, _>::{closure#0} |
844 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::VBRInteger>::read_vbr::<_, _> |
845 | | |
846 | | #[inline] |
847 | 0 | fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>( |
848 | 0 | self, |
849 | 0 | writer: &mut W, |
850 | 0 | ) -> io::Result<()> { |
851 | 0 | <$t as VBRInteger>::write_vbr::<FIELD_SIZE, W>(self.get() - 1, writer) |
852 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as bitstream_io::VBRInteger>::write_vbr::<_, _> |
853 | | } |
854 | | |
855 | | impl Integer for Option<NonZero<$t>> { |
856 | | #[inline] |
857 | 0 | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
858 | 0 | where |
859 | 0 | Self: Sized, |
860 | | { |
861 | 0 | <$t as Integer>::read::<BITS, R>(reader).map(NonZero::new) |
862 | 0 | } Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::Integer>::read::<_, _> |
863 | | |
864 | | #[inline] |
865 | 0 | fn read_var<const MAX: u32, R>(reader: &mut R, count: BitCount<MAX>) -> io::Result<Self> |
866 | 0 | where |
867 | 0 | R: BitRead + ?Sized, |
868 | 0 | Self: Sized, |
869 | | { |
870 | 0 | <$t as Integer>::read_var::<MAX, R>(reader, count).map(NonZero::new) |
871 | 0 | } Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::Integer>::read_var::<_, _> |
872 | | |
873 | | #[inline] |
874 | 0 | fn write<const BITS: u32, W: BitWrite + ?Sized>( |
875 | 0 | self, |
876 | 0 | writer: &mut W, |
877 | 0 | ) -> io::Result<()> { |
878 | 0 | <$t as Integer>::write::<BITS, W>(self.map(|n| n.get()).unwrap_or(0), writer) Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::Integer>::write::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::Integer>::write::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::Integer>::write::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::Integer>::write::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::Integer>::write::<_, _>::{closure#0} |
879 | 0 | } Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::Integer>::write::<_, _> |
880 | | |
881 | | #[inline] |
882 | 0 | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
883 | 0 | self, |
884 | 0 | writer: &mut W, |
885 | 0 | count: BitCount<MAX>, |
886 | 0 | ) -> io::Result<()> { |
887 | 0 | <$t as Integer>::write_var::<MAX, W>( |
888 | 0 | self.map(|n| n.get()).unwrap_or(0), Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::Integer>::write_var::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::Integer>::write_var::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::Integer>::write_var::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::Integer>::write_var::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::Integer>::write_var::<_, _>::{closure#0} |
889 | 0 | writer, |
890 | 0 | count, |
891 | | ) |
892 | 0 | } Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::Integer>::write_var::<_, _> |
893 | | } |
894 | | |
895 | | impl VBRInteger for Option<NonZero<$t>> { |
896 | | #[inline(always)] |
897 | 0 | fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self> |
898 | 0 | where |
899 | 0 | R: BitRead + ?Sized, |
900 | 0 | Self: Sized, |
901 | | { |
902 | 0 | <$t as VBRInteger>::read_vbr::<FIELD_SIZE, _>(reader).map(NonZero::new) |
903 | 0 | } Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::VBRInteger>::read_vbr::<_, _> |
904 | | |
905 | | #[inline] |
906 | 0 | fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>( |
907 | 0 | self, |
908 | 0 | writer: &mut W, |
909 | 0 | ) -> io::Result<()> { |
910 | 0 | <$t as VBRInteger>::write_vbr::<FIELD_SIZE, W>( |
911 | 0 | self.map(|n| n.get()).unwrap_or(0), Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::VBRInteger>::write_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::VBRInteger>::write_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::VBRInteger>::write_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::VBRInteger>::write_vbr::<_, _>::{closure#0}Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::VBRInteger>::write_vbr::<_, _>::{closure#0} |
912 | 0 | writer, |
913 | | ) |
914 | 0 | } Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u32>> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u64>> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u128>> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u8>> as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <core::option::Option<core::num::nonzero::NonZero<u16>> as bitstream_io::VBRInteger>::write_vbr::<_, _> |
915 | | } |
916 | | }; |
917 | | } |
918 | | |
919 | | /// This trait extends many common signed integer types |
920 | | /// so that they can be used with the bitstream handling traits. |
921 | | /// |
922 | | /// This trait was formerly named `SignedNumeric` in 2.X.X code. |
923 | | /// If backwards-compatibility is needed one can |
924 | | /// import `SignedInteger` as `SignedNumeric`. |
925 | | pub trait SignedInteger: Numeric { |
926 | | /// The unsigned variant of ourself |
927 | | type Unsigned: UnsignedInteger<Signed = Self>; |
928 | | |
929 | | /// Returns true if this value is negative |
930 | | /// |
931 | | /// # Example |
932 | | /// ``` |
933 | | /// use bitstream_io::SignedInteger; |
934 | | /// assert!(!1i8.is_negative()); |
935 | | /// assert!((-1i8).is_negative()); |
936 | | /// ``` |
937 | | fn is_negative(self) -> bool; |
938 | | |
939 | | /// Returns ourself as a non-negative value. |
940 | | /// The location of the sign bit depends on the stream's endianness |
941 | | /// and is not stored in the result. |
942 | | /// |
943 | | /// # Example |
944 | | /// ``` |
945 | | /// use bitstream_io::SignedInteger; |
946 | | /// assert_eq!(1i8.as_non_negative(), 0b00000001u8); |
947 | | /// ``` |
948 | | fn as_non_negative(self) -> Self::Unsigned; |
949 | | |
950 | | /// Given a negative value and a certain number of bits, |
951 | | /// returns this value as a twos-complement positive number. |
952 | | /// The location of the sign bit depends on the stream's endianness |
953 | | /// and is not stored in the result. |
954 | | /// |
955 | | /// # Example |
956 | | /// ``` |
957 | | /// use bitstream_io::SignedInteger; |
958 | | /// assert_eq!((-1i8).as_negative(8), 0b01111111u8); |
959 | | /// ``` |
960 | | fn as_negative(self, bits: u32) -> Self::Unsigned; |
961 | | |
962 | | /// Given a negative value and a certain number of bits, |
963 | | /// returns this value as a twos-complement positive number. |
964 | | /// |
965 | | /// # Example |
966 | | /// ``` |
967 | | /// use bitstream_io::SignedInteger; |
968 | | /// assert_eq!((-1i8).as_negative_fixed::<8>(), 0b01111111u8); |
969 | | /// ``` |
970 | | fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned; |
971 | | } |
972 | | |
973 | | macro_rules! define_signed_integer { |
974 | | ($t:ty, $u:ty) => { |
975 | | define_numeric!($t); |
976 | | |
977 | | impl SignedInteger for $t { |
978 | | type Unsigned = $u; |
979 | | |
980 | | #[inline(always)] |
981 | 0 | fn is_negative(self) -> bool { |
982 | 0 | self.is_negative() |
983 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::SignedInteger>::is_negative Unexecuted instantiation: <i8 as bitstream_io::SignedInteger>::is_negative Unexecuted instantiation: <i16 as bitstream_io::SignedInteger>::is_negative Unexecuted instantiation: <i64 as bitstream_io::SignedInteger>::is_negative Unexecuted instantiation: <i128 as bitstream_io::SignedInteger>::is_negative |
984 | 0 | fn as_non_negative(self) -> Self::Unsigned { |
985 | 0 | self as $u |
986 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::SignedInteger>::as_non_negative Unexecuted instantiation: <i64 as bitstream_io::SignedInteger>::as_non_negative Unexecuted instantiation: <i128 as bitstream_io::SignedInteger>::as_non_negative Unexecuted instantiation: <i8 as bitstream_io::SignedInteger>::as_non_negative Unexecuted instantiation: <i16 as bitstream_io::SignedInteger>::as_non_negative |
987 | 0 | fn as_negative(self, bits: u32) -> Self::Unsigned { |
988 | 0 | (self - (-1 << (bits - 1))) as $u |
989 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::SignedInteger>::as_negative Unexecuted instantiation: <i64 as bitstream_io::SignedInteger>::as_negative Unexecuted instantiation: <i128 as bitstream_io::SignedInteger>::as_negative Unexecuted instantiation: <i8 as bitstream_io::SignedInteger>::as_negative Unexecuted instantiation: <i16 as bitstream_io::SignedInteger>::as_negative |
990 | 0 | fn as_negative_fixed<const BITS: u32>(self) -> Self::Unsigned { |
991 | 0 | (self - (-1 << (BITS - 1))) as $u |
992 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::SignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <i64 as bitstream_io::SignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <i128 as bitstream_io::SignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <i8 as bitstream_io::SignedInteger>::as_negative_fixed::<_> Unexecuted instantiation: <i16 as bitstream_io::SignedInteger>::as_negative_fixed::<_> |
993 | | } |
994 | | |
995 | | impl Integer for $t { |
996 | | #[inline(always)] |
997 | 0 | fn read<const BITS: u32, R: BitRead + ?Sized>(reader: &mut R) -> io::Result<Self> |
998 | 0 | where |
999 | 0 | Self: Sized, |
1000 | | { |
1001 | 0 | reader.read_signed::<BITS, _>() |
1002 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <i64 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <i128 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <i8 as bitstream_io::Integer>::read::<_, _> Unexecuted instantiation: <i16 as bitstream_io::Integer>::read::<_, _> |
1003 | | |
1004 | | #[inline(always)] |
1005 | 0 | fn read_var<const MAX: u32, R>(reader: &mut R, bits: BitCount<MAX>) -> io::Result<Self> |
1006 | 0 | where |
1007 | 0 | R: BitRead + ?Sized, |
1008 | 0 | Self: Sized, |
1009 | | { |
1010 | 0 | reader.read_signed_counted::<MAX, _>(bits) |
1011 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <i64 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <i128 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <i8 as bitstream_io::Integer>::read_var::<_, _> Unexecuted instantiation: <i16 as bitstream_io::Integer>::read_var::<_, _> |
1012 | | |
1013 | | #[inline(always)] |
1014 | 0 | fn write<const BITS: u32, W: BitWrite + ?Sized>( |
1015 | 0 | self, |
1016 | 0 | writer: &mut W, |
1017 | 0 | ) -> io::Result<()> { |
1018 | 0 | writer.write_signed::<BITS, _>(self) |
1019 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <i64 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <i128 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <i8 as bitstream_io::Integer>::write::<_, _> Unexecuted instantiation: <i16 as bitstream_io::Integer>::write::<_, _> |
1020 | | |
1021 | | #[inline(always)] |
1022 | 0 | fn write_var<const MAX: u32, W: BitWrite + ?Sized>( |
1023 | 0 | self, |
1024 | 0 | writer: &mut W, |
1025 | 0 | bits: BitCount<MAX>, |
1026 | 0 | ) -> io::Result<()> { |
1027 | 0 | writer.write_signed_counted::<MAX, _>(bits, self) |
1028 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::Integer>::write_var::<4294967295, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <i16 as bitstream_io::Integer>::write_var::<4294967295, bitstream_io::write::BitWriter<&mut alloc::vec::Vec<u8>, bitstream_io::BigEndian>> Unexecuted instantiation: <i32 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <i64 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <i128 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <i8 as bitstream_io::Integer>::write_var::<_, _> Unexecuted instantiation: <i16 as bitstream_io::Integer>::write_var::<_, _> |
1029 | | } |
1030 | | |
1031 | | impl VBRInteger for $t { |
1032 | | #[inline(always)] |
1033 | 0 | fn read_vbr<const FIELD_SIZE: u32, R>(reader: &mut R) -> io::Result<Self> |
1034 | 0 | where |
1035 | 0 | R: BitRead + ?Sized, |
1036 | 0 | Self: Sized, |
1037 | | { |
1038 | 0 | reader.read_signed_vbr::<FIELD_SIZE, _>() |
1039 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <i64 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <i128 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <i8 as bitstream_io::VBRInteger>::read_vbr::<_, _> Unexecuted instantiation: <i16 as bitstream_io::VBRInteger>::read_vbr::<_, _> |
1040 | | |
1041 | | #[inline(always)] |
1042 | 0 | fn write_vbr<const FIELD_SIZE: u32, W: BitWrite + ?Sized>( |
1043 | 0 | self, |
1044 | 0 | writer: &mut W, |
1045 | 0 | ) -> io::Result<()> { |
1046 | 0 | writer.write_signed_vbr::<FIELD_SIZE, _>(self) |
1047 | 0 | } Unexecuted instantiation: <i32 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <i64 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <i128 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <i8 as bitstream_io::VBRInteger>::write_vbr::<_, _> Unexecuted instantiation: <i16 as bitstream_io::VBRInteger>::write_vbr::<_, _> |
1048 | | } |
1049 | | }; |
1050 | | } |
1051 | | |
1052 | | define_unsigned_integer!(u8, i8); |
1053 | | define_unsigned_integer!(u16, i16); |
1054 | | define_unsigned_integer!(u32, i32); |
1055 | | define_unsigned_integer!(u64, i64); |
1056 | | define_unsigned_integer!(u128, i128); |
1057 | | |
1058 | | define_signed_integer!(i8, u8); |
1059 | | define_signed_integer!(i16, u16); |
1060 | | define_signed_integer!(i32, u32); |
1061 | | define_signed_integer!(i64, u64); |
1062 | | define_signed_integer!(i128, u128); |
1063 | | |
1064 | | define_primitive_numeric!(f32); |
1065 | | define_primitive_numeric!(f64); |
1066 | | |
1067 | | mod private { |
1068 | | use crate::{ |
1069 | | io, BitCount, BitRead, BitWrite, CheckedSigned, CheckedUnsigned, Primitive, SignedBitCount, |
1070 | | SignedInteger, UnsignedInteger, |
1071 | | }; |
1072 | | |
1073 | | #[test] |
1074 | | fn test_checked_signed() { |
1075 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<8>(), -128i8).is_ok()); |
1076 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<8>(), 127i8).is_ok()); |
1077 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<7>(), -64i8).is_ok()); |
1078 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<7>(), 63i8).is_ok()); |
1079 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<6>(), -32i8).is_ok()); |
1080 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<6>(), 31i8).is_ok()); |
1081 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<5>(), -16i8).is_ok()); |
1082 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<5>(), 15i8).is_ok()); |
1083 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<4>(), -8i8).is_ok()); |
1084 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<4>(), 7i8).is_ok()); |
1085 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<3>(), -4i8).is_ok()); |
1086 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<3>(), 3i8).is_ok()); |
1087 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<2>(), -2i8).is_ok()); |
1088 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<2>(), 1i8).is_ok()); |
1089 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<1>(), -1i8).is_ok()); |
1090 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<1>(), 0i8).is_ok()); |
1091 | | |
1092 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<7>(), -65i8).is_err()); |
1093 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<7>(), 64i8).is_err()); |
1094 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<6>(), -33i8).is_err()); |
1095 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<6>(), 32i8).is_err()); |
1096 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<5>(), -17i8).is_err()); |
1097 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<5>(), 16i8).is_err()); |
1098 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<4>(), -9i8).is_err()); |
1099 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<4>(), 8i8).is_err()); |
1100 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<3>(), -5i8).is_err()); |
1101 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<3>(), 4i8).is_err()); |
1102 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<2>(), -3i8).is_err()); |
1103 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<2>(), 2i8).is_err()); |
1104 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<1>(), -2i8).is_err()); |
1105 | | assert!(CheckedSigned::new(SignedBitCount::<8>::new::<1>(), 1i8).is_err()); |
1106 | | } |
1107 | | |
1108 | | pub trait Endianness: Sized { |
1109 | | /// Pops the next bit from the queue, |
1110 | | /// repleneshing it from the given reader if necessary |
1111 | | fn pop_bit_refill<R>( |
1112 | | reader: &mut R, |
1113 | | queue_value: &mut u8, |
1114 | | queue_bits: &mut u32, |
1115 | | ) -> io::Result<bool> |
1116 | | where |
1117 | | R: io::Read; |
1118 | | |
1119 | | /// Pops the next unary value from the source until |
1120 | | /// `STOP_BIT` is encountered, replenishing it from the given |
1121 | | /// closure if necessary. |
1122 | | /// |
1123 | | /// `STOP_BIT` must be 0 or 1. |
1124 | | fn pop_unary<const STOP_BIT: u8, R>( |
1125 | | reader: &mut R, |
1126 | | queue_value: &mut u8, |
1127 | | queue_bits: &mut u32, |
1128 | | ) -> io::Result<u32> |
1129 | | where |
1130 | | R: io::Read; |
1131 | | |
1132 | | /// Pushes the next bit into the queue, |
1133 | | /// and returns `Some` value if the queue is full. |
1134 | | fn push_bit_flush(queue_value: &mut u8, queue_bits: &mut u32, bit: bool) -> Option<u8>; |
1135 | | |
1136 | | /// For performing bulk reads from a bit source to an output type. |
1137 | | fn read_bits<const MAX: u32, R, U>( |
1138 | | reader: &mut R, |
1139 | | queue_value: &mut u8, |
1140 | | queue_bits: &mut u32, |
1141 | | count: BitCount<MAX>, |
1142 | | ) -> io::Result<U> |
1143 | | where |
1144 | | R: io::Read, |
1145 | | U: UnsignedInteger; |
1146 | | |
1147 | | /// For performing bulk reads from a bit source to an output type. |
1148 | | fn read_bits_fixed<const BITS: u32, R, U>( |
1149 | | reader: &mut R, |
1150 | | queue_value: &mut u8, |
1151 | | queue_bits: &mut u32, |
1152 | | ) -> io::Result<U> |
1153 | | where |
1154 | | R: io::Read, |
1155 | | U: UnsignedInteger; |
1156 | | |
1157 | | /// For performing a checked write to a bit sink |
1158 | | fn write_bits_checked<const MAX: u32, W, U>( |
1159 | | writer: &mut W, |
1160 | | queue_value: &mut u8, |
1161 | | queue_bits: &mut u32, |
1162 | | value: CheckedUnsigned<MAX, U>, |
1163 | | ) -> io::Result<()> |
1164 | | where |
1165 | | W: io::Write, |
1166 | | U: UnsignedInteger; |
1167 | | |
1168 | | /// For performing a checked signed write to a bit sink |
1169 | | fn write_signed_bits_checked<const MAX: u32, W, S>( |
1170 | | writer: &mut W, |
1171 | | queue_value: &mut u8, |
1172 | | queue_bits: &mut u32, |
1173 | | value: CheckedSigned<MAX, S>, |
1174 | | ) -> io::Result<()> |
1175 | | where |
1176 | | W: io::Write, |
1177 | | S: SignedInteger; |
1178 | | |
1179 | | /// Reads signed value from reader in this endianness |
1180 | | fn read_signed_counted<const MAX: u32, R, S>( |
1181 | | r: &mut R, |
1182 | | bits: SignedBitCount<MAX>, |
1183 | | ) -> io::Result<S> |
1184 | | where |
1185 | | R: BitRead, |
1186 | | S: SignedInteger; |
1187 | | |
1188 | | /// Reads whole set of bytes to output buffer |
1189 | | fn read_bytes<const CHUNK_SIZE: usize, R>( |
1190 | | reader: &mut R, |
1191 | | queue_value: &mut u8, |
1192 | | queue_bits: u32, |
1193 | | buf: &mut [u8], |
1194 | | ) -> io::Result<()> |
1195 | | where |
1196 | | R: io::Read; |
1197 | | |
1198 | | /// Writes whole set of bytes to output buffer |
1199 | | fn write_bytes<const CHUNK_SIZE: usize, W>( |
1200 | | writer: &mut W, |
1201 | | queue_value: &mut u8, |
1202 | | queue_bits: u32, |
1203 | | buf: &[u8], |
1204 | | ) -> io::Result<()> |
1205 | | where |
1206 | | W: io::Write; |
1207 | | |
1208 | | /// Converts a primitive's byte buffer to a primitive |
1209 | | fn bytes_to_primitive<P: Primitive>(buf: P::Bytes) -> P; |
1210 | | |
1211 | | /// Converts a primitive to a primitive's byte buffer |
1212 | | fn primitive_to_bytes<P: Primitive>(p: P) -> P::Bytes; |
1213 | | |
1214 | | /// Reads convertable numeric value from reader in this endianness |
1215 | | #[deprecated(since = "4.0.0")] |
1216 | | fn read_primitive<R, V>(r: &mut R) -> io::Result<V> |
1217 | | where |
1218 | | R: BitRead, |
1219 | | V: Primitive; |
1220 | | |
1221 | | /// Writes convertable numeric value to writer in this endianness |
1222 | | #[deprecated(since = "4.0.0")] |
1223 | | fn write_primitive<W, V>(w: &mut W, value: V) -> io::Result<()> |
1224 | | where |
1225 | | W: BitWrite, |
1226 | | V: Primitive; |
1227 | | } |
1228 | | |
1229 | | pub trait Checkable { |
1230 | | fn write_endian<E, W>( |
1231 | | self, |
1232 | | writer: &mut W, |
1233 | | queue_value: &mut u8, |
1234 | | queue_bits: &mut u32, |
1235 | | ) -> io::Result<()> |
1236 | | where |
1237 | | E: Endianness, |
1238 | | W: io::Write; |
1239 | | } |
1240 | | } |
1241 | | |
1242 | | /// A stream's endianness, or byte order, for determining |
1243 | | /// how bits should be read. |
1244 | | /// |
1245 | | /// It comes in `BigEndian` and `LittleEndian` varieties |
1246 | | /// (which may be shortened to `BE` and `LE`) |
1247 | | /// and is not something programmers should implement directly. |
1248 | | pub trait Endianness: private::Endianness {} |
1249 | | |
1250 | | #[inline(always)] |
1251 | 0 | fn read_byte<R>(mut reader: R) -> io::Result<u8> |
1252 | 0 | where |
1253 | 0 | R: io::Read, |
1254 | | { |
1255 | 0 | let mut byte = 0; |
1256 | 0 | reader |
1257 | 0 | .read_exact(core::slice::from_mut(&mut byte)) |
1258 | 0 | .map(|()| byte) |
1259 | 0 | } |
1260 | | |
1261 | | #[inline(always)] |
1262 | 0 | fn write_byte<W>(mut writer: W, byte: u8) -> io::Result<()> |
1263 | 0 | where |
1264 | 0 | W: io::Write, |
1265 | | { |
1266 | 0 | writer.write_all(core::slice::from_ref(&byte)) |
1267 | 0 | } Unexecuted instantiation: bitstream_io::write_byte::<&mut &mut alloc::vec::Vec<u8>> Unexecuted instantiation: bitstream_io::write_byte::<_> |
1268 | | |
1269 | | /// A number of bits to be consumed or written, with a known maximum |
1270 | | /// |
1271 | | /// Although [`BitRead::read`] and [`BitWrite::write`] should be |
1272 | | /// preferred when the number of bits is fixed and known at compile-time - |
1273 | | /// because they can validate the bit count is less than or equal |
1274 | | /// to the type's size in bits at compile-time - |
1275 | | /// there are many instances where bit count is dynamic and |
1276 | | /// determined by the file format itself. |
1277 | | /// But when using [`BitRead::read_var`] or [`BitWrite::write_var`] |
1278 | | /// we must pessimistically assume any number of bits as an argument |
1279 | | /// and validate that the number of bits is not larger than the |
1280 | | /// type being read or written on every call. |
1281 | | /// |
1282 | | /// ``` |
1283 | | /// use bitstream_io::{BitRead, BitReader, BigEndian}; |
1284 | | /// |
1285 | | /// let data: &[u8] = &[0b100_0001_1, 0b111_0110_0]; |
1286 | | /// let mut r = BitReader::endian(data, BigEndian); |
1287 | | /// // our bit count is a 3 bit value |
1288 | | /// let count = r.read::<3, u32>().unwrap(); |
1289 | | /// // that count indicates we need to read 4 bits (0b100) |
1290 | | /// assert_eq!(count, 4); |
1291 | | /// // read the first 4-bit value |
1292 | | /// assert_eq!(r.read_var::<u8>(count).unwrap(), 0b0001); |
1293 | | /// // read the second 4-bit value |
1294 | | /// assert_eq!(r.read_var::<u8>(count).unwrap(), 0b1111); |
1295 | | /// // read the third 4-bit value |
1296 | | /// assert_eq!(r.read_var::<u8>(count).unwrap(), 0b0110); |
1297 | | /// ``` |
1298 | | /// |
1299 | | /// In the preceding example, even though we know `count` is a |
1300 | | /// 3 bit value whose maximum value will never be greater than 7, |
1301 | | /// the subsequent `read_var` calls have no way to know that. |
1302 | | /// They must assume `count` could be 9, or `u32::MAX` or any other `u32` value |
1303 | | /// and validate the count is not larger than the `u8` types we're reading. |
1304 | | /// |
1305 | | /// But we can convert our example to use the `BitCount` type: |
1306 | | /// |
1307 | | /// ``` |
1308 | | /// use bitstream_io::{BitRead, BitReader, BigEndian, BitCount}; |
1309 | | /// |
1310 | | /// let data: &[u8] = &[0b100_0001_1, 0b111_0110_0]; |
1311 | | /// let mut r = BitReader::endian(data, BigEndian); |
1312 | | /// // our bit count is a 3 bit value with a maximum value of 7 |
1313 | | /// let count = r.read_count::<0b111>().unwrap(); |
1314 | | /// // that count indicates we need to read 4 bits (0b100) |
1315 | | /// assert_eq!(count, BitCount::<7>::new::<4>()); |
1316 | | /// // read the first 4-bit value |
1317 | | /// assert_eq!(r.read_counted::<7, u8>(count).unwrap(), 0b0001); |
1318 | | /// // read the second 4-bit value |
1319 | | /// assert_eq!(r.read_counted::<7, u8>(count).unwrap(), 0b1111); |
1320 | | /// // read the third 4-bit value |
1321 | | /// assert_eq!(r.read_counted::<7, u8>(count).unwrap(), 0b0110); |
1322 | | /// ``` |
1323 | | /// |
1324 | | /// Because the [`BitRead::read_counted`] methods know at compile-time |
1325 | | /// that the bit count will be larger than 7, that check can be eliminated |
1326 | | /// simply by taking advantage of information we already know. |
1327 | | /// |
1328 | | /// Leveraging the `BitCount` type also allows us to reason about |
1329 | | /// bit counts in a more formal way, and use checked permutation methods |
1330 | | /// to modify them while ensuring they remain constrained by |
1331 | | /// the file format's requirements. |
1332 | | #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] |
1333 | | pub struct BitCount<const MAX: u32> { |
1334 | | // The amount of bits may be less than or equal to the maximum, |
1335 | | // but never more. |
1336 | | bits: u32, |
1337 | | } |
1338 | | |
1339 | | impl<const MAX: u32> BitCount<MAX> { |
1340 | | /// Builds a bit count from a constant number |
1341 | | /// of bits, which must not be greater than `MAX`. |
1342 | | /// |
1343 | | /// Intended to be used for defining constants. |
1344 | | /// |
1345 | | /// Use `TryFrom` to conditionally build |
1346 | | /// counts from values at runtime. |
1347 | | /// |
1348 | | /// # Examples |
1349 | | /// |
1350 | | /// ``` |
1351 | | /// use bitstream_io::{BitReader, BitRead, BigEndian, BitCount}; |
1352 | | /// let data: &[u8] = &[0b111_00000]; |
1353 | | /// let mut r = BitReader::endian(data, BigEndian); |
1354 | | /// // reading 3 bits from a stream out of a maximum of 8 |
1355 | | /// // doesn't require checking that the bit count is larger |
1356 | | /// // than a u8 at runtime because specifying the maximum of 8 |
1357 | | /// // guarantees our bit count will not be larger than 8 |
1358 | | /// assert_eq!(r.read_counted::<8, u8>(BitCount::new::<3>()).unwrap(), 0b111); |
1359 | | /// ``` |
1360 | | /// |
1361 | | /// ```rust,compile_fail |
1362 | | /// use bitstream_io::BitCount; |
1363 | | /// // trying to build a count of 10 with a maximum of 8 |
1364 | | /// // fails to compile at all |
1365 | | /// let count = BitCount::<8>::new::<10>(); |
1366 | | /// ``` |
1367 | 0 | pub const fn new<const BITS: u32>() -> Self { |
1368 | | const { |
1369 | | assert!(BITS <= MAX, "BITS must be <= MAX"); |
1370 | | } |
1371 | | |
1372 | 0 | Self { bits: BITS } |
1373 | 0 | } Unexecuted instantiation: <bitstream_io::BitCount<16>>::new::<16> Unexecuted instantiation: <bitstream_io::BitCount<16>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<1>>::new::<1> Unexecuted instantiation: <bitstream_io::BitCount<1>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<32>>::new::<32> Unexecuted instantiation: <bitstream_io::BitCount<32>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<2>>::new::<2> Unexecuted instantiation: <bitstream_io::BitCount<2>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<3>>::new::<3> Unexecuted instantiation: <bitstream_io::BitCount<3>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<4>>::new::<4> Unexecuted instantiation: <bitstream_io::BitCount<4>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<5>>::new::<5> Unexecuted instantiation: <bitstream_io::BitCount<5>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<6>>::new::<6> Unexecuted instantiation: <bitstream_io::BitCount<6>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<7>>::new::<7> Unexecuted instantiation: <bitstream_io::BitCount<7>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<8>>::new::<8> Unexecuted instantiation: <bitstream_io::BitCount<8>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<9>>::new::<9> Unexecuted instantiation: <bitstream_io::BitCount<9>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<12>>::new::<12> Unexecuted instantiation: <bitstream_io::BitCount<12>>::new::<0> Unexecuted instantiation: <bitstream_io::BitCount<_>>::new::<_> |
1374 | | |
1375 | | /// Add a number of bits to our count, |
1376 | | /// returning a new count with a new maximum. |
1377 | | /// |
1378 | | /// Returns `None` if the new count goes above our new maximum. |
1379 | | /// |
1380 | | /// # Examples |
1381 | | /// |
1382 | | /// ``` |
1383 | | /// use bitstream_io::BitCount; |
1384 | | /// |
1385 | | /// let count = BitCount::<2>::new::<1>(); |
1386 | | /// // adding 2 to 1 and increasing the max to 3 yields a new count of 3 |
1387 | | /// assert_eq!(count.checked_add::<3>(2), Some(BitCount::<3>::new::<3>())); |
1388 | | /// // adding 2 to 1 without increasing the max yields None |
1389 | | /// assert_eq!(count.checked_add::<2>(2), None); |
1390 | | /// ``` |
1391 | | #[inline] |
1392 | 0 | pub const fn checked_add<const NEW_MAX: u32>(self, bits: u32) -> Option<BitCount<NEW_MAX>> { |
1393 | 0 | match self.bits.checked_add(bits) { |
1394 | 0 | Some(bits) if bits <= NEW_MAX => Some(BitCount { bits }), |
1395 | 0 | _ => None, |
1396 | | } |
1397 | 0 | } |
1398 | | |
1399 | | /// Subtracts a number of bits from our count, |
1400 | | /// returning a new count with a new maximum. |
1401 | | /// |
1402 | | /// Returns `None` if the new count goes below 0 |
1403 | | /// or below our new maximum. |
1404 | | /// |
1405 | | /// # Example |
1406 | | /// ``` |
1407 | | /// use bitstream_io::BitCount; |
1408 | | /// let count = BitCount::<5>::new::<5>(); |
1409 | | /// // subtracting 1 from 5 yields a new count of 4 |
1410 | | /// assert_eq!(count.checked_sub::<5>(1), Some(BitCount::<5>::new::<4>())); |
1411 | | /// // subtracting 6 from 5 yields None |
1412 | | /// assert!(count.checked_sub::<5>(6).is_none()); |
1413 | | /// // subtracting 1 with a new maximum of 3 also yields None |
1414 | | /// // because 4 is larger than the maximum of 3 |
1415 | | /// assert!(count.checked_sub::<3>(1).is_none()); |
1416 | | /// ``` |
1417 | | #[inline] |
1418 | 0 | pub const fn checked_sub<const NEW_MAX: u32>(self, bits: u32) -> Option<BitCount<NEW_MAX>> { |
1419 | 0 | match self.bits.checked_sub(bits) { |
1420 | 0 | Some(bits) if bits <= NEW_MAX => Some(BitCount { bits }), |
1421 | 0 | _ => None, |
1422 | | } |
1423 | 0 | } |
1424 | | |
1425 | | /// Attempt to convert our count to a count with a new |
1426 | | /// bit count and new maximum. |
1427 | | /// |
1428 | | /// Returns `Some(count)` if the updated number of bits |
1429 | | /// is less than or equal to the new maximum. |
1430 | | /// Returns `None` if not. |
1431 | | /// |
1432 | | /// # Examples |
1433 | | /// ``` |
1434 | | /// use bitstream_io::BitCount; |
1435 | | /// |
1436 | | /// let count = BitCount::<5>::new::<5>(); |
1437 | | /// // muliplying 5 bits by 2 with a new max of 10 is ok |
1438 | | /// assert_eq!( |
1439 | | /// count.try_map::<10, _>(|i| i.checked_mul(2)), |
1440 | | /// Some(BitCount::<10>::new::<10>()), |
1441 | | /// ); |
1442 | | /// |
1443 | | /// // multiplying 5 bits by 3 with a new max of 10 overflows |
1444 | | /// assert_eq!(count.try_map::<10, _>(|i| i.checked_mul(3)), None); |
1445 | | /// ``` |
1446 | | #[inline] |
1447 | 0 | pub fn try_map<const NEW_MAX: u32, F>(self, f: F) -> Option<BitCount<NEW_MAX>> |
1448 | 0 | where |
1449 | 0 | F: FnOnce(u32) -> Option<u32>, |
1450 | | { |
1451 | 0 | f(self.bits) |
1452 | 0 | .filter(|bits| *bits <= NEW_MAX) |
1453 | 0 | .map(|bits| BitCount { bits }) |
1454 | 0 | } |
1455 | | |
1456 | | /// Returns our maximum bit count |
1457 | | /// |
1458 | | /// # Example |
1459 | | /// ``` |
1460 | | /// use bitstream_io::BitCount; |
1461 | | /// |
1462 | | /// let count = BitCount::<10>::new::<5>(); |
1463 | | /// assert_eq!(count.max(), 10); |
1464 | | /// ``` |
1465 | | #[inline(always)] |
1466 | 0 | pub const fn max(&self) -> u32 { |
1467 | 0 | MAX |
1468 | 0 | } |
1469 | | |
1470 | | /// Returns signed count if our bit count is greater than 0 |
1471 | | /// |
1472 | | /// # Example |
1473 | | /// |
1474 | | /// ``` |
1475 | | /// use bitstream_io::{BitCount, SignedBitCount}; |
1476 | | /// |
1477 | | /// let count = BitCount::<10>::new::<5>(); |
1478 | | /// assert_eq!(count.signed_count(), Some(SignedBitCount::<10>::new::<5>())); |
1479 | | /// |
1480 | | /// let count = BitCount::<10>::new::<0>(); |
1481 | | /// assert_eq!(count.signed_count(), None); |
1482 | | /// ``` |
1483 | | #[inline(always)] |
1484 | 0 | pub const fn signed_count(&self) -> Option<SignedBitCount<MAX>> { |
1485 | 0 | match self.bits.checked_sub(1) { |
1486 | 0 | Some(bits) => Some(SignedBitCount { |
1487 | 0 | bits: *self, |
1488 | 0 | unsigned: BitCount { bits }, |
1489 | 0 | }), |
1490 | 0 | None => None, |
1491 | | } |
1492 | 0 | } Unexecuted instantiation: <bitstream_io::BitCount<4294967295>>::signed_count Unexecuted instantiation: <bitstream_io::BitCount<_>>::signed_count |
1493 | | |
1494 | | /// Masks off the least-significant bits for the given type |
1495 | | /// |
1496 | | /// Returns closure that takes a value and returns a |
1497 | | /// pair of the most-significant and least-significant |
1498 | | /// bits. Because the least-significant bits cannot |
1499 | | /// be larger than this bit count, that value is |
1500 | | /// returned as a [`Checked`] type. |
1501 | | /// |
1502 | | /// # Examples |
1503 | | /// |
1504 | | /// ``` |
1505 | | /// use bitstream_io::BitCount; |
1506 | | /// |
1507 | | /// // create a bit count of 3 |
1508 | | /// let count = BitCount::<8>::new::<3>(); |
1509 | | /// |
1510 | | /// // create a mask suitable for u8 types |
1511 | | /// let mask = count.mask_lsb::<u8>(); |
1512 | | /// |
1513 | | /// let (msb, lsb) = mask(0b11011_110); |
1514 | | /// assert_eq!(msb, 0b11011); // the most-significant bits |
1515 | | /// assert_eq!(lsb.into_value(), 0b110); // the least-significant bits |
1516 | | /// |
1517 | | /// let (msb, lsb) = mask(0b01100_010); |
1518 | | /// assert_eq!(msb, 0b01100); // the most-significant bits |
1519 | | /// assert_eq!(lsb.into_value(), 0b010); // the least-significant bits |
1520 | | /// |
1521 | | /// let (msb, lsb) = mask(0b00000_111); |
1522 | | /// assert_eq!(msb, 0b00000); // the most-significant bits |
1523 | | /// assert_eq!(lsb.into_value(), 0b111); // the least-significant bits |
1524 | | /// ``` |
1525 | | /// |
1526 | | /// ``` |
1527 | | /// use bitstream_io::BitCount; |
1528 | | /// |
1529 | | /// // a mask with a bit count of 0 puts everything in msb |
1530 | | /// let mask = BitCount::<8>::new::<0>().mask_lsb::<u8>(); |
1531 | | /// |
1532 | | /// let (msb, lsb) = mask(0b11111111); |
1533 | | /// assert_eq!(msb, 0b11111111); |
1534 | | /// assert_eq!(lsb.into_value(), 0); |
1535 | | /// |
1536 | | /// // a mask with a bit count larger than the type |
1537 | | /// // is restricted to that type's size, if possible |
1538 | | /// let mask = BitCount::<16>::new::<9>().mask_lsb::<u8>(); |
1539 | | /// |
1540 | | /// let (msb, lsb) = mask(0b11111111); |
1541 | | /// assert_eq!(msb, 0); |
1542 | | /// assert_eq!(lsb.into_value(), 0b11111111); |
1543 | | /// ``` |
1544 | 0 | pub fn mask_lsb<U: UnsignedInteger>(self) -> impl Fn(U) -> (U, CheckedUnsigned<MAX, U>) { |
1545 | | use core::convert::TryFrom; |
1546 | | |
1547 | 0 | let (mask, shift, count) = match U::BITS_SIZE.checked_sub(self.bits) { |
1548 | 0 | Some(mask_bits) => (U::ALL.shr_default(mask_bits), self.bits, self), |
1549 | 0 | None => ( |
1550 | 0 | U::ALL, |
1551 | 0 | U::BITS_SIZE, |
1552 | 0 | BitCount::try_from(U::BITS_SIZE).expect("bit count too small for type"), |
1553 | 0 | ), |
1554 | | }; |
1555 | | |
1556 | 0 | move |v| { |
1557 | 0 | ( |
1558 | 0 | v.shr_default(shift), |
1559 | 0 | Checked { |
1560 | 0 | value: v & mask, |
1561 | 0 | count, |
1562 | 0 | }, |
1563 | 0 | ) |
1564 | 0 | } |
1565 | 0 | } |
1566 | | |
1567 | | /// Returns this bit count's range for the given unsigned type |
1568 | | /// |
1569 | | /// # Example |
1570 | | /// |
1571 | | /// ``` |
1572 | | /// use bitstream_io::BitCount; |
1573 | | /// |
1574 | | /// assert_eq!(BitCount::<9>::new::<0>().range::<u8>(), 0..=0); |
1575 | | /// assert_eq!(BitCount::<9>::new::<1>().range::<u8>(), 0..=0b1); |
1576 | | /// assert_eq!(BitCount::<9>::new::<2>().range::<u8>(), 0..=0b11); |
1577 | | /// assert_eq!(BitCount::<9>::new::<3>().range::<u8>(), 0..=0b111); |
1578 | | /// assert_eq!(BitCount::<9>::new::<4>().range::<u8>(), 0..=0b1111); |
1579 | | /// assert_eq!(BitCount::<9>::new::<5>().range::<u8>(), 0..=0b11111); |
1580 | | /// assert_eq!(BitCount::<9>::new::<6>().range::<u8>(), 0..=0b111111); |
1581 | | /// assert_eq!(BitCount::<9>::new::<7>().range::<u8>(), 0..=0b1111111); |
1582 | | /// assert_eq!(BitCount::<9>::new::<8>().range::<u8>(), 0..=0b11111111); |
1583 | | /// // a count that exceeds the type's size is |
1584 | | /// // naturally restricted to that type's maximum range |
1585 | | /// assert_eq!(BitCount::<9>::new::<9>().range::<u8>(), 0..=0b11111111); |
1586 | | /// ``` |
1587 | | #[inline] |
1588 | 0 | pub fn range<U: UnsignedInteger>(&self) -> core::ops::RangeInclusive<U> { |
1589 | 0 | match U::ONE.checked_shl(self.bits) { |
1590 | 0 | Some(top) => U::ZERO..=(top - U::ONE), |
1591 | 0 | None => U::ZERO..=U::ALL, |
1592 | | } |
1593 | 0 | } |
1594 | | |
1595 | | /// Returns minimum value between ourself and bit count |
1596 | | /// |
1597 | | /// # Example |
1598 | | /// |
1599 | | /// ``` |
1600 | | /// use bitstream_io::BitCount; |
1601 | | /// |
1602 | | /// let count = BitCount::<8>::new::<7>(); |
1603 | | /// assert_eq!(count.min(6), BitCount::new::<6>()); |
1604 | | /// assert_eq!(count.min(8), BitCount::new::<7>()); |
1605 | | /// ``` |
1606 | | #[inline(always)] |
1607 | 0 | pub fn min(self, bits: u32) -> Self { |
1608 | | // the minimum of ourself and another bit count |
1609 | | // can never exceed our maximum bit count |
1610 | 0 | Self { |
1611 | 0 | bits: self.bits.min(bits), |
1612 | 0 | } |
1613 | 0 | } |
1614 | | |
1615 | | /// Returns the minimum value of an unsigned int in this bit count |
1616 | | /// |
1617 | | /// # Example |
1618 | | /// |
1619 | | /// ``` |
1620 | | /// use bitstream_io::BitCount; |
1621 | | /// |
1622 | | /// assert_eq!(BitCount::<8>::new::<0>().none::<u8>().into_value(), 0b0); |
1623 | | /// assert_eq!(BitCount::<8>::new::<1>().none::<u8>().into_value(), 0b0); |
1624 | | /// assert_eq!(BitCount::<8>::new::<2>().none::<u8>().into_value(), 0b00); |
1625 | | /// assert_eq!(BitCount::<8>::new::<3>().none::<u8>().into_value(), 0b000); |
1626 | | /// assert_eq!(BitCount::<8>::new::<4>().none::<u8>().into_value(), 0b0000); |
1627 | | /// assert_eq!(BitCount::<8>::new::<5>().none::<u8>().into_value(), 0b00000); |
1628 | | /// assert_eq!(BitCount::<8>::new::<6>().none::<u8>().into_value(), 0b000000); |
1629 | | /// assert_eq!(BitCount::<8>::new::<7>().none::<u8>().into_value(), 0b0000000); |
1630 | | /// assert_eq!(BitCount::<8>::new::<8>().none::<u8>().into_value(), 0b00000000); |
1631 | | /// ``` |
1632 | | #[inline(always)] |
1633 | 0 | pub fn none<U: UnsignedInteger>(self) -> CheckedUnsigned<MAX, U> { |
1634 | 0 | CheckedUnsigned { |
1635 | 0 | value: U::ZERO, |
1636 | 0 | count: self, |
1637 | 0 | } |
1638 | 0 | } |
1639 | | |
1640 | | /// Returns the maximum value of an unsigned int in this bit count |
1641 | | /// |
1642 | | /// # Example |
1643 | | /// |
1644 | | /// ``` |
1645 | | /// use bitstream_io::BitCount; |
1646 | | /// |
1647 | | /// assert_eq!(BitCount::<8>::new::<0>().all::<u8>().into_value(), 0b0); |
1648 | | /// assert_eq!(BitCount::<8>::new::<1>().all::<u8>().into_value(), 0b1); |
1649 | | /// assert_eq!(BitCount::<8>::new::<2>().all::<u8>().into_value(), 0b11); |
1650 | | /// assert_eq!(BitCount::<8>::new::<3>().all::<u8>().into_value(), 0b111); |
1651 | | /// assert_eq!(BitCount::<8>::new::<4>().all::<u8>().into_value(), 0b1111); |
1652 | | /// assert_eq!(BitCount::<8>::new::<5>().all::<u8>().into_value(), 0b11111); |
1653 | | /// assert_eq!(BitCount::<8>::new::<6>().all::<u8>().into_value(), 0b111111); |
1654 | | /// assert_eq!(BitCount::<8>::new::<7>().all::<u8>().into_value(), 0b1111111); |
1655 | | /// assert_eq!(BitCount::<8>::new::<8>().all::<u8>().into_value(), 0b11111111); |
1656 | | /// ``` |
1657 | | #[inline(always)] |
1658 | 0 | pub fn all<U: UnsignedInteger>(self) -> CheckedUnsigned<MAX, U> { |
1659 | | CheckedUnsigned { |
1660 | 0 | value: match U::ONE.checked_shl(self.bits) { |
1661 | 0 | Some(top) => top - U::ONE, |
1662 | 0 | None => U::ALL, |
1663 | | }, |
1664 | 0 | count: self, |
1665 | | } |
1666 | 0 | } |
1667 | | } |
1668 | | |
1669 | | impl<const MAX: u32> core::convert::TryFrom<u32> for BitCount<MAX> { |
1670 | | type Error = u32; |
1671 | | |
1672 | | /// Attempts to convert a `u32` bit count to a `BitCount` |
1673 | | /// |
1674 | | /// Attempting a bit maximum bit count larger than the |
1675 | | /// largest supported type is a compile-time error |
1676 | | /// |
1677 | | /// # Examples |
1678 | | /// ``` |
1679 | | /// use bitstream_io::BitCount; |
1680 | | /// use std::convert::TryInto; |
1681 | | /// |
1682 | | /// assert_eq!(8u32.try_into(), Ok(BitCount::<8>::new::<8>())); |
1683 | | /// assert_eq!(9u32.try_into(), Err::<BitCount<8>, _>(9)); |
1684 | | /// ``` |
1685 | 0 | fn try_from(bits: u32) -> Result<Self, Self::Error> { |
1686 | 0 | (bits <= MAX).then_some(Self { bits }).ok_or(bits) |
1687 | 0 | } |
1688 | | } |
1689 | | |
1690 | | impl<const MAX: u32> core::fmt::Display for BitCount<MAX> { |
1691 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
1692 | 0 | core::fmt::Display::fmt(&self.bits, f) |
1693 | 0 | } |
1694 | | } |
1695 | | |
1696 | | impl BitCount<{ u32::MAX }> { |
1697 | | /// Builds a bit count where the maximum bits is unknown. |
1698 | | /// |
1699 | | /// # Example |
1700 | | /// ``` |
1701 | | /// use bitstream_io::BitCount; |
1702 | | /// assert_eq!(BitCount::unknown(5), BitCount::<{ u32::MAX }>::new::<5>()); |
1703 | | /// ``` |
1704 | 0 | pub const fn unknown(bits: u32) -> Self { |
1705 | 0 | Self { bits } |
1706 | 0 | } |
1707 | | } |
1708 | | |
1709 | | #[test] |
1710 | | fn test_unknown_bitcount() { |
1711 | | let count = BitCount::unknown(u32::MAX); |
1712 | | assert!(u32::from(count) <= count.max()); |
1713 | | } |
1714 | | |
1715 | | impl<const MAX: u32> From<BitCount<MAX>> for u32 { |
1716 | | #[inline(always)] |
1717 | 0 | fn from(BitCount { bits }: BitCount<MAX>) -> u32 { |
1718 | 0 | bits |
1719 | 0 | } |
1720 | | } |
1721 | | |
1722 | | /// A number of bits to be read or written for signed integers, with a known maximum |
1723 | | /// |
1724 | | /// This is closely related to the [`BitCount`] type, but further constrained |
1725 | | /// to have a minimum value of 1 - because signed values require at least |
1726 | | /// 1 bit for the sign. |
1727 | | /// |
1728 | | /// Let's start with a basic example: |
1729 | | /// |
1730 | | /// ``` |
1731 | | /// use bitstream_io::{BitRead, BitReader, BigEndian}; |
1732 | | /// |
1733 | | /// let data: &[u8] = &[0b100_0001_1, 0b111_0110_0]; |
1734 | | /// let mut r = BitReader::endian(data, BigEndian); |
1735 | | /// // our bit count is a 3 bit value |
1736 | | /// let count = r.read::<3, u32>().unwrap(); |
1737 | | /// // that count indicates we need to read 4 bits (0b100) |
1738 | | /// assert_eq!(count, 4); |
1739 | | /// // read the first 4-bit signed value |
1740 | | /// assert_eq!(r.read_var::<i8>(count).unwrap(), 1); |
1741 | | /// // read the second 4-bit signed value |
1742 | | /// assert_eq!(r.read_var::<i8>(count).unwrap(), -1); |
1743 | | /// // read the third 4-bit signed value |
1744 | | /// assert_eq!(r.read_var::<i8>(count).unwrap(), 6); |
1745 | | /// ``` |
1746 | | /// |
1747 | | /// In the preceding example, even though we know `count` is a |
1748 | | /// 3 bit value whose maximum value will never be greater than 7, |
1749 | | /// the subsequent `read_var` calls have no way to know that. |
1750 | | /// They must assume `count` could be 9, or `u32::MAX` or any other `u32` value |
1751 | | /// and validate the count is not larger than the `i8` types we're reading |
1752 | | /// while also greater than 0 because `i8` requires a sign bit. |
1753 | | /// |
1754 | | /// But we can convert our example to use the `SignedBitCount` type: |
1755 | | /// ``` |
1756 | | /// use bitstream_io::{BitRead, BitReader, BigEndian, SignedBitCount}; |
1757 | | /// |
1758 | | /// let data: &[u8] = &[0b100_0001_1, 0b111_0110_0]; |
1759 | | /// let mut r = BitReader::endian(data, BigEndian); |
1760 | | /// // our bit count is a 3 bit value with a maximum value of 7 |
1761 | | /// let count = r.read_count::<0b111>().unwrap(); |
1762 | | /// // convert that count to a signed bit count, |
1763 | | /// // which guarantees its value is greater than 0 |
1764 | | /// let count = count.signed_count().unwrap(); |
1765 | | /// // that count indicates we need to read 4 bits (0b100) |
1766 | | /// assert_eq!(count, SignedBitCount::<7>::new::<4>()); |
1767 | | /// // read the first 4-bit value |
1768 | | /// assert_eq!(r.read_signed_counted::<7, i8>(count).unwrap(), 1); |
1769 | | /// // read the second 4-bit value |
1770 | | /// assert_eq!(r.read_signed_counted::<7, i8>(count).unwrap(), -1); |
1771 | | /// // read the third 4-bit value |
1772 | | /// assert_eq!(r.read_signed_counted::<7, i8>(count).unwrap(), 6); |
1773 | | /// ``` |
1774 | | /// |
1775 | | /// Because the [`BitRead::read_signed_counted`] methods know at compile-time |
1776 | | /// that the bit count will be larger than 7, that check can be eliminated |
1777 | | /// simply by taking advantage of information we already know. |
1778 | | /// |
1779 | | /// Leveraging the `SignedBitCount` type also allows us to reason about |
1780 | | /// bit counts in a more formal way, and use checked permutation methods |
1781 | | /// to modify them while ensuring they remain constrained by |
1782 | | /// the file format's requirements. |
1783 | | #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] |
1784 | | pub struct SignedBitCount<const MAX: u32> { |
1785 | | // the whole original bit count |
1786 | | bits: BitCount<MAX>, |
1787 | | // a bit count with one bit removed for the sign |
1788 | | unsigned: BitCount<MAX>, |
1789 | | } |
1790 | | |
1791 | | impl<const MAX: u32> SignedBitCount<MAX> { |
1792 | | /// Builds a signed bit count from a constant number |
1793 | | /// of bits, which must be greater than 0 and |
1794 | | /// not be greater than `MAX`. |
1795 | | /// |
1796 | | /// Intended to be used for defining constants. |
1797 | | /// |
1798 | | /// Use `TryFrom` to conditionally build |
1799 | | /// counts from values at runtime. |
1800 | | /// |
1801 | | /// # Examples |
1802 | | /// |
1803 | | /// ``` |
1804 | | /// use bitstream_io::{BitReader, BitRead, BigEndian, SignedBitCount}; |
1805 | | /// let data: &[u8] = &[0b111_00000]; |
1806 | | /// let mut r = BitReader::endian(data, BigEndian); |
1807 | | /// // reading 3 bits from a stream out of a maximum of 8 |
1808 | | /// // doesn't require checking that the bit count is larger |
1809 | | /// // than a u8 at runtime because specifying the maximum of 8 |
1810 | | /// // guarantees our bit count will not be larger than 8 |
1811 | | /// assert_eq!(r.read_signed_counted::<8, i8>(SignedBitCount::new::<3>()).unwrap(), -1); |
1812 | | /// ``` |
1813 | | /// |
1814 | | /// ```rust,compile_fail |
1815 | | /// use bitstream_io::SignedBitCount; |
1816 | | /// // trying to build a count of 10 with a maximum of 8 |
1817 | | /// // fails to compile at all |
1818 | | /// let count = SignedBitCount::<8>::new::<10>(); |
1819 | | /// ``` |
1820 | | /// |
1821 | | /// ```rust,compile_fail |
1822 | | /// use bitstream_io::SignedBitCount; |
1823 | | /// // trying to build a count of 0 also fails to compile |
1824 | | /// let count = SignedBitCount::<8>::new::<0>(); |
1825 | | /// ``` |
1826 | 0 | pub const fn new<const BITS: u32>() -> Self { |
1827 | | const { |
1828 | | assert!(BITS > 0, "BITS must be > 0"); |
1829 | | } |
1830 | | |
1831 | 0 | Self { |
1832 | 0 | bits: BitCount::new::<BITS>(), |
1833 | 0 | unsigned: BitCount { bits: BITS - 1 }, |
1834 | 0 | } |
1835 | 0 | } Unexecuted instantiation: <bitstream_io::SignedBitCount<7>>::new::<7> Unexecuted instantiation: <bitstream_io::SignedBitCount<_>>::new::<_> |
1836 | | |
1837 | | /// Add a number of bits to our count, |
1838 | | /// returning a new count with a new maximum. |
1839 | | /// |
1840 | | /// Returns `None` if the new count goes above our new maximum. |
1841 | | /// |
1842 | | /// # Examples |
1843 | | /// |
1844 | | /// ``` |
1845 | | /// use bitstream_io::SignedBitCount; |
1846 | | /// |
1847 | | /// let count = SignedBitCount::<2>::new::<1>(); |
1848 | | /// // adding 2 to 1 and increasing the max to 3 yields a new count of 3 |
1849 | | /// assert_eq!(count.checked_add::<3>(2), Some(SignedBitCount::<3>::new::<3>())); |
1850 | | /// // adding 2 to 1 without increasing the max yields None |
1851 | | /// assert_eq!(count.checked_add::<2>(2), None); |
1852 | | /// ``` |
1853 | | #[inline] |
1854 | 0 | pub const fn checked_add<const NEW_MAX: u32>( |
1855 | 0 | self, |
1856 | 0 | bits: u32, |
1857 | 0 | ) -> Option<SignedBitCount<NEW_MAX>> { |
1858 | 0 | match self.bits.checked_add(bits) { |
1859 | 0 | Some(bits_new) => match self.unsigned.checked_add(bits) { |
1860 | 0 | Some(unsigned) => Some(SignedBitCount { |
1861 | 0 | bits: bits_new, |
1862 | 0 | unsigned, |
1863 | 0 | }), |
1864 | 0 | None => None, |
1865 | | }, |
1866 | 0 | None => None, |
1867 | | } |
1868 | 0 | } |
1869 | | |
1870 | | /// Subtracts a number of bits from our count, |
1871 | | /// returning a new count with a new maximum. |
1872 | | /// |
1873 | | /// Returns `None` if the new count goes below 1 |
1874 | | /// or below our new maximum. |
1875 | | /// |
1876 | | /// # Example |
1877 | | /// ``` |
1878 | | /// use bitstream_io::SignedBitCount; |
1879 | | /// let count = SignedBitCount::<5>::new::<5>(); |
1880 | | /// // subtracting 1 from 5 yields a new count of 4 |
1881 | | /// assert_eq!(count.checked_sub::<5>(1), Some(SignedBitCount::<5>::new::<4>())); |
1882 | | /// // subtracting 6 from 5 yields None |
1883 | | /// assert!(count.checked_sub::<5>(6).is_none()); |
1884 | | /// // subtracting 1 with a new maximum of 3 also yields None |
1885 | | /// // because 4 is larger than the maximum of 3 |
1886 | | /// assert!(count.checked_sub::<3>(1).is_none()); |
1887 | | /// // subtracting 5 from 5 also yields None |
1888 | | /// // because SignedBitCount always requires 1 bit for the sign |
1889 | | /// assert!(count.checked_sub::<5>(5).is_none()); |
1890 | | /// ``` |
1891 | | #[inline] |
1892 | 0 | pub const fn checked_sub<const NEW_MAX: u32>( |
1893 | 0 | self, |
1894 | 0 | bits: u32, |
1895 | 0 | ) -> Option<SignedBitCount<NEW_MAX>> { |
1896 | 0 | match self.bits.checked_sub(bits) { |
1897 | 0 | Some(bits_new) => match self.unsigned.checked_sub(bits) { |
1898 | 0 | Some(unsigned) => Some(SignedBitCount { |
1899 | 0 | bits: bits_new, |
1900 | 0 | unsigned, |
1901 | 0 | }), |
1902 | 0 | None => None, |
1903 | | }, |
1904 | 0 | None => None, |
1905 | | } |
1906 | 0 | } |
1907 | | |
1908 | | /// Attempt to convert our count to a count with a new |
1909 | | /// bit count and new maximum. |
1910 | | /// |
1911 | | /// Returns `Some(count)` if the updated number of bits |
1912 | | /// is less than or equal to the new maximum |
1913 | | /// and greater than 0. |
1914 | | /// Returns `None` if not. |
1915 | | /// |
1916 | | /// # Examples |
1917 | | /// ``` |
1918 | | /// use bitstream_io::SignedBitCount; |
1919 | | /// |
1920 | | /// let count = SignedBitCount::<5>::new::<5>(); |
1921 | | /// // muliplying 5 bits by 2 with a new max of 10 is ok |
1922 | | /// assert_eq!( |
1923 | | /// count.try_map::<10, _>(|i| i.checked_mul(2)), |
1924 | | /// Some(SignedBitCount::<10>::new::<10>()), |
1925 | | /// ); |
1926 | | /// |
1927 | | /// // multiplying 5 bits by 3 with a new max of 10 overflows |
1928 | | /// assert_eq!(count.try_map::<10, _>(|i| i.checked_mul(3)), None); |
1929 | | /// |
1930 | | /// // multiplying 5 bits by 0 results in 0 bits, |
1931 | | /// // which isn't value for a SignedBitCount |
1932 | | /// assert_eq!(count.try_map::<10, _>(|i| Some(i * 0)), None); |
1933 | | /// ``` |
1934 | | #[inline] |
1935 | 0 | pub fn try_map<const NEW_MAX: u32, F>(self, f: F) -> Option<SignedBitCount<NEW_MAX>> |
1936 | 0 | where |
1937 | 0 | F: FnOnce(u32) -> Option<u32>, |
1938 | | { |
1939 | 0 | self.bits.try_map(f).and_then(|b| b.signed_count()) |
1940 | 0 | } |
1941 | | |
1942 | | /// Returns our maximum bit count |
1943 | | /// |
1944 | | /// # Example |
1945 | | /// ``` |
1946 | | /// use bitstream_io::SignedBitCount; |
1947 | | /// |
1948 | | /// let count = SignedBitCount::<10>::new::<5>(); |
1949 | | /// assert_eq!(count.max(), 10); |
1950 | | /// ``` |
1951 | | #[inline(always)] |
1952 | 0 | pub const fn max(&self) -> u32 { |
1953 | 0 | MAX |
1954 | 0 | } |
1955 | | |
1956 | | /// Returns regular unsigned bit count |
1957 | | /// |
1958 | | /// # Example |
1959 | | /// |
1960 | | /// ``` |
1961 | | /// use bitstream_io::{BitCount, SignedBitCount}; |
1962 | | /// |
1963 | | /// let signed_count = SignedBitCount::<10>::new::<5>(); |
1964 | | /// assert_eq!(signed_count.count(), BitCount::<10>::new::<5>()); |
1965 | | /// ``` |
1966 | | #[inline(always)] |
1967 | 0 | pub const fn count(&self) -> BitCount<MAX> { |
1968 | 0 | self.bits |
1969 | 0 | } |
1970 | | |
1971 | | /// Returns this bit count's range for the given signed type |
1972 | | /// |
1973 | | /// # Example |
1974 | | /// |
1975 | | /// ``` |
1976 | | /// use bitstream_io::SignedBitCount; |
1977 | | /// |
1978 | | /// assert_eq!(SignedBitCount::<9>::new::<1>().range::<i8>(), -1..=0); |
1979 | | /// assert_eq!(SignedBitCount::<9>::new::<2>().range::<i8>(), -2..=1); |
1980 | | /// assert_eq!(SignedBitCount::<9>::new::<3>().range::<i8>(), -4..=3); |
1981 | | /// assert_eq!(SignedBitCount::<9>::new::<4>().range::<i8>(), -8..=7); |
1982 | | /// assert_eq!(SignedBitCount::<9>::new::<5>().range::<i8>(), -16..=15); |
1983 | | /// assert_eq!(SignedBitCount::<9>::new::<6>().range::<i8>(), -32..=31); |
1984 | | /// assert_eq!(SignedBitCount::<9>::new::<7>().range::<i8>(), -64..=63); |
1985 | | /// assert_eq!(SignedBitCount::<9>::new::<8>().range::<i8>(), -128..=127); |
1986 | | /// // a count that exceeds the type's size is |
1987 | | /// // naturally restricted to that type's maximum range |
1988 | | /// assert_eq!(SignedBitCount::<9>::new::<9>().range::<i8>(), -128..=127); |
1989 | | /// ``` |
1990 | 0 | pub fn range<S: SignedInteger>(&self) -> core::ops::RangeInclusive<S> { |
1991 | | // a bit of a hack to get around the somewhat restrictive |
1992 | | // SignedInteger trait I've created for myself |
1993 | | |
1994 | 0 | if self.bits.bits < S::BITS_SIZE { |
1995 | 0 | (!S::ZERO << self.unsigned.bits)..=((S::ONE << self.unsigned.bits) - S::ONE) |
1996 | | } else { |
1997 | 0 | S::Unsigned::ZERO.as_negative(S::BITS_SIZE)..=(S::Unsigned::ALL >> 1).as_non_negative() |
1998 | | } |
1999 | 0 | } |
2000 | | } |
2001 | | |
2002 | | impl<const MAX: u32> core::fmt::Display for SignedBitCount<MAX> { |
2003 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
2004 | 0 | core::fmt::Display::fmt(&self.bits, f) |
2005 | 0 | } |
2006 | | } |
2007 | | |
2008 | | impl<const MAX: u32> core::convert::TryFrom<BitCount<MAX>> for SignedBitCount<MAX> { |
2009 | | type Error = (); |
2010 | | |
2011 | | #[inline] |
2012 | 0 | fn try_from(count: BitCount<MAX>) -> Result<Self, Self::Error> { |
2013 | 0 | count.signed_count().ok_or(()) |
2014 | 0 | } Unexecuted instantiation: <bitstream_io::SignedBitCount<4294967295> as core::convert::TryFrom<bitstream_io::BitCount<4294967295>>>::try_from Unexecuted instantiation: <bitstream_io::SignedBitCount<_> as core::convert::TryFrom<bitstream_io::BitCount<_>>>::try_from |
2015 | | } |
2016 | | |
2017 | | impl<const MAX: u32> core::convert::TryFrom<u32> for SignedBitCount<MAX> { |
2018 | | type Error = u32; |
2019 | | |
2020 | | #[inline] |
2021 | 0 | fn try_from(count: u32) -> Result<Self, Self::Error> { |
2022 | 0 | BitCount::<MAX>::try_from(count).and_then(|b| b.signed_count().ok_or(count)) |
2023 | 0 | } |
2024 | | } |
2025 | | |
2026 | | impl<const MAX: u32> From<SignedBitCount<MAX>> for u32 { |
2027 | | #[inline(always)] |
2028 | 0 | fn from( |
2029 | 0 | SignedBitCount { |
2030 | 0 | bits: BitCount { bits }, |
2031 | 0 | .. |
2032 | 0 | }: SignedBitCount<MAX>, |
2033 | 0 | ) -> u32 { |
2034 | 0 | bits |
2035 | 0 | } |
2036 | | } |
2037 | | |
2038 | | /// An error when converting a value to a [`Checked`] struct |
2039 | | #[derive(Copy, Clone, Debug)] |
2040 | | pub enum CheckedError { |
2041 | | /// Excessive bits for type |
2042 | | ExcessiveBits, |
2043 | | /// Excessive value for bits |
2044 | | ExcessiveValue, |
2045 | | } |
2046 | | |
2047 | | impl core::error::Error for CheckedError {} |
2048 | | |
2049 | | impl core::fmt::Display for CheckedError { |
2050 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
2051 | 0 | match self { |
2052 | 0 | Self::ExcessiveBits => core::fmt::Display::fmt("excessive bits for type written", f), |
2053 | 0 | Self::ExcessiveValue => core::fmt::Display::fmt("excessive value for bits written", f), |
2054 | | } |
2055 | 0 | } |
2056 | | } |
2057 | | |
2058 | | impl From<CheckedError> for io::Error { |
2059 | | #[inline] |
2060 | 0 | fn from(error: CheckedError) -> Self { |
2061 | 0 | match error { |
2062 | 0 | CheckedError::ExcessiveBits => io::Error::new( |
2063 | 0 | io::ErrorKind::InvalidInput, |
2064 | | "excessive bits for type written", |
2065 | | ), |
2066 | 0 | CheckedError::ExcessiveValue => io::Error::new( |
2067 | 0 | io::ErrorKind::InvalidInput, |
2068 | | "excessive value for bits written", |
2069 | | ), |
2070 | | } |
2071 | 0 | } Unexecuted instantiation: <std::io::error::Error as core::convert::From<bitstream_io::CheckedError>>::from Unexecuted instantiation: <std::io::error::Error as core::convert::From<bitstream_io::CheckedError>>::from |
2072 | | } |
2073 | | |
2074 | | /// A type for eliminating redundant validation when writing |
2075 | | /// |
2076 | | /// Normally, when writing a value, not only must the number of bits |
2077 | | /// must be checked against the type being written |
2078 | | /// (e.g. writing 9 bits from a `u8` is always an error), |
2079 | | /// but the value must also be checked against the number of bits |
2080 | | /// (e.g. writing a value of 2 in 1 bit is always an error). |
2081 | | /// |
2082 | | /// But when the value's range can be checked in advance, |
2083 | | /// the write-time check can be skipped through the use |
2084 | | /// of the [`BitWrite::write_checked`] method. |
2085 | | #[derive(Copy, Clone, Debug)] |
2086 | | pub struct Checked<C, T> { |
2087 | | count: C, |
2088 | | value: T, |
2089 | | } |
2090 | | |
2091 | | impl<C, T> Checked<C, T> { |
2092 | | /// Returns our bit count and value |
2093 | | #[inline] |
2094 | 0 | pub fn into_count_value(self) -> (C, T) { |
2095 | 0 | (self.count, self.value) |
2096 | 0 | } Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<7>, i8>>::into_count_value Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<4294967295>, i32>>::into_count_value Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<4294967295>, i16>>::into_count_value Unexecuted instantiation: <bitstream_io::Checked<_, _>>::into_count_value |
2097 | | |
2098 | | /// Returns our value |
2099 | | #[inline] |
2100 | 0 | pub fn into_value(self) -> T { |
2101 | 0 | self.value |
2102 | 0 | } |
2103 | | } |
2104 | | |
2105 | | impl<C, T> AsRef<T> for Checked<C, T> { |
2106 | 0 | fn as_ref(&self) -> &T { |
2107 | 0 | &self.value |
2108 | 0 | } |
2109 | | } |
2110 | | |
2111 | | /// An unsigned type with a verified value |
2112 | | pub type CheckedUnsigned<const MAX: u32, T> = Checked<BitCount<MAX>, T>; |
2113 | | |
2114 | | impl<const MAX: u32, U: UnsignedInteger> Checkable for CheckedUnsigned<MAX, U> { |
2115 | | #[inline] |
2116 | 0 | fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> io::Result<()> { |
2117 | | // a naive default implementation |
2118 | 0 | writer.write_unsigned_counted(self.count, self.value) |
2119 | 0 | } |
2120 | | |
2121 | | #[inline] |
2122 | 0 | fn written_bits(&self) -> u32 { |
2123 | 0 | self.count.bits |
2124 | 0 | } |
2125 | | } |
2126 | | |
2127 | | impl<const MAX: u32, U: UnsignedInteger> CheckablePrimitive for CheckedUnsigned<MAX, U> { |
2128 | | type CountType = BitCount<MAX>; |
2129 | | |
2130 | | #[inline] |
2131 | 0 | fn read<R: BitRead + ?Sized>(reader: &mut R, count: Self::CountType) -> io::Result<Self> { |
2132 | 0 | reader |
2133 | 0 | .read_unsigned_counted(count) |
2134 | 0 | .map(|value| Self { value, count }) |
2135 | 0 | } |
2136 | | } |
2137 | | |
2138 | | impl<const MAX: u32, U: UnsignedInteger> private::Checkable for CheckedUnsigned<MAX, U> { |
2139 | 0 | fn write_endian<E, W>( |
2140 | 0 | self, |
2141 | 0 | writer: &mut W, |
2142 | 0 | queue_value: &mut u8, |
2143 | 0 | queue_bits: &mut u32, |
2144 | 0 | ) -> io::Result<()> |
2145 | 0 | where |
2146 | 0 | E: private::Endianness, |
2147 | 0 | W: io::Write, |
2148 | | { |
2149 | 0 | E::write_bits_checked(writer, queue_value, queue_bits, self) |
2150 | 0 | } |
2151 | | } |
2152 | | |
2153 | | impl<const MAX: u32, U: UnsignedInteger> CheckedUnsigned<MAX, U> { |
2154 | | /// Returns our value if it fits in the given number of bits |
2155 | | /// |
2156 | | /// # Example |
2157 | | /// |
2158 | | /// ``` |
2159 | | /// use bitstream_io::{BitCount, CheckedUnsigned, CheckedError}; |
2160 | | /// |
2161 | | /// // a value of 7 fits into a 3 bit count |
2162 | | /// assert!(CheckedUnsigned::<8, u8>::new(3, 0b111).is_ok()); |
2163 | | /// |
2164 | | /// // a value of 8 does not fit into a 3 bit count |
2165 | | /// assert!(matches!( |
2166 | | /// CheckedUnsigned::<8, u8>::new(3, 0b1000), |
2167 | | /// Err(CheckedError::ExcessiveValue), |
2168 | | /// )); |
2169 | | /// |
2170 | | /// // a bit count of 9 is too large for u8 |
2171 | | /// assert!(matches!( |
2172 | | /// CheckedUnsigned::<9, _>::new(9, 1u8), |
2173 | | /// Err(CheckedError::ExcessiveBits), |
2174 | | /// )); |
2175 | | /// ``` |
2176 | | #[inline] |
2177 | 0 | pub fn new(count: impl TryInto<BitCount<MAX>>, value: U) -> Result<Self, CheckedError> { |
2178 | 0 | let count @ BitCount { bits } = |
2179 | 0 | count.try_into().map_err(|_| CheckedError::ExcessiveBits)?; |
2180 | | |
2181 | 0 | if MAX <= U::BITS_SIZE || bits <= U::BITS_SIZE { |
2182 | 0 | if bits == 0 { |
2183 | 0 | Ok(Self { |
2184 | 0 | count, |
2185 | 0 | value: U::ZERO, |
2186 | 0 | }) |
2187 | 0 | } else if value <= U::ALL >> (U::BITS_SIZE - bits) { |
2188 | 0 | Ok(Self { count, value }) |
2189 | | } else { |
2190 | 0 | Err(CheckedError::ExcessiveValue) |
2191 | | } |
2192 | | } else { |
2193 | 0 | Err(CheckedError::ExcessiveBits) |
2194 | | } |
2195 | 0 | } Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<4294967295>, u8>>::new::<bitstream_io::BitCount<4294967295>> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<4294967295>, u32>>::new::<bitstream_io::BitCount<4294967295>> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<4294967295>, u16>>::new::<bitstream_io::BitCount<4294967295>> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<4294967295>, u64>>::new::<bitstream_io::BitCount<4294967295>> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<_>, _>>::new::<_> |
2196 | | |
2197 | | /// Returns our value if it fits in the given number of const bits |
2198 | | /// |
2199 | | /// # Examples |
2200 | | /// |
2201 | | /// ``` |
2202 | | /// use bitstream_io::{CheckedUnsigned, CheckedError}; |
2203 | | /// |
2204 | | /// // a value of 7 fits into a 3 bit count |
2205 | | /// assert!(CheckedUnsigned::<8, u8>::new_fixed::<3>(0b111).is_ok()); |
2206 | | /// |
2207 | | /// // a value of 8 does not fit into a 3 bit count |
2208 | | /// assert!(matches!( |
2209 | | /// CheckedUnsigned::<8, u8>::new_fixed::<3>(0b1000), |
2210 | | /// Err(CheckedError::ExcessiveValue), |
2211 | | /// )); |
2212 | | /// ``` |
2213 | | /// |
2214 | | /// ```compile_fail |
2215 | | /// use bitstream_io::{BitCount, CheckedUnsigned}; |
2216 | | /// |
2217 | | /// // a bit count of 9 is too large for u8 |
2218 | | /// |
2219 | | /// // because this is checked at compile-time, |
2220 | | /// // it does not compile at all |
2221 | | /// let c = CheckedUnsigned::<16, u8>::new_fixed::<9>(1); |
2222 | | /// ``` |
2223 | 0 | pub fn new_fixed<const BITS: u32>(value: U) -> Result<Self, CheckedError> { |
2224 | | const { |
2225 | | assert!(BITS <= U::BITS_SIZE, "excessive bits for type written"); |
2226 | | } |
2227 | | |
2228 | 0 | if BITS == 0 { |
2229 | 0 | Ok(Self { |
2230 | 0 | count: BitCount::new::<0>(), |
2231 | 0 | value: U::ZERO, |
2232 | 0 | }) |
2233 | 0 | } else if BITS == U::BITS_SIZE || value <= (U::ALL >> (U::BITS_SIZE - BITS)) { |
2234 | 0 | Ok(Self { |
2235 | 0 | // whether BITS is larger than MAX is checked here |
2236 | 0 | count: BitCount::new::<BITS>(), |
2237 | 0 | value, |
2238 | 0 | }) |
2239 | | } else { |
2240 | 0 | Err(CheckedError::ExcessiveValue) |
2241 | | } |
2242 | 0 | } Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<16>, u32>>::new_fixed::<16> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<16>, u16>>::new_fixed::<16> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<1>, u8>>::new_fixed::<1> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<1>, u16>>::new_fixed::<1> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<32>, u32>>::new_fixed::<32> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<32>, u64>>::new_fixed::<32> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<2>, u8>>::new_fixed::<2> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<2>, u32>>::new_fixed::<2> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<3>, u8>>::new_fixed::<3> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<3>, u32>>::new_fixed::<3> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<4>, u8>>::new_fixed::<4> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<4>, u32>>::new_fixed::<4> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<5>, u8>>::new_fixed::<5> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<6>, u8>>::new_fixed::<6> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<7>, u8>>::new_fixed::<7> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<8>, u8>>::new_fixed::<8> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<9>, u16>>::new_fixed::<9> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<12>, u16>>::new_fixed::<12> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::BitCount<_>, _>>::new_fixed::<_> |
2243 | | } |
2244 | | |
2245 | | /// A fixed number of bits to be consumed or written |
2246 | | /// |
2247 | | /// Analagous to [`BitCount`], this is a zero-sized type |
2248 | | /// whose value is fixed at compile-time and cannot be changed. |
2249 | | /// |
2250 | | /// # Example |
2251 | | /// |
2252 | | /// ``` |
2253 | | /// use bitstream_io::{ |
2254 | | /// BigEndian, BitRead, BitReader, BitWrite, BitWriter, CheckedUnsignedFixed, FixedBitCount, |
2255 | | /// }; |
2256 | | /// |
2257 | | /// type FourBits = CheckedUnsignedFixed<4, u8>; |
2258 | | /// |
2259 | | /// let input: &[u8] = &[0b0001_1111, 0b0110_1001]; |
2260 | | /// let mut r = BitReader::endian(input, BigEndian); |
2261 | | /// |
2262 | | /// // read 4, 4-bit values |
2263 | | /// let v1 = r.read_checked::<FourBits>(FixedBitCount).unwrap(); |
2264 | | /// let v2 = r.read_checked::<FourBits>(FixedBitCount).unwrap(); |
2265 | | /// let v3 = r.read_checked::<FourBits>(FixedBitCount).unwrap(); |
2266 | | /// let v4 = r.read_checked::<FourBits>(FixedBitCount).unwrap(); |
2267 | | /// |
2268 | | /// assert_eq!(v1.into_value(), 0b0001); |
2269 | | /// assert_eq!(v2.into_value(), 0b1111); |
2270 | | /// assert_eq!(v3.into_value(), 0b0110); |
2271 | | /// assert_eq!(v4.into_value(), 0b1001); |
2272 | | /// |
2273 | | /// // write those same values back to disk |
2274 | | /// let mut w = BitWriter::endian(vec![], BigEndian); |
2275 | | /// w.write_checked(v1).unwrap(); |
2276 | | /// w.write_checked(v2).unwrap(); |
2277 | | /// w.write_checked(v3).unwrap(); |
2278 | | /// w.write_checked(v4).unwrap(); |
2279 | | /// |
2280 | | /// // ensure they're the same |
2281 | | /// assert_eq!(w.into_writer().as_slice(), input); |
2282 | | /// ``` |
2283 | | #[derive(Copy, Clone, Debug)] |
2284 | | pub struct FixedBitCount<const BITS: u32>; |
2285 | | |
2286 | | impl<const BITS: u32> From<FixedBitCount<BITS>> for BitCount<BITS> { |
2287 | 0 | fn from(_count: FixedBitCount<BITS>) -> Self { |
2288 | 0 | BitCount::new::<BITS>() |
2289 | 0 | } |
2290 | | } |
2291 | | |
2292 | | impl<const BITS: u32, const MAX: u32> core::convert::TryFrom<BitCount<MAX>> |
2293 | | for FixedBitCount<BITS> |
2294 | | { |
2295 | | type Error = BitCount<MAX>; |
2296 | | |
2297 | 0 | fn try_from(count: BitCount<MAX>) -> Result<Self, Self::Error> { |
2298 | 0 | (count.bits == BITS).then_some(FixedBitCount).ok_or(count) |
2299 | 0 | } |
2300 | | } |
2301 | | |
2302 | | /// An unsigned type with a verified value for a fixed number of bits |
2303 | | pub type CheckedUnsignedFixed<const BITS: u32, T> = Checked<FixedBitCount<BITS>, T>; |
2304 | | |
2305 | | impl<const BITS: u32, U: UnsignedInteger> CheckedUnsignedFixed<BITS, U> { |
2306 | | /// Returns our checked value if it fits in the given number of const bits |
2307 | | /// |
2308 | | /// # Examples |
2309 | | /// |
2310 | | /// ``` |
2311 | | /// use bitstream_io::{CheckedUnsignedFixed, CheckedError}; |
2312 | | /// |
2313 | | /// // a value of 7 fits into a maximum of 3 bits |
2314 | | /// assert!(CheckedUnsignedFixed::<3, u8>::new_fixed(0b111).is_ok()); |
2315 | | /// |
2316 | | /// // a value of 8 does not fit into a maximum of 3 bits |
2317 | | /// assert!(matches!( |
2318 | | /// CheckedUnsignedFixed::<3, u8>::new_fixed(0b1000), |
2319 | | /// Err(CheckedError::ExcessiveValue), |
2320 | | /// )); |
2321 | | /// ``` |
2322 | | /// |
2323 | | /// ```compile_fail |
2324 | | /// use bitstream_io::CheckedUnsignedFixed; |
2325 | | /// |
2326 | | /// // a bit count of 9 is too large for u8 |
2327 | | /// |
2328 | | /// // because this is checked at compile-time, |
2329 | | /// // it does not compile at all |
2330 | | /// let c = CheckedUnsignedFixed::<9, u8>::new_fixed(1); |
2331 | | /// ``` |
2332 | 0 | pub fn new_fixed(value: U) -> Result<Self, CheckedError> { |
2333 | | const { |
2334 | | assert!(BITS <= U::BITS_SIZE, "excessive bits for type written"); |
2335 | | } |
2336 | | |
2337 | 0 | if BITS == 0 { |
2338 | 0 | Ok(Self { |
2339 | 0 | count: FixedBitCount, |
2340 | 0 | value: U::ZERO, |
2341 | 0 | }) |
2342 | 0 | } else if BITS == U::BITS_SIZE || value <= (U::ALL >> (U::BITS_SIZE - BITS)) { |
2343 | 0 | Ok(Self { |
2344 | 0 | count: FixedBitCount, |
2345 | 0 | value, |
2346 | 0 | }) |
2347 | | } else { |
2348 | 0 | Err(CheckedError::ExcessiveValue) |
2349 | | } |
2350 | 0 | } |
2351 | | } |
2352 | | |
2353 | | impl<const BITS: u32, U: UnsignedInteger> Checkable for CheckedUnsignedFixed<BITS, U> { |
2354 | | #[inline] |
2355 | 0 | fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> io::Result<()> { |
2356 | | // a naive default implementation |
2357 | 0 | writer.write_unsigned::<BITS, _>(self.value) |
2358 | 0 | } |
2359 | | |
2360 | | #[inline] |
2361 | 0 | fn written_bits(&self) -> u32 { |
2362 | 0 | BITS |
2363 | 0 | } |
2364 | | } |
2365 | | |
2366 | | impl<const BITS: u32, U: UnsignedInteger> private::Checkable for CheckedUnsignedFixed<BITS, U> { |
2367 | 0 | fn write_endian<E, W>( |
2368 | 0 | self, |
2369 | 0 | writer: &mut W, |
2370 | 0 | queue_value: &mut u8, |
2371 | 0 | queue_bits: &mut u32, |
2372 | 0 | ) -> io::Result<()> |
2373 | 0 | where |
2374 | 0 | E: private::Endianness, |
2375 | 0 | W: io::Write, |
2376 | | { |
2377 | 0 | E::write_bits_checked( |
2378 | 0 | writer, |
2379 | 0 | queue_value, |
2380 | 0 | queue_bits, |
2381 | 0 | Checked { |
2382 | 0 | value: self.value, |
2383 | 0 | count: self.count.into(), |
2384 | 0 | }, |
2385 | | ) |
2386 | 0 | } |
2387 | | } |
2388 | | |
2389 | | impl<const BITS: u32, U: UnsignedInteger> CheckablePrimitive for CheckedUnsignedFixed<BITS, U> { |
2390 | | type CountType = FixedBitCount<BITS>; |
2391 | | |
2392 | 0 | fn read<R: BitRead + ?Sized>(reader: &mut R, count: FixedBitCount<BITS>) -> io::Result<Self> { |
2393 | | Ok(Self { |
2394 | 0 | value: reader.read_unsigned::<BITS, _>()?, |
2395 | 0 | count, |
2396 | | }) |
2397 | 0 | } |
2398 | | } |
2399 | | |
2400 | | /// A signed type with a verified value |
2401 | | pub type CheckedSigned<const MAX: u32, T> = Checked<SignedBitCount<MAX>, T>; |
2402 | | |
2403 | | impl<const MAX: u32, S: SignedInteger> Checkable for CheckedSigned<MAX, S> { |
2404 | | #[inline] |
2405 | 0 | fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> io::Result<()> { |
2406 | | // a naive default implementation |
2407 | 0 | writer.write_signed_counted(self.count, self.value) |
2408 | 0 | } |
2409 | | |
2410 | | #[inline] |
2411 | 0 | fn written_bits(&self) -> u32 { |
2412 | 0 | self.count.bits.into() |
2413 | 0 | } |
2414 | | } |
2415 | | |
2416 | | impl<const MAX: u32, S: SignedInteger> CheckablePrimitive for CheckedSigned<MAX, S> { |
2417 | | type CountType = SignedBitCount<MAX>; |
2418 | | |
2419 | | #[inline] |
2420 | 0 | fn read<R: BitRead + ?Sized>(reader: &mut R, count: Self::CountType) -> io::Result<Self> { |
2421 | 0 | reader |
2422 | 0 | .read_signed_counted(count) |
2423 | 0 | .map(|value| Self { value, count }) |
2424 | 0 | } |
2425 | | } |
2426 | | |
2427 | | impl<const MAX: u32, S: SignedInteger> private::Checkable for CheckedSigned<MAX, S> { |
2428 | | #[inline] |
2429 | 0 | fn write_endian<E, W>( |
2430 | 0 | self, |
2431 | 0 | writer: &mut W, |
2432 | 0 | queue_value: &mut u8, |
2433 | 0 | queue_bits: &mut u32, |
2434 | 0 | ) -> io::Result<()> |
2435 | 0 | where |
2436 | 0 | E: private::Endianness, |
2437 | 0 | W: io::Write, |
2438 | | { |
2439 | 0 | E::write_signed_bits_checked(writer, queue_value, queue_bits, self) |
2440 | 0 | } |
2441 | | } |
2442 | | |
2443 | | impl<const MAX: u32, S: SignedInteger> CheckedSigned<MAX, S> { |
2444 | | /// Returns our value if it fits in the given number of bits |
2445 | | /// |
2446 | | /// # Example |
2447 | | /// |
2448 | | /// ``` |
2449 | | /// use bitstream_io::{SignedBitCount, CheckedSigned, CheckedError}; |
2450 | | /// |
2451 | | /// // a value of 3 fits into a 3 bit count |
2452 | | /// assert!(CheckedSigned::<8, _>::new(3, 3i8).is_ok()); |
2453 | | /// |
2454 | | /// // a value of 4 does not fit into a 3 bit count |
2455 | | /// assert!(matches!( |
2456 | | /// CheckedSigned::<8, _>::new(3, 4i8), |
2457 | | /// Err(CheckedError::ExcessiveValue), |
2458 | | /// )); |
2459 | | /// |
2460 | | /// // a bit count of 9 is too large for i8 |
2461 | | /// assert!(matches!( |
2462 | | /// CheckedSigned::<9, _>::new(9, 1i8), |
2463 | | /// Err(CheckedError::ExcessiveBits), |
2464 | | /// )); |
2465 | | /// ``` |
2466 | | #[inline] |
2467 | 0 | pub fn new(count: impl TryInto<SignedBitCount<MAX>>, value: S) -> Result<Self, CheckedError> { |
2468 | 0 | let count @ SignedBitCount { |
2469 | 0 | bits: BitCount { bits }, |
2470 | | unsigned: BitCount { |
2471 | 0 | bits: unsigned_bits, |
2472 | | }, |
2473 | 0 | } = count.try_into().map_err(|_| CheckedError::ExcessiveBits)?; |
2474 | | |
2475 | 0 | if MAX <= S::BITS_SIZE || bits <= S::BITS_SIZE { |
2476 | 0 | if bits == S::BITS_SIZE |
2477 | 0 | || (((S::ZERO - S::ONE) << unsigned_bits) <= value |
2478 | 0 | && value < (S::ONE << unsigned_bits)) |
2479 | | { |
2480 | 0 | Ok(Self { count, value }) |
2481 | | } else { |
2482 | 0 | Err(CheckedError::ExcessiveValue) |
2483 | | } |
2484 | | } else { |
2485 | 0 | Err(CheckedError::ExcessiveBits) |
2486 | | } |
2487 | 0 | } Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<4294967295>, i32>>::new::<bitstream_io::SignedBitCount<4294967295>> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<4294967295>, i16>>::new::<bitstream_io::SignedBitCount<4294967295>> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<_>, _>>::new::<_> |
2488 | | |
2489 | | /// Returns our value if it fits in the given number of const bits |
2490 | | /// |
2491 | | /// # Examples |
2492 | | /// |
2493 | | /// ``` |
2494 | | /// use bitstream_io::{CheckedSigned, CheckedError}; |
2495 | | /// |
2496 | | /// // a value of 3 fits into a 3 bit count |
2497 | | /// assert!(CheckedSigned::<8, i8>::new_fixed::<3>(3).is_ok()); |
2498 | | /// |
2499 | | /// // a value of 4 does not fit into a 3 bit count |
2500 | | /// assert!(matches!( |
2501 | | /// CheckedSigned::<8, i8>::new_fixed::<3>(4), |
2502 | | /// Err(CheckedError::ExcessiveValue), |
2503 | | /// )); |
2504 | | /// ``` |
2505 | | /// |
2506 | | /// ```compile_fail |
2507 | | /// use bitstream_io::{BitCount, CheckedSigned}; |
2508 | | /// |
2509 | | /// // a bit count of 9 is too large for i8 |
2510 | | /// |
2511 | | /// // because this is checked at compile-time, |
2512 | | /// // it does not compile at all |
2513 | | /// let c = CheckedSigned::<16, i8>::new_fixed::<9>(1); |
2514 | | /// ``` |
2515 | 0 | pub fn new_fixed<const BITS: u32>(value: S) -> Result<Self, CheckedError> { |
2516 | | const { |
2517 | | assert!(BITS <= S::BITS_SIZE, "excessive bits for type written"); |
2518 | | } |
2519 | | |
2520 | 0 | if BITS == S::BITS_SIZE |
2521 | 0 | || (((S::ZERO - S::ONE) << (BITS - 1)) <= value && value < (S::ONE << (BITS - 1))) |
2522 | | { |
2523 | 0 | Ok(Self { |
2524 | 0 | count: SignedBitCount::new::<BITS>(), |
2525 | 0 | value, |
2526 | 0 | }) |
2527 | | } else { |
2528 | 0 | Err(CheckedError::ExcessiveValue) |
2529 | | } |
2530 | 0 | } Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<7>, i8>>::new_fixed::<7> Unexecuted instantiation: <bitstream_io::Checked<bitstream_io::SignedBitCount<_>, _>>::new_fixed::<_> |
2531 | | } |
2532 | | |
2533 | | /// A fixed number of bits to be consumed or written |
2534 | | /// |
2535 | | /// Analagous to [`SignedBitCount`], this is a zero-sized type |
2536 | | /// whose value is fixed at compile-time and cannot be changed. |
2537 | | /// |
2538 | | /// # Example |
2539 | | /// |
2540 | | /// ``` |
2541 | | /// use bitstream_io::{ |
2542 | | /// BigEndian, BitRead, BitReader, BitWrite, BitWriter, |
2543 | | /// CheckedSignedFixed, FixedSignedBitCount, |
2544 | | /// }; |
2545 | | /// |
2546 | | /// type FourBits = CheckedSignedFixed<4, i8>; |
2547 | | /// |
2548 | | /// let input: &[u8] = &[0b0001_1111, 0b0110_1001]; |
2549 | | /// let mut r = BitReader::endian(input, BigEndian); |
2550 | | /// |
2551 | | /// // read 4, 4-bit values |
2552 | | /// let v1 = r.read_checked::<FourBits>(FixedSignedBitCount).unwrap(); |
2553 | | /// let v2 = r.read_checked::<FourBits>(FixedSignedBitCount).unwrap(); |
2554 | | /// let v3 = r.read_checked::<FourBits>(FixedSignedBitCount).unwrap(); |
2555 | | /// let v4 = r.read_checked::<FourBits>(FixedSignedBitCount).unwrap(); |
2556 | | /// |
2557 | | /// assert_eq!(v1.into_value(), 1); |
2558 | | /// assert_eq!(v2.into_value(), -1); |
2559 | | /// assert_eq!(v3.into_value(), 6); |
2560 | | /// assert_eq!(v4.into_value(), -7); |
2561 | | /// |
2562 | | /// // write those same values back to disk |
2563 | | /// let mut w = BitWriter::endian(vec![], BigEndian); |
2564 | | /// w.write_checked(v1).unwrap(); |
2565 | | /// w.write_checked(v2).unwrap(); |
2566 | | /// w.write_checked(v3).unwrap(); |
2567 | | /// w.write_checked(v4).unwrap(); |
2568 | | /// |
2569 | | /// // ensure they're the same |
2570 | | /// assert_eq!(w.into_writer().as_slice(), input); |
2571 | | /// ``` |
2572 | | #[derive(Copy, Clone, Debug)] |
2573 | | pub struct FixedSignedBitCount<const BITS: u32>; |
2574 | | |
2575 | | impl<const BITS: u32> From<FixedSignedBitCount<BITS>> for SignedBitCount<BITS> { |
2576 | 0 | fn from(_count: FixedSignedBitCount<BITS>) -> Self { |
2577 | 0 | SignedBitCount::new::<BITS>() |
2578 | 0 | } |
2579 | | } |
2580 | | |
2581 | | impl<const BITS: u32, const MAX: u32> core::convert::TryFrom<SignedBitCount<MAX>> |
2582 | | for FixedSignedBitCount<BITS> |
2583 | | { |
2584 | | type Error = SignedBitCount<MAX>; |
2585 | | |
2586 | 0 | fn try_from(count: SignedBitCount<MAX>) -> Result<Self, Self::Error> { |
2587 | 0 | (count.bits.bits == BITS) |
2588 | 0 | .then_some(FixedSignedBitCount) |
2589 | 0 | .ok_or(count) |
2590 | 0 | } |
2591 | | } |
2592 | | |
2593 | | /// A signed type with a verified value for a fixed number of bits |
2594 | | pub type CheckedSignedFixed<const BITS: u32, T> = Checked<FixedSignedBitCount<BITS>, T>; |
2595 | | |
2596 | | impl<const BITS: u32, S: SignedInteger> CheckedSignedFixed<BITS, S> { |
2597 | | /// Returns our checked value if it fits in the given number of const bits |
2598 | | /// |
2599 | | /// # Examples |
2600 | | /// |
2601 | | /// ``` |
2602 | | /// use bitstream_io::{SignedBitCount, CheckedSignedFixed, CheckedError}; |
2603 | | /// |
2604 | | /// // a value of 3 fits into a 3 bit count |
2605 | | /// assert!(CheckedSignedFixed::<3, _>::new_fixed(3i8).is_ok()); |
2606 | | /// |
2607 | | /// // a value of 4 does not fit into a 3 bit count |
2608 | | /// assert!(matches!( |
2609 | | /// CheckedSignedFixed::<3, _>::new_fixed(4i8), |
2610 | | /// Err(CheckedError::ExcessiveValue), |
2611 | | /// )); |
2612 | | /// ``` |
2613 | | /// |
2614 | | /// ```compile_fail |
2615 | | /// use bitstream_io::CheckedSignedFixed; |
2616 | | /// |
2617 | | /// // a bit count of 9 is too large for i8 |
2618 | | /// |
2619 | | /// // because this is checked at compile-time, |
2620 | | /// // it does not compile at all |
2621 | | /// let c = CheckedSignedFixed::<9, _>::new_fixed(1i8); |
2622 | | /// ``` |
2623 | 0 | pub fn new_fixed(value: S) -> Result<Self, CheckedError> { |
2624 | | const { |
2625 | | assert!(BITS <= S::BITS_SIZE, "excessive bits for type written"); |
2626 | | } |
2627 | | |
2628 | 0 | if BITS == S::BITS_SIZE |
2629 | 0 | || (((S::ZERO - S::ONE) << (BITS - 1)) <= value && value < (S::ONE << (BITS - 1))) |
2630 | | { |
2631 | 0 | Ok(Self { |
2632 | 0 | count: FixedSignedBitCount, |
2633 | 0 | value, |
2634 | 0 | }) |
2635 | | } else { |
2636 | 0 | Err(CheckedError::ExcessiveValue) |
2637 | | } |
2638 | 0 | } |
2639 | | } |
2640 | | impl<const BITS: u32, S: SignedInteger> Checkable for CheckedSignedFixed<BITS, S> { |
2641 | | #[inline] |
2642 | 0 | fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> io::Result<()> { |
2643 | | // a naive default implementation |
2644 | 0 | writer.write_signed::<BITS, _>(self.value) |
2645 | 0 | } |
2646 | | |
2647 | | #[inline] |
2648 | 0 | fn written_bits(&self) -> u32 { |
2649 | 0 | BITS |
2650 | 0 | } |
2651 | | } |
2652 | | |
2653 | | impl<const BITS: u32, S: SignedInteger> private::Checkable for CheckedSignedFixed<BITS, S> { |
2654 | | #[inline] |
2655 | 0 | fn write_endian<E, W>( |
2656 | 0 | self, |
2657 | 0 | writer: &mut W, |
2658 | 0 | queue_value: &mut u8, |
2659 | 0 | queue_bits: &mut u32, |
2660 | 0 | ) -> io::Result<()> |
2661 | 0 | where |
2662 | 0 | E: private::Endianness, |
2663 | 0 | W: io::Write, |
2664 | | { |
2665 | 0 | E::write_signed_bits_checked( |
2666 | 0 | writer, |
2667 | 0 | queue_value, |
2668 | 0 | queue_bits, |
2669 | 0 | CheckedSigned { |
2670 | 0 | value: self.value, |
2671 | 0 | count: self.count.into(), |
2672 | 0 | }, |
2673 | | ) |
2674 | 0 | } |
2675 | | } |
2676 | | |
2677 | | impl<const BITS: u32, S: SignedInteger> CheckablePrimitive for CheckedSignedFixed<BITS, S> { |
2678 | | type CountType = FixedSignedBitCount<BITS>; |
2679 | | |
2680 | 0 | fn read<R: BitRead + ?Sized>( |
2681 | 0 | reader: &mut R, |
2682 | 0 | count: FixedSignedBitCount<BITS>, |
2683 | 0 | ) -> io::Result<Self> { |
2684 | | Ok(Self { |
2685 | 0 | value: reader.read_signed::<BITS, _>()?, |
2686 | 0 | count, |
2687 | | }) |
2688 | 0 | } |
2689 | | } |
2690 | | |
2691 | | /// A trait for writable types whose values can be validated |
2692 | | /// |
2693 | | /// Ordinarily, when writing a value to a stream with a given |
2694 | | /// number of bits, the value must be validated to ensure |
2695 | | /// it will fit within that number of bits. |
2696 | | /// |
2697 | | /// # Example 1 |
2698 | | /// |
2699 | | /// ``` |
2700 | | /// use bitstream_io::{BitWrite, BitWriter, BigEndian}; |
2701 | | /// |
2702 | | /// let mut w = BitWriter::endian(vec![], BigEndian); |
2703 | | /// |
2704 | | /// // writing a value of 2 in 1 bit is always an error |
2705 | | /// // which is checked here at write-time |
2706 | | /// assert!(w.write::<1, u8>(2).is_err()); |
2707 | | /// ``` |
2708 | | /// |
2709 | | /// But if the value can be checked beforehand, |
2710 | | /// it doesn't need to be checked at write-time. |
2711 | | /// |
2712 | | /// # Example 2 |
2713 | | /// |
2714 | | /// ``` |
2715 | | /// use bitstream_io::{BitWrite, BitWriter, BigEndian, CheckedUnsigned}; |
2716 | | /// |
2717 | | /// let mut w = BitWriter::endian(vec![], BigEndian); |
2718 | | /// |
2719 | | /// // writing a value of 1 in 1 bit is ok |
2720 | | /// // and we're checking that validity at this stage |
2721 | | /// let value = CheckedUnsigned::<1, u8>::new_fixed::<1>(1).unwrap(); |
2722 | | /// |
2723 | | /// // because we've pre-validated the value beforehand, |
2724 | | /// // it doesn't need to be checked again here |
2725 | | /// // (though the write itself may still fail) |
2726 | | /// assert!(w.write_checked(value).is_ok()); |
2727 | | /// ``` |
2728 | | /// |
2729 | | pub trait Checkable: private::Checkable + Sized { |
2730 | | /// Write our value to the given stream |
2731 | | fn write<W: BitWrite + ?Sized>(&self, writer: &mut W) -> io::Result<()>; |
2732 | | |
2733 | | /// The number of written bits |
2734 | | fn written_bits(&self) -> u32; |
2735 | | } |
2736 | | |
2737 | | /// A trait for readable types whose bit counts can be saved |
2738 | | /// |
2739 | | /// Because the intent of reading checkable values is |
2740 | | /// to avoid validating their values when being written, |
2741 | | /// implementing the [`Checkable`] trait is required. |
2742 | | pub trait CheckablePrimitive: Checkable { |
2743 | | /// Our bit count type for reading |
2744 | | type CountType; |
2745 | | |
2746 | | /// Reads our value from the given stream |
2747 | | fn read<R: BitRead + ?Sized>(reader: &mut R, count: Self::CountType) -> io::Result<Self>; |
2748 | | } |
2749 | | |
2750 | | /// Big-endian, or most significant bits first |
2751 | | #[derive(Copy, Clone, Debug)] |
2752 | | pub struct BigEndian; |
2753 | | |
2754 | | /// Big-endian, or most significant bits first |
2755 | | pub type BE = BigEndian; |
2756 | | |
2757 | | impl BigEndian { |
2758 | | // checked in the sense that we've verified |
2759 | | // the output type is large enough to hold the |
2760 | | // requested number of bits |
2761 | | #[inline] |
2762 | 0 | fn read_bits_checked<const MAX: u32, R, U>( |
2763 | 0 | reader: &mut R, |
2764 | 0 | queue: &mut u8, |
2765 | 0 | queue_bits: &mut u32, |
2766 | 0 | BitCount { bits }: BitCount<MAX>, |
2767 | 0 | ) -> io::Result<U> |
2768 | 0 | where |
2769 | 0 | R: io::Read, |
2770 | 0 | U: UnsignedInteger, |
2771 | | { |
2772 | | // reads a whole value with the given number of |
2773 | | // bytes in our endianness, where the number of bytes |
2774 | | // must be less than or equal to the type's size in bytes |
2775 | | #[inline(always)] |
2776 | 0 | fn read_bytes<R, U>(reader: &mut R, bytes: usize) -> io::Result<U> |
2777 | 0 | where |
2778 | 0 | R: io::Read, |
2779 | 0 | U: UnsignedInteger, |
2780 | | { |
2781 | 0 | let mut buf = U::buffer(); |
2782 | 0 | reader |
2783 | 0 | .read_exact(&mut buf.as_mut()[(mem::size_of::<U>() - bytes)..]) |
2784 | 0 | .map(|()| U::from_be_bytes(buf)) |
2785 | 0 | } |
2786 | | |
2787 | 0 | if bits <= *queue_bits { |
2788 | | // all bits in queue, so no byte needed |
2789 | 0 | let value = queue.shr_default(u8::BITS_SIZE - bits); |
2790 | 0 | *queue = queue.shl_default(bits); |
2791 | 0 | *queue_bits -= bits; |
2792 | 0 | Ok(U::from_u8(value)) |
2793 | | } else { |
2794 | | // at least one byte needed |
2795 | | |
2796 | | // bits needed beyond what's in the queue |
2797 | 0 | let needed_bits = bits - *queue_bits; |
2798 | | |
2799 | 0 | match (needed_bits / 8, needed_bits % 8) { |
2800 | 0 | (0, needed) => { |
2801 | | // only one additional byte needed, |
2802 | | // which we share between our returned value |
2803 | | // and the bit queue |
2804 | 0 | let next_byte = read_byte(reader)?; |
2805 | | |
2806 | 0 | Ok(U::from_u8( |
2807 | 0 | mem::replace(queue, next_byte.shl_default(needed)).shr_default( |
2808 | 0 | u8::BITS_SIZE - mem::replace(queue_bits, u8::BITS_SIZE - needed), |
2809 | 0 | ), |
2810 | 0 | ) |
2811 | 0 | .shl_default(needed) |
2812 | 0 | | U::from_u8(next_byte.shr_default(u8::BITS_SIZE - needed))) |
2813 | | } |
2814 | 0 | (bytes, 0) => { |
2815 | | // exact number of bytes needed beyond what's |
2816 | | // available in the queue |
2817 | | // so read a whole value from the reader |
2818 | | // and prepend what's left of our queue onto it |
2819 | | |
2820 | 0 | Ok(U::from_u8( |
2821 | 0 | mem::take(queue).shr_default(u8::BITS_SIZE - mem::take(queue_bits)), |
2822 | | ) |
2823 | 0 | .shl_default(needed_bits) |
2824 | 0 | | read_bytes(reader, bytes as usize)?) |
2825 | | } |
2826 | 0 | (bytes, needed) => { |
2827 | | // read a whole value from the reader |
2828 | | // prepend what's in the queue at the front of it |
2829 | | // *and* append a partial byte at the end of it |
2830 | | // while also updating the queue and its bit count |
2831 | | |
2832 | 0 | let whole: U = read_bytes(reader, bytes as usize)?; |
2833 | 0 | let next_byte = read_byte(reader)?; |
2834 | | |
2835 | 0 | Ok(U::from_u8( |
2836 | 0 | mem::replace(queue, next_byte.shl_default(needed)).shr_default( |
2837 | 0 | u8::BITS_SIZE - mem::replace(queue_bits, u8::BITS_SIZE - needed), |
2838 | 0 | ), |
2839 | 0 | ) |
2840 | 0 | .shl_default(needed_bits) |
2841 | 0 | | whole.shl_default(needed) |
2842 | 0 | | U::from_u8(next_byte.shr_default(u8::BITS_SIZE - needed))) |
2843 | | } |
2844 | | } |
2845 | | } |
2846 | 0 | } |
2847 | | } |
2848 | | |
2849 | | impl Endianness for BigEndian {} |
2850 | | |
2851 | | impl private::Endianness for BigEndian { |
2852 | | #[inline] |
2853 | 0 | fn push_bit_flush(queue_value: &mut u8, queue_bits: &mut u32, bit: bool) -> Option<u8> { |
2854 | 0 | *queue_value = (*queue_value << 1) | u8::from(bit); |
2855 | 0 | *queue_bits = (*queue_bits + 1) % 8; |
2856 | 0 | (*queue_bits == 0).then(|| mem::take(queue_value)) Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::push_bit_flush::{closure#0}Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::push_bit_flush::{closure#0} |
2857 | 0 | } Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::push_bit_flush Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::push_bit_flush |
2858 | | |
2859 | | #[inline] |
2860 | 0 | fn read_bits<const MAX: u32, R, U>( |
2861 | 0 | reader: &mut R, |
2862 | 0 | queue_value: &mut u8, |
2863 | 0 | queue_bits: &mut u32, |
2864 | 0 | count @ BitCount { bits }: BitCount<MAX>, |
2865 | 0 | ) -> io::Result<U> |
2866 | 0 | where |
2867 | 0 | R: io::Read, |
2868 | 0 | U: UnsignedInteger, |
2869 | | { |
2870 | 0 | if MAX <= U::BITS_SIZE || bits <= U::BITS_SIZE { |
2871 | 0 | Self::read_bits_checked::<MAX, R, U>(reader, queue_value, queue_bits, count) |
2872 | | } else { |
2873 | 0 | Err(io::Error::new( |
2874 | 0 | io::ErrorKind::InvalidInput, |
2875 | 0 | "excessive bits for type read", |
2876 | 0 | )) |
2877 | | } |
2878 | 0 | } |
2879 | | |
2880 | | #[inline] |
2881 | 0 | fn read_bits_fixed<const BITS: u32, R, U>( |
2882 | 0 | reader: &mut R, |
2883 | 0 | queue_value: &mut u8, |
2884 | 0 | queue_bits: &mut u32, |
2885 | 0 | ) -> io::Result<U> |
2886 | 0 | where |
2887 | 0 | R: io::Read, |
2888 | 0 | U: UnsignedInteger, |
2889 | | { |
2890 | | const { |
2891 | | assert!(BITS <= U::BITS_SIZE, "excessive bits for type read"); |
2892 | | } |
2893 | | |
2894 | 0 | Self::read_bits_checked::<BITS, R, U>( |
2895 | 0 | reader, |
2896 | 0 | queue_value, |
2897 | 0 | queue_bits, |
2898 | 0 | BitCount::new::<BITS>(), |
2899 | | ) |
2900 | 0 | } |
2901 | | |
2902 | | // checked in the sense that we've verified |
2903 | | // the input type is large enough to hold the |
2904 | | // requested number of bits and that the value is |
2905 | | // not too large for those bits |
2906 | | #[inline] |
2907 | 0 | fn write_bits_checked<const MAX: u32, W, U>( |
2908 | 0 | writer: &mut W, |
2909 | 0 | queue_value: &mut u8, |
2910 | 0 | queue_bits: &mut u32, |
2911 | 0 | CheckedUnsigned { |
2912 | 0 | count: BitCount { bits }, |
2913 | 0 | value, |
2914 | 0 | }: CheckedUnsigned<MAX, U>, |
2915 | 0 | ) -> io::Result<()> |
2916 | 0 | where |
2917 | 0 | W: io::Write, |
2918 | 0 | U: UnsignedInteger, |
2919 | | { |
2920 | 0 | fn write_bytes<W, U>(writer: &mut W, bytes: usize, value: U) -> io::Result<()> |
2921 | 0 | where |
2922 | 0 | W: io::Write, |
2923 | 0 | U: UnsignedInteger, |
2924 | | { |
2925 | 0 | let buf = U::to_be_bytes(value); |
2926 | 0 | writer.write_all(&buf.as_ref()[(mem::size_of::<U>() - bytes)..]) |
2927 | 0 | } Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<&mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<&mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<&mut alloc::vec::Vec<u8>, u16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<&mut alloc::vec::Vec<u8>, u64> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<_, _> |
2928 | | |
2929 | | // the amount of available bits in the queue |
2930 | 0 | let available_bits = u8::BITS_SIZE - *queue_bits; |
2931 | | |
2932 | 0 | if bits < available_bits { |
2933 | | // all bits fit in queue, so no write needed |
2934 | 0 | *queue_value = queue_value.shl_default(bits) | U::to_u8(value); |
2935 | 0 | *queue_bits += bits; |
2936 | 0 | Ok(()) |
2937 | | } else { |
2938 | | // at least one byte needs to be written |
2939 | | |
2940 | | // bits beyond what can fit in the queue |
2941 | 0 | let excess_bits = bits - available_bits; |
2942 | | |
2943 | 0 | match (excess_bits / 8, excess_bits % 8) { |
2944 | 0 | (0, excess) => { |
2945 | | // only one byte to be written, |
2946 | | // while the excess bits are shared |
2947 | | // between the written byte and the bit queue |
2948 | | |
2949 | 0 | *queue_bits = excess; |
2950 | | |
2951 | 0 | write_byte( |
2952 | 0 | writer, |
2953 | 0 | mem::replace( |
2954 | 0 | queue_value, |
2955 | 0 | U::to_u8(value & U::ALL.shr_default(U::BITS_SIZE - excess)), |
2956 | 0 | ) |
2957 | 0 | .shl_default(available_bits) |
2958 | 0 | | U::to_u8(value.shr_default(excess)), |
2959 | | ) |
2960 | | } |
2961 | 0 | (bytes, 0) => { |
2962 | | // no excess bytes beyond what can fit the queue |
2963 | | // so write a whole byte and |
2964 | | // the remainder of the whole value |
2965 | | |
2966 | 0 | *queue_bits = 0; |
2967 | | |
2968 | 0 | write_byte( |
2969 | 0 | writer.by_ref(), |
2970 | 0 | mem::take(queue_value).shl_default(available_bits) |
2971 | 0 | | U::to_u8(value.shr_default(bytes * 8)), |
2972 | 0 | )?; |
2973 | | |
2974 | 0 | write_bytes(writer, bytes as usize, value) |
2975 | | } |
2976 | 0 | (bytes, excess) => { |
2977 | | // write what's in the queue along |
2978 | | // with the head of our whole value, |
2979 | | // write the middle section of our whole value, |
2980 | | // while also replacing the queue with |
2981 | | // the tail of our whole value |
2982 | | |
2983 | 0 | *queue_bits = excess; |
2984 | | |
2985 | 0 | write_byte( |
2986 | 0 | writer.by_ref(), |
2987 | 0 | mem::replace( |
2988 | 0 | queue_value, |
2989 | 0 | U::to_u8(value & U::ALL.shr_default(U::BITS_SIZE - excess)), |
2990 | 0 | ) |
2991 | 0 | .shl_default(available_bits) |
2992 | 0 | | U::to_u8(value.shr_default(excess + bytes * 8)), |
2993 | 0 | )?; |
2994 | | |
2995 | 0 | write_bytes(writer, bytes as usize, value.shr_default(excess)) |
2996 | | } |
2997 | | } |
2998 | | } |
2999 | 0 | } Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<16, &mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<16, &mut alloc::vec::Vec<u8>, u16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<1, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<1, &mut alloc::vec::Vec<u8>, u16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<32, &mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<32, &mut alloc::vec::Vec<u8>, u64> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<2, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<2, &mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<3, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<3, &mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<4, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<4, &mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<5, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<6, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<7, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<8, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<9, &mut alloc::vec::Vec<u8>, u16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<12, &mut alloc::vec::Vec<u8>, u16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<4294967295, &mut alloc::vec::Vec<u8>, u8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<4294967295, &mut alloc::vec::Vec<u8>, u32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<4294967295, &mut alloc::vec::Vec<u8>, u16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bits_checked::<_, _, _> |
3000 | | |
3001 | 0 | fn write_signed_bits_checked<const MAX: u32, W, S>( |
3002 | 0 | writer: &mut W, |
3003 | 0 | queue_value: &mut u8, |
3004 | 0 | queue_bits: &mut u32, |
3005 | 0 | value: CheckedSigned<MAX, S>, |
3006 | 0 | ) -> io::Result<()> |
3007 | 0 | where |
3008 | 0 | W: io::Write, |
3009 | 0 | S: SignedInteger, |
3010 | | { |
3011 | | let ( |
3012 | | SignedBitCount { |
3013 | 0 | bits: BitCount { bits }, |
3014 | 0 | unsigned, |
3015 | | }, |
3016 | 0 | value, |
3017 | 0 | ) = value.into_count_value(); |
3018 | | |
3019 | 0 | if let Some(b) = Self::push_bit_flush(queue_value, queue_bits, value.is_negative()) { |
3020 | 0 | write_byte(writer.by_ref(), b)?; |
3021 | 0 | } |
3022 | 0 | Self::write_bits_checked( |
3023 | 0 | writer, |
3024 | 0 | queue_value, |
3025 | 0 | queue_bits, |
3026 | | Checked { |
3027 | 0 | value: if value.is_negative() { |
3028 | 0 | value.as_negative(bits) |
3029 | | } else { |
3030 | 0 | value.as_non_negative() |
3031 | | }, |
3032 | 0 | count: unsigned, |
3033 | | }, |
3034 | | ) |
3035 | 0 | } Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_signed_bits_checked::<7, &mut alloc::vec::Vec<u8>, i8> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_signed_bits_checked::<4294967295, &mut alloc::vec::Vec<u8>, i32> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_signed_bits_checked::<4294967295, &mut alloc::vec::Vec<u8>, i16> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_signed_bits_checked::<_, _, _> |
3036 | | |
3037 | | #[inline] |
3038 | 0 | fn pop_bit_refill<R>( |
3039 | 0 | reader: &mut R, |
3040 | 0 | queue_value: &mut u8, |
3041 | 0 | queue_bits: &mut u32, |
3042 | 0 | ) -> io::Result<bool> |
3043 | 0 | where |
3044 | 0 | R: io::Read, |
3045 | | { |
3046 | 0 | Ok(if *queue_bits == 0 { |
3047 | 0 | let value = read_byte(reader)?; |
3048 | 0 | let msb = value & u8::MSB_BIT; |
3049 | 0 | *queue_value = value << 1; |
3050 | 0 | *queue_bits = u8::BITS_SIZE - 1; |
3051 | 0 | msb |
3052 | | } else { |
3053 | 0 | let msb = *queue_value & u8::MSB_BIT; |
3054 | 0 | *queue_value <<= 1; |
3055 | 0 | *queue_bits -= 1; |
3056 | 0 | msb |
3057 | | } != 0) |
3058 | 0 | } |
3059 | | |
3060 | | #[inline] |
3061 | 0 | fn pop_unary<const STOP_BIT: u8, R>( |
3062 | 0 | reader: &mut R, |
3063 | 0 | queue_value: &mut u8, |
3064 | 0 | queue_bits: &mut u32, |
3065 | 0 | ) -> io::Result<u32> |
3066 | 0 | where |
3067 | 0 | R: io::Read, |
3068 | | { |
3069 | | const { |
3070 | | assert!(matches!(STOP_BIT, 0 | 1), "stop bit must be 0 or 1"); |
3071 | | } |
3072 | | |
3073 | 0 | match STOP_BIT { |
3074 | 0 | 0 => find_unary( |
3075 | 0 | reader, |
3076 | 0 | queue_value, |
3077 | 0 | queue_bits, |
3078 | 0 | |v| v.leading_ones(), |
3079 | | |q| *q, |
3080 | 0 | |v, b| v.checked_shl(b), |
3081 | | ), |
3082 | 0 | 1 => find_unary( |
3083 | 0 | reader, |
3084 | 0 | queue_value, |
3085 | 0 | queue_bits, |
3086 | 0 | |v| v.leading_zeros(), |
3087 | | |_| u8::BITS_SIZE, |
3088 | 0 | |v, b| v.checked_shl(b), |
3089 | | ), |
3090 | 0 | _ => unreachable!(), |
3091 | | } |
3092 | 0 | } |
3093 | | |
3094 | | #[inline] |
3095 | 0 | fn read_signed_counted<const MAX: u32, R, S>( |
3096 | 0 | r: &mut R, |
3097 | 0 | SignedBitCount { |
3098 | 0 | bits: BitCount { bits }, |
3099 | 0 | unsigned, |
3100 | 0 | }: SignedBitCount<MAX>, |
3101 | 0 | ) -> io::Result<S> |
3102 | 0 | where |
3103 | 0 | R: BitRead, |
3104 | 0 | S: SignedInteger, |
3105 | | { |
3106 | 0 | if MAX <= S::BITS_SIZE || bits <= S::BITS_SIZE { |
3107 | 0 | let is_negative = r.read_bit()?; |
3108 | 0 | let unsigned = r.read_unsigned_counted::<MAX, S::Unsigned>(unsigned)?; |
3109 | 0 | Ok(if is_negative { |
3110 | 0 | unsigned.as_negative(bits) |
3111 | | } else { |
3112 | 0 | unsigned.as_non_negative() |
3113 | | }) |
3114 | | } else { |
3115 | 0 | Err(io::Error::new( |
3116 | 0 | io::ErrorKind::InvalidInput, |
3117 | 0 | "excessive bits for type read", |
3118 | 0 | )) |
3119 | | } |
3120 | 0 | } |
3121 | | |
3122 | 0 | fn read_bytes<const CHUNK_SIZE: usize, R>( |
3123 | 0 | reader: &mut R, |
3124 | 0 | queue_value: &mut u8, |
3125 | 0 | queue_bits: u32, |
3126 | 0 | buf: &mut [u8], |
3127 | 0 | ) -> io::Result<()> |
3128 | 0 | where |
3129 | 0 | R: io::Read, |
3130 | | { |
3131 | 0 | if queue_bits == 0 { |
3132 | 0 | reader.read_exact(buf) |
3133 | | } else { |
3134 | 0 | let mut input_chunk: [u8; CHUNK_SIZE] = [0; CHUNK_SIZE]; |
3135 | | |
3136 | 0 | for output_chunk in buf.chunks_mut(CHUNK_SIZE) { |
3137 | 0 | let input_chunk = &mut input_chunk[0..output_chunk.len()]; |
3138 | 0 | reader.read_exact(input_chunk)?; |
3139 | | |
3140 | | // shift down each byte in our input to eventually |
3141 | | // accomodate the contents of the bit queue |
3142 | | // and make that our output |
3143 | 0 | output_chunk |
3144 | 0 | .iter_mut() |
3145 | 0 | .zip(input_chunk.iter()) |
3146 | 0 | .for_each(|(o, i)| { |
3147 | 0 | *o = i >> queue_bits; |
3148 | 0 | }); |
3149 | | |
3150 | | // include leftover bits from the next byte |
3151 | | // shifted to the top |
3152 | 0 | output_chunk[1..] |
3153 | 0 | .iter_mut() |
3154 | 0 | .zip(input_chunk.iter()) |
3155 | 0 | .for_each(|(o, i)| { |
3156 | 0 | *o |= *i << (u8::BITS_SIZE - queue_bits); |
3157 | 0 | }); |
3158 | | |
3159 | | // finally, prepend the queue's contents |
3160 | | // to the first byte in the chunk |
3161 | | // while replacing those contents |
3162 | | // with the final byte of the input |
3163 | 0 | output_chunk[0] |= mem::replace( |
3164 | 0 | queue_value, |
3165 | 0 | input_chunk.last().unwrap() << (u8::BITS_SIZE - queue_bits), |
3166 | 0 | ); |
3167 | | } |
3168 | | |
3169 | 0 | Ok(()) |
3170 | | } |
3171 | 0 | } |
3172 | | |
3173 | 0 | fn write_bytes<const CHUNK_SIZE: usize, W>( |
3174 | 0 | writer: &mut W, |
3175 | 0 | queue_value: &mut u8, |
3176 | 0 | queue_bits: u32, |
3177 | 0 | buf: &[u8], |
3178 | 0 | ) -> io::Result<()> |
3179 | 0 | where |
3180 | 0 | W: io::Write, |
3181 | | { |
3182 | 0 | if queue_bits == 0 { |
3183 | 0 | writer.write_all(buf) |
3184 | | } else { |
3185 | 0 | let mut output_chunk: [u8; CHUNK_SIZE] = [0; CHUNK_SIZE]; |
3186 | | |
3187 | 0 | for input_chunk in buf.chunks(CHUNK_SIZE) { |
3188 | 0 | let output_chunk = &mut output_chunk[0..input_chunk.len()]; |
3189 | | |
3190 | 0 | output_chunk |
3191 | 0 | .iter_mut() |
3192 | 0 | .zip(input_chunk.iter()) |
3193 | 0 | .for_each(|(o, i)| { |
3194 | 0 | *o = i >> queue_bits; |
3195 | 0 | }); Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bytes::<1024, &mut alloc::vec::Vec<u8>>::{closure#0}Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bytes::<_, _>::{closure#0} |
3196 | | |
3197 | 0 | output_chunk[1..] |
3198 | 0 | .iter_mut() |
3199 | 0 | .zip(input_chunk.iter()) |
3200 | 0 | .for_each(|(o, i)| { |
3201 | 0 | *o |= *i << (u8::BITS_SIZE - queue_bits); |
3202 | 0 | }); Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bytes::<1024, &mut alloc::vec::Vec<u8>>::{closure#1}Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bytes::<_, _>::{closure#1} |
3203 | | |
3204 | 0 | output_chunk[0] |= mem::replace( |
3205 | 0 | queue_value, |
3206 | 0 | input_chunk.last().unwrap() & (u8::ALL >> (u8::BITS_SIZE - queue_bits)), |
3207 | 0 | ) << (u8::BITS_SIZE - queue_bits); |
3208 | | |
3209 | 0 | writer.write_all(output_chunk)?; |
3210 | | } |
3211 | | |
3212 | 0 | Ok(()) |
3213 | | } |
3214 | 0 | } Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bytes::<1024, &mut alloc::vec::Vec<u8>> Unexecuted instantiation: <bitstream_io::BigEndian as bitstream_io::private::Endianness>::write_bytes::<_, _> |
3215 | | |
3216 | | #[inline(always)] |
3217 | 0 | fn bytes_to_primitive<P: Primitive>(buf: P::Bytes) -> P { |
3218 | 0 | P::from_be_bytes(buf) |
3219 | 0 | } |
3220 | | |
3221 | | #[inline(always)] |
3222 | 0 | fn primitive_to_bytes<P: Primitive>(p: P) -> P::Bytes { |
3223 | 0 | p.to_be_bytes() |
3224 | 0 | } |
3225 | | |
3226 | | #[inline] |
3227 | 0 | fn read_primitive<R, V>(r: &mut R) -> io::Result<V> |
3228 | 0 | where |
3229 | 0 | R: BitRead, |
3230 | 0 | V: Primitive, |
3231 | | { |
3232 | 0 | let mut buffer = V::buffer(); |
3233 | 0 | r.read_bytes(buffer.as_mut())?; |
3234 | 0 | Ok(V::from_be_bytes(buffer)) |
3235 | 0 | } |
3236 | | |
3237 | | #[inline] |
3238 | 0 | fn write_primitive<W, V>(w: &mut W, value: V) -> io::Result<()> |
3239 | 0 | where |
3240 | 0 | W: BitWrite, |
3241 | 0 | V: Primitive, |
3242 | | { |
3243 | 0 | w.write_bytes(value.to_be_bytes().as_ref()) |
3244 | 0 | } |
3245 | | } |
3246 | | |
3247 | | /// Little-endian, or least significant bits first |
3248 | | #[derive(Copy, Clone, Debug)] |
3249 | | pub struct LittleEndian; |
3250 | | |
3251 | | /// Little-endian, or least significant bits first |
3252 | | pub type LE = LittleEndian; |
3253 | | |
3254 | | impl LittleEndian { |
3255 | | // checked in the sense that we've verified |
3256 | | // the output type is large enough to hold the |
3257 | | // requested number of bits |
3258 | | #[inline] |
3259 | 0 | fn read_bits_checked<const MAX: u32, R, U>( |
3260 | 0 | reader: &mut R, |
3261 | 0 | queue: &mut u8, |
3262 | 0 | queue_bits: &mut u32, |
3263 | 0 | BitCount { bits }: BitCount<MAX>, |
3264 | 0 | ) -> io::Result<U> |
3265 | 0 | where |
3266 | 0 | R: io::Read, |
3267 | 0 | U: UnsignedInteger, |
3268 | | { |
3269 | | // reads a whole value with the given number of |
3270 | | // bytes in our endianness, where the number of bytes |
3271 | | // must be less than or equal to the type's size in bytes |
3272 | | #[inline(always)] |
3273 | 0 | fn read_bytes<R, U>(reader: &mut R, bytes: usize) -> io::Result<U> |
3274 | 0 | where |
3275 | 0 | R: io::Read, |
3276 | 0 | U: UnsignedInteger, |
3277 | | { |
3278 | 0 | let mut buf = U::buffer(); |
3279 | 0 | reader |
3280 | 0 | .read_exact(&mut buf.as_mut()[0..bytes]) |
3281 | 0 | .map(|()| U::from_le_bytes(buf)) |
3282 | 0 | } |
3283 | | |
3284 | 0 | if bits <= *queue_bits { |
3285 | | // all bits in queue, so no byte needed |
3286 | 0 | let value = *queue & u8::ALL.shr_default(u8::BITS_SIZE - bits); |
3287 | 0 | *queue = queue.shr_default(bits); |
3288 | 0 | *queue_bits -= bits; |
3289 | 0 | Ok(U::from_u8(value)) |
3290 | | } else { |
3291 | | // at least one byte needed |
3292 | | |
3293 | | // bits needed beyond what's in the queue |
3294 | 0 | let needed_bits = bits - *queue_bits; |
3295 | | |
3296 | 0 | match (needed_bits / 8, needed_bits % 8) { |
3297 | 0 | (0, needed) => { |
3298 | | // only one additional byte needed, |
3299 | | // which we share between our returned value |
3300 | | // and the bit queue |
3301 | 0 | let next_byte = read_byte(reader)?; |
3302 | | |
3303 | 0 | Ok( |
3304 | 0 | U::from_u8(mem::replace(queue, next_byte.shr_default(needed))) |
3305 | 0 | | (U::from_u8(next_byte & (u8::ALL >> (u8::BITS_SIZE - needed))) |
3306 | 0 | << mem::replace(queue_bits, u8::BITS_SIZE - needed)), |
3307 | 0 | ) |
3308 | | } |
3309 | 0 | (bytes, 0) => { |
3310 | | // exact number of bytes needed beyond what's |
3311 | | // available in the queue |
3312 | | |
3313 | | // so read a whole value from the reader |
3314 | | // and prepend what's left of our queue onto it |
3315 | | |
3316 | 0 | Ok(U::from_u8(mem::take(queue)) |
3317 | 0 | | (read_bytes::<R, U>(reader, bytes as usize)? << mem::take(queue_bits))) |
3318 | | } |
3319 | 0 | (bytes, needed) => { |
3320 | | // read a whole value from the reader |
3321 | | // prepend what's in the queue at the front of it |
3322 | | // *and* append a partial byte at the end of it |
3323 | | // while also updating the queue and its bit count |
3324 | | |
3325 | 0 | let whole: U = read_bytes(reader, bytes as usize)?; |
3326 | 0 | let next_byte = read_byte(reader)?; |
3327 | | |
3328 | 0 | Ok( |
3329 | 0 | U::from_u8(mem::replace(queue, next_byte.shr_default(needed))) |
3330 | 0 | | (whole << *queue_bits) |
3331 | 0 | | (U::from_u8(next_byte & (u8::ALL >> (u8::BITS_SIZE - needed))) |
3332 | 0 | << (mem::replace(queue_bits, u8::BITS_SIZE - needed) + bytes * 8)), |
3333 | 0 | ) |
3334 | | } |
3335 | | } |
3336 | | } |
3337 | 0 | } |
3338 | | } |
3339 | | |
3340 | | impl Endianness for LittleEndian {} |
3341 | | |
3342 | | impl private::Endianness for LittleEndian { |
3343 | | #[inline] |
3344 | 0 | fn push_bit_flush(queue_value: &mut u8, queue_bits: &mut u32, bit: bool) -> Option<u8> { |
3345 | 0 | *queue_value |= u8::from(bit) << *queue_bits; |
3346 | 0 | *queue_bits = (*queue_bits + 1) % 8; |
3347 | 0 | (*queue_bits == 0).then(|| mem::take(queue_value)) |
3348 | 0 | } |
3349 | | |
3350 | | #[inline] |
3351 | 0 | fn read_bits<const MAX: u32, R, U>( |
3352 | 0 | reader: &mut R, |
3353 | 0 | queue_value: &mut u8, |
3354 | 0 | queue_bits: &mut u32, |
3355 | 0 | count @ BitCount { bits }: BitCount<MAX>, |
3356 | 0 | ) -> io::Result<U> |
3357 | 0 | where |
3358 | 0 | R: io::Read, |
3359 | 0 | U: UnsignedInteger, |
3360 | | { |
3361 | 0 | if MAX <= U::BITS_SIZE || bits <= U::BITS_SIZE { |
3362 | 0 | Self::read_bits_checked::<MAX, R, U>(reader, queue_value, queue_bits, count) |
3363 | | } else { |
3364 | 0 | Err(io::Error::new( |
3365 | 0 | io::ErrorKind::InvalidInput, |
3366 | 0 | "excessive bits for type read", |
3367 | 0 | )) |
3368 | | } |
3369 | 0 | } |
3370 | | |
3371 | | #[inline] |
3372 | 0 | fn read_bits_fixed<const BITS: u32, R, U>( |
3373 | 0 | reader: &mut R, |
3374 | 0 | queue_value: &mut u8, |
3375 | 0 | queue_bits: &mut u32, |
3376 | 0 | ) -> io::Result<U> |
3377 | 0 | where |
3378 | 0 | R: io::Read, |
3379 | 0 | U: UnsignedInteger, |
3380 | | { |
3381 | | const { |
3382 | | assert!(BITS <= U::BITS_SIZE, "excessive bits for type read"); |
3383 | | } |
3384 | | |
3385 | 0 | Self::read_bits_checked::<BITS, R, U>( |
3386 | 0 | reader, |
3387 | 0 | queue_value, |
3388 | 0 | queue_bits, |
3389 | 0 | BitCount::new::<BITS>(), |
3390 | | ) |
3391 | 0 | } |
3392 | | |
3393 | | // checked in the sense that we've verified |
3394 | | // the input type is large enough to hold the |
3395 | | // requested number of bits and that the value is |
3396 | | // not too large for those bits |
3397 | | #[inline] |
3398 | 0 | fn write_bits_checked<const MAX: u32, W, U>( |
3399 | 0 | writer: &mut W, |
3400 | 0 | queue_value: &mut u8, |
3401 | 0 | queue_bits: &mut u32, |
3402 | 0 | CheckedUnsigned { |
3403 | 0 | count: BitCount { bits }, |
3404 | 0 | value, |
3405 | 0 | }: CheckedUnsigned<MAX, U>, |
3406 | 0 | ) -> io::Result<()> |
3407 | 0 | where |
3408 | 0 | W: io::Write, |
3409 | 0 | U: UnsignedInteger, |
3410 | | { |
3411 | 0 | fn write_bytes<W, U>(writer: &mut W, bytes: usize, value: U) -> io::Result<()> |
3412 | 0 | where |
3413 | 0 | W: io::Write, |
3414 | 0 | U: UnsignedInteger, |
3415 | | { |
3416 | 0 | let buf = U::to_le_bytes(value); |
3417 | 0 | writer.write_all(&buf.as_ref()[0..bytes]) |
3418 | 0 | } Unexecuted instantiation: <bitstream_io::LittleEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<&mut alloc::vec::Vec<u8>, u64> Unexecuted instantiation: <bitstream_io::LittleEndian as bitstream_io::private::Endianness>::write_bits_checked::write_bytes::<_, _> |
3419 | | |
3420 | | // the amount of available bits in the queue |
3421 | 0 | let available_bits = u8::BITS_SIZE - *queue_bits; |
3422 | | |
3423 | 0 | if bits < available_bits { |
3424 | | // all bits fit in queue, so no write needed |
3425 | 0 | *queue_value |= U::to_u8(value.shl_default(*queue_bits)); |
3426 | 0 | *queue_bits += bits; |
3427 | 0 | Ok(()) |
3428 | | } else { |
3429 | | // at least one byte needs to be written |
3430 | | |
3431 | | // bits beyond what can fit in the queue |
3432 | 0 | let excess_bits = bits - available_bits; |
3433 | | |
3434 | 0 | match (excess_bits / 8, excess_bits % 8) { |
3435 | 0 | (0, excess) => { |
3436 | | // only one byte to be written, |
3437 | | // while the excess bits are shared |
3438 | | // between the written byte and the bit queue |
3439 | | |
3440 | 0 | write_byte( |
3441 | 0 | writer, |
3442 | 0 | mem::replace(queue_value, U::to_u8(value.shr_default(available_bits))) |
3443 | 0 | | U::to_u8( |
3444 | 0 | (value << mem::replace(queue_bits, excess)) & U::from_u8(u8::ALL), |
3445 | 0 | ), |
3446 | | ) |
3447 | | } |
3448 | 0 | (bytes, 0) => { |
3449 | | // no excess bytes beyond what can fit the queue |
3450 | | // so write a whole byte and |
3451 | | // the remainder of the whole value |
3452 | | |
3453 | 0 | write_byte( |
3454 | 0 | writer.by_ref(), |
3455 | 0 | mem::take(queue_value) |
3456 | 0 | | U::to_u8((value << mem::take(queue_bits)) & U::from_u8(u8::ALL)), |
3457 | 0 | )?; |
3458 | | |
3459 | 0 | write_bytes(writer, bytes as usize, value >> available_bits) |
3460 | | } |
3461 | 0 | (bytes, excess) => { |
3462 | | // write what's in the queue along |
3463 | | // with the head of our whole value, |
3464 | | // write the middle section of our whole value, |
3465 | | // while also replacing the queue with |
3466 | | // the tail of our whole value |
3467 | | |
3468 | 0 | write_byte( |
3469 | 0 | writer.by_ref(), |
3470 | 0 | mem::replace( |
3471 | 0 | queue_value, |
3472 | 0 | U::to_u8(value.shr_default(available_bits + bytes * 8)), |
3473 | 0 | ) | U::to_u8( |
3474 | 0 | (value << mem::replace(queue_bits, excess)) & U::from_u8(u8::ALL), |
3475 | 0 | ), |
3476 | 0 | )?; |
3477 | | |
3478 | 0 | write_bytes(writer, bytes as usize, value >> available_bits) |
3479 | | } |
3480 | | } |
3481 | | } |
3482 | 0 | } Unexecuted instantiation: <bitstream_io::LittleEndian as bitstream_io::private::Endianness>::write_bits_checked::<4294967295, &mut alloc::vec::Vec<u8>, u64> Unexecuted instantiation: <bitstream_io::LittleEndian as bitstream_io::private::Endianness>::write_bits_checked::<_, _, _> |
3483 | | |
3484 | 0 | fn write_signed_bits_checked<const MAX: u32, W, S>( |
3485 | 0 | writer: &mut W, |
3486 | 0 | queue_value: &mut u8, |
3487 | 0 | queue_bits: &mut u32, |
3488 | 0 | value: CheckedSigned<MAX, S>, |
3489 | 0 | ) -> io::Result<()> |
3490 | 0 | where |
3491 | 0 | W: io::Write, |
3492 | 0 | S: SignedInteger, |
3493 | | { |
3494 | | // little-endian |
3495 | | let ( |
3496 | | SignedBitCount { |
3497 | 0 | bits: BitCount { bits }, |
3498 | 0 | unsigned, |
3499 | | }, |
3500 | 0 | value, |
3501 | 0 | ) = value.into_count_value(); |
3502 | | |
3503 | 0 | Self::write_bits_checked( |
3504 | 0 | writer.by_ref(), |
3505 | 0 | queue_value, |
3506 | 0 | queue_bits, |
3507 | | Checked { |
3508 | 0 | value: if value.is_negative() { |
3509 | 0 | value.as_negative(bits) |
3510 | | } else { |
3511 | 0 | value.as_non_negative() |
3512 | | }, |
3513 | 0 | count: unsigned, |
3514 | | }, |
3515 | 0 | )?; |
3516 | 0 | match Self::push_bit_flush(queue_value, queue_bits, value.is_negative()) { |
3517 | 0 | Some(b) => write_byte(writer, b), |
3518 | 0 | None => Ok(()), |
3519 | | } |
3520 | 0 | } |
3521 | | |
3522 | | #[inline] |
3523 | 0 | fn pop_bit_refill<R>( |
3524 | 0 | reader: &mut R, |
3525 | 0 | queue_value: &mut u8, |
3526 | 0 | queue_bits: &mut u32, |
3527 | 0 | ) -> io::Result<bool> |
3528 | 0 | where |
3529 | 0 | R: io::Read, |
3530 | | { |
3531 | 0 | Ok(if *queue_bits == 0 { |
3532 | 0 | let value = read_byte(reader)?; |
3533 | 0 | let lsb = value & u8::LSB_BIT; |
3534 | 0 | *queue_value = value >> 1; |
3535 | 0 | *queue_bits = u8::BITS_SIZE - 1; |
3536 | 0 | lsb |
3537 | | } else { |
3538 | 0 | let lsb = *queue_value & u8::LSB_BIT; |
3539 | 0 | *queue_value >>= 1; |
3540 | 0 | *queue_bits -= 1; |
3541 | 0 | lsb |
3542 | | } != 0) |
3543 | 0 | } |
3544 | | |
3545 | | #[inline] |
3546 | 0 | fn pop_unary<const STOP_BIT: u8, R>( |
3547 | 0 | reader: &mut R, |
3548 | 0 | queue_value: &mut u8, |
3549 | 0 | queue_bits: &mut u32, |
3550 | 0 | ) -> io::Result<u32> |
3551 | 0 | where |
3552 | 0 | R: io::Read, |
3553 | | { |
3554 | | const { |
3555 | | assert!(matches!(STOP_BIT, 0 | 1), "stop bit must be 0 or 1"); |
3556 | | } |
3557 | | |
3558 | 0 | match STOP_BIT { |
3559 | 0 | 0 => find_unary( |
3560 | 0 | reader, |
3561 | 0 | queue_value, |
3562 | 0 | queue_bits, |
3563 | 0 | |v| v.trailing_ones(), |
3564 | | |q| *q, |
3565 | 0 | |v, b| v.checked_shr(b), |
3566 | | ), |
3567 | 0 | 1 => find_unary( |
3568 | 0 | reader, |
3569 | 0 | queue_value, |
3570 | 0 | queue_bits, |
3571 | 0 | |v| v.trailing_zeros(), |
3572 | | |_| u8::BITS_SIZE, |
3573 | 0 | |v, b| v.checked_shr(b), |
3574 | | ), |
3575 | 0 | _ => unreachable!(), |
3576 | | } |
3577 | 0 | } |
3578 | | |
3579 | | #[inline] |
3580 | 0 | fn read_signed_counted<const MAX: u32, R, S>( |
3581 | 0 | r: &mut R, |
3582 | 0 | SignedBitCount { |
3583 | 0 | bits: BitCount { bits }, |
3584 | 0 | unsigned, |
3585 | 0 | }: SignedBitCount<MAX>, |
3586 | 0 | ) -> io::Result<S> |
3587 | 0 | where |
3588 | 0 | R: BitRead, |
3589 | 0 | S: SignedInteger, |
3590 | | { |
3591 | 0 | if MAX <= S::BITS_SIZE || bits <= S::BITS_SIZE { |
3592 | 0 | let unsigned = r.read_unsigned_counted::<MAX, S::Unsigned>(unsigned)?; |
3593 | 0 | let is_negative = r.read_bit()?; |
3594 | 0 | Ok(if is_negative { |
3595 | 0 | unsigned.as_negative(bits) |
3596 | | } else { |
3597 | 0 | unsigned.as_non_negative() |
3598 | | }) |
3599 | | } else { |
3600 | 0 | Err(io::Error::new( |
3601 | 0 | io::ErrorKind::InvalidInput, |
3602 | 0 | "excessive bits for type read", |
3603 | 0 | )) |
3604 | | } |
3605 | 0 | } |
3606 | | |
3607 | 0 | fn read_bytes<const CHUNK_SIZE: usize, R>( |
3608 | 0 | reader: &mut R, |
3609 | 0 | queue_value: &mut u8, |
3610 | 0 | queue_bits: u32, |
3611 | 0 | buf: &mut [u8], |
3612 | 0 | ) -> io::Result<()> |
3613 | 0 | where |
3614 | 0 | R: io::Read, |
3615 | | { |
3616 | 0 | if queue_bits == 0 { |
3617 | 0 | reader.read_exact(buf) |
3618 | | } else { |
3619 | 0 | let mut input_chunk: [u8; CHUNK_SIZE] = [0; CHUNK_SIZE]; |
3620 | | |
3621 | 0 | for output_chunk in buf.chunks_mut(CHUNK_SIZE) { |
3622 | 0 | let input_chunk = &mut input_chunk[0..output_chunk.len()]; |
3623 | 0 | reader.read_exact(input_chunk)?; |
3624 | | |
3625 | 0 | output_chunk |
3626 | 0 | .iter_mut() |
3627 | 0 | .zip(input_chunk.iter()) |
3628 | 0 | .for_each(|(o, i)| { |
3629 | 0 | *o = i << queue_bits; |
3630 | 0 | }); |
3631 | | |
3632 | 0 | output_chunk[1..] |
3633 | 0 | .iter_mut() |
3634 | 0 | .zip(input_chunk.iter()) |
3635 | 0 | .for_each(|(o, i)| { |
3636 | 0 | *o |= i >> (u8::BITS_SIZE - queue_bits); |
3637 | 0 | }); |
3638 | | |
3639 | 0 | output_chunk[0] |= mem::replace( |
3640 | 0 | queue_value, |
3641 | 0 | input_chunk.last().unwrap() >> (u8::BITS_SIZE - queue_bits), |
3642 | 0 | ); |
3643 | | } |
3644 | | |
3645 | 0 | Ok(()) |
3646 | | } |
3647 | 0 | } |
3648 | | |
3649 | 0 | fn write_bytes<const CHUNK_SIZE: usize, W>( |
3650 | 0 | writer: &mut W, |
3651 | 0 | queue_value: &mut u8, |
3652 | 0 | queue_bits: u32, |
3653 | 0 | buf: &[u8], |
3654 | 0 | ) -> io::Result<()> |
3655 | 0 | where |
3656 | 0 | W: io::Write, |
3657 | | { |
3658 | 0 | if queue_bits == 0 { |
3659 | 0 | writer.write_all(buf) |
3660 | | } else { |
3661 | 0 | let mut output_chunk: [u8; CHUNK_SIZE] = [0; CHUNK_SIZE]; |
3662 | | |
3663 | 0 | for input_chunk in buf.chunks(CHUNK_SIZE) { |
3664 | 0 | let output_chunk = &mut output_chunk[0..input_chunk.len()]; |
3665 | | |
3666 | 0 | output_chunk |
3667 | 0 | .iter_mut() |
3668 | 0 | .zip(input_chunk.iter()) |
3669 | 0 | .for_each(|(o, i)| { |
3670 | 0 | *o = i << queue_bits; |
3671 | 0 | }); |
3672 | | |
3673 | 0 | output_chunk[1..] |
3674 | 0 | .iter_mut() |
3675 | 0 | .zip(input_chunk.iter()) |
3676 | 0 | .for_each(|(o, i)| { |
3677 | 0 | *o |= i >> (u8::BITS_SIZE - queue_bits); |
3678 | 0 | }); |
3679 | | |
3680 | 0 | output_chunk[0] |= mem::replace( |
3681 | 0 | queue_value, |
3682 | 0 | input_chunk.last().unwrap() >> (u8::BITS_SIZE - queue_bits), |
3683 | 0 | ); |
3684 | | |
3685 | 0 | writer.write_all(output_chunk)?; |
3686 | | } |
3687 | | |
3688 | 0 | Ok(()) |
3689 | | } |
3690 | 0 | } |
3691 | | |
3692 | | #[inline(always)] |
3693 | 0 | fn bytes_to_primitive<P: Primitive>(buf: P::Bytes) -> P { |
3694 | 0 | P::from_le_bytes(buf) |
3695 | 0 | } |
3696 | | |
3697 | | #[inline(always)] |
3698 | 0 | fn primitive_to_bytes<P: Primitive>(p: P) -> P::Bytes { |
3699 | 0 | p.to_le_bytes() |
3700 | 0 | } |
3701 | | |
3702 | | #[inline] |
3703 | 0 | fn read_primitive<R, V>(r: &mut R) -> io::Result<V> |
3704 | 0 | where |
3705 | 0 | R: BitRead, |
3706 | 0 | V: Primitive, |
3707 | | { |
3708 | 0 | let mut buffer = V::buffer(); |
3709 | 0 | r.read_bytes(buffer.as_mut())?; |
3710 | 0 | Ok(V::from_le_bytes(buffer)) |
3711 | 0 | } |
3712 | | |
3713 | | #[inline] |
3714 | 0 | fn write_primitive<W, V>(w: &mut W, value: V) -> io::Result<()> |
3715 | 0 | where |
3716 | 0 | W: BitWrite, |
3717 | 0 | V: Primitive, |
3718 | | { |
3719 | 0 | w.write_bytes(value.to_le_bytes().as_ref()) |
3720 | 0 | } |
3721 | | } |
3722 | | |
3723 | | #[inline] |
3724 | 0 | fn find_unary<R>( |
3725 | 0 | reader: &mut R, |
3726 | 0 | queue_value: &mut u8, |
3727 | 0 | queue_bits: &mut u32, |
3728 | 0 | leading_bits: impl Fn(u8) -> u32, |
3729 | 0 | max_bits: impl Fn(&mut u32) -> u32, |
3730 | 0 | checked_shift: impl Fn(u8, u32) -> Option<u8>, |
3731 | 0 | ) -> io::Result<u32> |
3732 | 0 | where |
3733 | 0 | R: io::Read, |
3734 | | { |
3735 | 0 | let mut acc = 0; |
3736 | | |
3737 | | loop { |
3738 | 0 | match leading_bits(*queue_value) { |
3739 | 0 | bits if bits == max_bits(queue_bits) => { |
3740 | | // all bits exhausted |
3741 | | // fetch another byte and keep going |
3742 | 0 | acc += *queue_bits; |
3743 | 0 | *queue_value = read_byte(reader.by_ref())?; |
3744 | 0 | *queue_bits = u8::BITS_SIZE; |
3745 | | } |
3746 | 0 | bits => match checked_shift(*queue_value, bits + 1) { |
3747 | 0 | Some(value) => { |
3748 | | // fetch part of source byte |
3749 | 0 | *queue_value = value; |
3750 | 0 | *queue_bits -= bits + 1; |
3751 | 0 | break Ok(acc + bits); |
3752 | | } |
3753 | | None => { |
3754 | | // fetch all of source byte |
3755 | 0 | *queue_value = 0; |
3756 | 0 | *queue_bits = 0; |
3757 | 0 | break Ok(acc + bits); |
3758 | | } |
3759 | | }, |
3760 | | } |
3761 | | } |
3762 | 0 | } |