ubits
Bit fields and masks for rust!
Provides a macro for generating bit field types
complete with flags and some helpful trait implementations.
Supports field widths of 8, 16, 32, 64 and 128 bits.
- [
bitfield] - macro documentation. - [
examples] - examples of the output generated by the [bitfield] macro.
Note: the example module is only complied with documentation builds and is not available for importing in the wild.
Usage
Generate a bitfield struct with a flag enum... (The following examples all use this as a definition.)
use bitfield;
bitfield!
Instances
From integer:
let from_integer = ExampleField;
assert_eq!;
From a binary string:
let from_binary = from_binary_str;
assert_eq!
From ones:
let from_ones = ones;
assert_eq!;
assert_eq!;
From zeros:
let from_zeros = zeros;
assert_eq!;
assert_eq!;
Field Access
Get bit value by field:
let field = from_binary_str;
assert!;
assert!;
Get bit value by index:
let field = from_binary_str;
assert!;
assert!;
Set bit value by field:
let mut field = from_binary_str;
field.set;
field.set;
assert_eq!;
Set bit value by index:
let mut field = from_binary_str;
field.set_index;
field.set_index;
assert_eq!;
Clear bit value by field:
let mut field = from_binary_str;
field.clear;
field.clear;
assert_eq!;
Clear bit value by index:
let mut field = from_binary_str;
field.clear_index;
field.clear_index;
assert_eq!;
Toggle bit value by field:
let mut field = from_binary_str;
field.toggle;
assert_eq!;
field.toggle;
assert_eq!;
Toggle bit value by index:
let mut field = from_binary_str;
field.toggle_index;
assert_eq!;
field.toggle_index;
assert_eq!;
Named Getters and Setters
Ubits can generate getter and setter methods for zero or more fields, if a name is provided.
use bitfield;
bitfield!
let mut field = from_binary_str;
assert_eq!;
assert_eq!;
field.set_field_1;
assert_eq!;
field.clear_field_1;
assert_eq!;
field.toggle_field_1;
assert_eq!;
Combinations
Combine bit fields:
(use into_combined to consume self)
let mut a = from_binary_str;
let b = from_binary_str;
assert_eq!;
Get the intersection of two bitfields:
(use into_intersection to consume self)
let mut a = from_binary_str;
let b = from_binary_str;
assert_eq!;
Get the diff of two bitfields:
(use into_diff to consume self)
let mut a = from_binary_str;
let b = from_binary_str;
assert_eq!;
Bitwise
Both bit field instances and flags use bitwise operators to change bit values.
let mut from_zeros = zeros;
assert_eq!;
// set bit to 1
from_zeros |= Flag1;
assert_eq!;
// set bit back to 0
from_zeros &= Flag1;
assert_eq!;
// toggle a bit
from_zeros ^= Flag1;
assert_eq!;
from_zeros ^= Flag1;
assert_eq!;
Operations can also be chained together:
let mut from_zeros = zeros | Flag1 | Flag3;
assert_eq!;
Bitfield instances can also be created from combining flags:
let mut from_zeros = Flag1 | Flag3;
assert_eq!;
Fields named with flags
The generated flags enum allows you to access bits by name.
The flag has an associated [u8] value,
which determines the index its target bit.
(See [bitfield] for more info)
With the following input...
1 0 1 0 0 1 1 0
and the following flags...
0 : f1
1 : f1
2 : f2
3 : f3
4 : f4
5 : f5
6 : f6
7 : f7
we end up with this layout.
| name | f7 | f6 | f5 | f4 | f3 | f2 | f1 | f0 |
|---|---|---|---|---|---|---|---|---|
| bit value | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
| index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
With the same input and only the first few flags:
0 : f0
1 : f1
2 : f2
we end up with this layout.
| name | f2 | f1 | f0 | |||||
|---|---|---|---|---|---|---|---|---|
| bit value | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
| index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
Using the same input, but with dispersed flags:
1 : f0
3 : f1
6 : f2
we end up with this layout.
| name | f2 | f1 | f0 | |||||
|---|---|---|---|---|---|---|---|---|
| bit value | 1 | 0 | 1 | 0 | 0 | 1 | 1 | 0 |
| index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |