rbitpack
The rbitpack crate provides a procedural macro for packing and unpacking boolean fields of a struct into various bit sizes. This can be particularly useful for reducing memory usage or performing bit-level operations.
Features
- Efficient Packing and Unpacking: Pack multiple boolean fields into a single integer type (
u8,u16,u32,u64) or aVec<u64>. - Overflow Handling: Control whether an overflow error should be triggered when the number of boolean fields exceeds the available bits.
- Dynamic Bitfield Size: Support for packing into a dynamic bitfield size with the
Bitfieldtype from thebitvalcrate.
Usage
To use the rbitpack macro, add it to your struct as follows:
use BitwisePackable;
Attributes
size: Specifies the bit size for packing (i8,i16,i32,i64, orautofor dynamic sizing).overflow: Controls whether to panic on overflow (defaults tofalse).
Auto Size
If you use auto for the size attribute, you need to install and import the Bitfield type from the bitval crate. Add bitval to your Cargo.toml:
[]
= "0.1"
Then, import Bitfield in your Rust code:
use Bitfield;
Examples
Packing and Unpacking with u8
let example = Example ;
let packed = pack;
let unpacked = unpack;
Using Dynamic Bitfield Size
let example = DynamicExample ;
let packed = pack;
let unpacked = unpack;
License
This crate is licensed under the MIT License. See the LICENSE file for details.