Crate bitstream_io
Traits and helpers for bitstream handling functionality
Bitstream readers are for reading signed and unsigned integer values from a stream whose sizes may not be whole bytes. Bitstream writers are for writing signed and unsigned integer values to a stream, also potentially un-aligned at a whole byte.
Both big-endian and little-endian streams are supported.
The only requirement for wrapped reader streams is that they must
implement the Read trait, and the only requirement
for writer streams is that they must implement the Write trait.
In addition, reader streams do not consume any more bytes from the underlying reader than necessary, buffering only a single partial byte as needed. Writer streams also write out all whole bytes as they are accumulated.
Readers and writers are also designed to work with integer types of any possible size. Many of Rust's built-in integer types are supported by default.
Minimum Compiler Version
Beginning with version 2.4, the minimum compiler version has been updated to Rust 1.79.
The issue is that reading an excessive number of bits to a type which is too small to hold them, or writing an excessive number of bits from too small of a type, are always errors:
use ;
use ;
let data = ;
let mut r = endian;
let x: = r.read; // reading 64 bits to u32 always fails at runtime
assert!;
but those errors will not be caught until the program runs, which is less than ideal for the common case in which the number of bits is already known at compile-time.
But starting with Rust 1.79, we can now have read and write methods which take a constant number of bits and can validate the number of bits are small enough for the type being read/written at compile-time:
use ;
use ;
let data = ;
let mut r = endian;
let x: = r.; // doesn't compile at all
Since catching potential bugs at compile-time is preferable to encountering errors at runtime, this will hopefully be an improvement in the long run.
Migrating From Pre 1.0.0
There are now BitRead and BitWrite traits for bitstream
reading and writing (analogous to the standard library's
Read and Write traits) which you will also need to import.
The upside to this approach is that library consumers
can now make functions and methods generic over any sort
of bit reader or bit writer, regardless of the underlying
stream byte source or endianness.
Modules
Structs
- BigEndian Big-endian, or most significant bits first
- BitCounter For counting the number of bits written but generating no output.
- BitQueue A queue for efficiently pushing bits onto a value and popping them off a value.
- BitReader For reading non-aligned bits from a stream of bytes in a given endianness.
-
BitRecorder
For recording writes in order to play them back on another writer
Example
use Write; use ; let mut recorder: = new; recorder.write.unwrap; recorder.write.unwrap; recorder.write.unwrap; assert_eq!; let mut writer = endian; recorder.playback; assert_eq!; - BitWriter For writing bit values to an underlying stream in a given endianness.
- ByteReader For reading aligned bytes from a stream of bytes in a given endianness.
- ByteWriter For writing aligned bytes to a stream of bytes in a given endianness.
- LittleEndian Little-endian, or least significant bits first
Traits
- BitRead A trait for anything that can read a variable number of potentially un-aligned values from an input stream
- BitWrite A trait for anything that can write a variable number of potentially un-aligned values to an output stream
- ByteRead A trait for anything that can read aligned values from an input stream
- ByteWrite A trait for anything that can write aligned values to an output stream
- Endianness A stream's endianness, or byte order, for determining how bits should be read.
-
FromBitStream
Implemented by complex types that don't require any additional context
to parse themselves from a reader. Analagous to
FromStr. - FromBitStreamWith Implemented by complex types that require some immutable context to parse themselves from a reader.
-
FromByteStream
Implemented by complex types that don't require any additional context
to parse themselves from a reader. Analagous to
FromStr. -
FromByteStreamWith
Implemented by complex types that require some additional context
to parse themselves from a reader. Analagous to
FromStr. - HuffmanRead A trait for anything that can read Huffman codes of a given endianness from an input stream
- HuffmanWrite A trait for anything that can write Huffman codes of a given endianness to an output stream
- Numeric This trait extends many common integer types (both unsigned and signed) with a few trivial methods so that they can be used with the bitstream handling traits.
- Primitive A trait intended for simple fixed-length primitives (such as ints and floats) which allows them to be read and written to streams of different endiannesses verbatim.
- SignedNumeric This trait extends many common signed integer types so that they can be used with the bitstream handling traits.
- ToBitStream Implemented by complex types that don't require any additional context to build themselves to a writer
- ToBitStreamWith Implemented by complex types that require additional context to build themselves to a writer
- ToByteStream Implemented by complex types that don't require any additional context to build themselves to a writer
- ToByteStreamWith Implemented by complex types that require additional context to build themselves to a writer