Crates.io | rbitpack |
lib.rs | rbitpack |
version | 0.1.0 |
source | src |
created_at | 2024-08-08 10:17:19.241088 |
updated_at | 2024-08-08 10:17:19.241088 |
description | For packing booleans in variables using bitwise operations |
homepage | |
repository | |
max_upload_size | |
id | 1329380 |
size | 29,038 |
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.
u8
, u16
, u32
, u64
) or a Vec<u64>
.Bitfield
type from the bitval
crate.To use the rbitpack
macro, add it to your struct as follows:
use rbitpack::BitwisePackable;
#[derive(BitwisePackable)]
#[rbitpack(size = "i8", overflow = false)]
struct MyStruct {
field1: bool,
field2: bool,
// Add more fields as needed
}
size
: Specifies the bit size for packing (i8
, i16
, i32
, i64
, or auto
for dynamic sizing).overflow
: Controls whether to panic on overflow (defaults to false
).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
:
[dependencies]
bitval = "0.1"
Then, import Bitfield
in your Rust code:
use bitval::Bitfield;
u8
#[derive(BitwisePackable)]
#[rbitpack(size = "i8")]
struct Example {
a: bool,
b: bool,
c: bool,
}
let example = Example { a: true, b: false, c: true };
let packed = Example::pack(&example);
let unpacked = Example::unpack(packed);
#[derive(BitwisePackable)]
#[rbitpack(size = "auto")]
struct DynamicExample {
x: bool,
y: bool,
z: bool,
}
let example = DynamicExample { x: true, y: false, z: true };
let packed = DynamicExample::pack(&example);
let unpacked = DynamicExample::unpack(packed);
This crate is licensed under the MIT License. See the LICENSE file for details.