bit-struct #![no_std]
Bit struct is a crate which allows for ergonomic use of C-like bit fields without mediocre IDE support resulting from proc macros. In addition, everything is statically typed checked!
Take the following example
use *;
enums!
bit_struct!
We can create a new HouseConfig
like such:
let config = new;
where all numbers are statically checked to be in bounds. We can get the
raw u8
which represents config
:
let raw: u8 = config.raw;
or we can get a HouseConfig
from a u8
like
let config: HouseConfig = try_from.unwrap;
We need to unwrap because HouseConfig
is not valid for all numbers. For instance, if the
most significant bits are 0b11
, it encodes an invalid HouseKind
. However,
if all elements of a struct are always valid (suppose we removed the kind
field), the struct will
auto implement a trait which allows calling
let config: HouseConfig = exact_from;
which will never panic. We can access values of config
like so:
// get a value
let kind: HouseKind = config.kind.get;
// set a value
config.lowest_floor.set;
We can also access bit-level numbers as std-lib values like this
let lowest_floor: i3 = config.lowest_floor.get;
let lowest_floor_std: i8 = lowest_floor.value;
Benefits
- No proc macros
- Autocompletion fully works (tested in IntelliJ Rust)
- Fast compile times
- Statically checked bounds (structs cannot be over-filled)
- Statically checked types
Further Documentation
Look at the integration tests in the tests
folder for further insight.