1
0
Fork 0
mirror of https://codeberg.org/icewind/bitbuffer.git synced 2026-06-04 00:54:07 +02:00

optimize push_bits

This commit is contained in:
Robin Appelman 2020-02-15 14:08:54 +01:00
commit 123223f479
3 changed files with 29 additions and 69 deletions

View file

@ -6,24 +6,22 @@
Tools for reading data types of arbitrary bit length and might not be byte-aligned in the source data
The main way of handling with the binary data is to first create a `BitBuffer`
,wrap it into a `BitStream` and then read from the stream.
If performance is critical, working directly on the BitBuffer can be faster.
The main way of handling with the binary data is to first create a [`BitBuffer`]
,wrap it into a [`BitStream`] and then read from the stream.
Once you have a BitStream, there are 2 different approaches of reading data
- read primitives, Strings and byte arrays, using `read_bool`, `read_int`, `read_float`, `read_byes` and `read_string`
- read any type implementing the `BitRead` or `BitReadSized` traits using `read` and `read_sized`
- `BitRead` is for types that can be read without requiring any size info (e.g. null-terminated strings, floats, whole integers, etc)
- `BitReadSized` is for types that require external sizing information to be read (fixed length strings, arbitrary length integers
- read primitives, Strings and byte arrays, using [`read_bool`], [`read_int`], [`read_float`], [`read_bytes`] and [`read_string`]
- read any type implementing the [`BitRead`] or [`BitReadSized`] traits using [`read`] and [`read_sized`]
- [`BitRead`] is for types that can be read without requiring any size info (e.g. null-terminal strings, floats, whole integers, etc)
- [`BitReadSized`] is for types that require external sizing information to be read (fixed length strings, arbitrary length integers
The `BitRead` and `BitReadSized` traits can be used with `#[derive]` if all fields implement `BitRead` or `BitReadSized`.
The [`BitRead`] and [`BitReadSized`] traits can be used with `#[derive]` if all fields implement [`BitRead`] or [`BitReadSized`].
## Examples
```rust
use bitbuffer::{BitBuffer, LittleEndian, BitStream, BitRead};
use bitbuffer::{BitReadBuffer, LittleEndian, BitReadStream, BitRead};
#[derive(BitRead)]
struct ComplexType {
@ -33,16 +31,14 @@ struct ComplexType {
third: bool,
}
fn main() {
let bytes = vec![
0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
];
let buffer = BitBuffer::new(bytes, LittleEndian);
let mut stream = BitStream::new(buffer);
let value: u8 = stream.read_int(7)?;
let complex: ComplexType = stream.read()?;
}
let bytes = vec![
0b1011_0101, 0b0110_1010, 0b1010_1100, 0b1001_1001,
0b1001_1001, 0b1001_1001, 0b1001_1001, 0b1110_0111
];
let buffer = BitReadBuffer::new(bytes, LittleEndian);
let mut stream = BitReadStream::new(buffer);
let value: u8 = stream.read_int(7)?;
let complex: ComplexType = stream.read()?;
```
## License