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