Expand description
The slicefields crate provides a zero-cost way of using bit slices
into fields of a struct as if they were real fields
In the following code snippet for example, the struct
Cat
only has one 8-bit field but
bitstruct allows this 8-bit field to be partitioned
into a 1-bit boolean field and a 7-bit integer field
by generating methods which act like getters and setters for such
fields but are instead backed by bit slices of said 8-bit field
extern crate slicefields;
use slicefields::slicefields;
#[slicefields(
has_kittens(bool, data(0..1)),
color(u8, data(1..8))
)]
struct Cat {
data: u8
}
let mut cat = Cat { data: 1 };
assert_eq!(cat.has_kittens(), true);
cat.set_color(4);
For a detailed explanation of the syntax, see the
slicefields
macro.
Note that you do not have to use Rust’s built-in types for this:
Any type that implements the necessary traits for bitwise operations
(such as those from the ux crate which provides non-standard width integers)
can both be used as backing for a slice field as well as be a slice
field itself
Attribute Macros§
- slicefields
- The syntax for the slicefields attribute works as follows:
#[slicefields((options)name(type, backing_field(range_of_bitslice)))]