Expand description
This crate provides a #[bitfield]
attribute macro for defining structs in
a packed binary representation that supports access to ranges of bits.
We conceptualize one of these structs as a sequence of bits 0..N. The bits
are grouped into fields in the order specified by a struct written by the
caller. The #[bitfield]
attribute rewrites the caller’s struct into a
private byte array representation with public getter and setter methods for
each field.
Byte order: note that we consider the bit i
to be the i % 8
’th least
significant bit in the i / 8
’th byte of the struct.
The total number of bits N is required to be a multiple of 8 (this is checked at compile time).
§Examples
The following invocation builds a struct with a total size of 32 bits or 4
bytes. It places field a
in the least significant bit of the first byte,
field b
in the next three least significant bits, field c
in the
remaining four most significant bits of the first byte, and field d
spanning the next three bytes. The least significant byte of d
will be
held in the second byte of our struct, adjacent to the byte holding the
first three fields.
use bit_field::*;
#[bitfield]
pub struct MyFourBytes {
a: B1,
b: B3,
c: B4,
d: B24,
}
less significant
/ more significant
/ /
(first byte) (second byte) / (third) / (fourth byte)
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
| \ / \_ _/ \_______________________ _______________________/
a b c less significant d more significant
The code emitted by the #[bitfield]
macro for this struct is as follows.
Note that the field getters and setters use whichever of u8
, u16
, u32
,
u64
is the smallest while being at least as large as the number of bits in
the field.
impl MyFourBytes {
// Initializes all fields to 0.
pub fn new() -> Self;
// Field getters and setters:
pub fn get_a(&self) -> u8;
pub fn set_a(&mut self, val: u8);
pub fn get_b(&self) -> u8;
pub fn set_b(&mut self, val: u8);
pub fn get_c(&self) -> u8;
pub fn set_c(&mut self, val: u8);
pub fn get_d(&self) -> u32;
pub fn set_d(&mut self, val: u32);
// Bit-level accessors:
pub fn get_bit(&self, offset: usize) -> bool;
pub fn set_bit(&mut self, offset: usize, val: bool);
pub fn get(&self, offset: usize, width: u8) -> u64;
pub fn set(&mut self, offset: usize, width: u8, val: u64);
}
§Bit field specifier types
Field types may be specified as B1 through B64, or alternatively as BitField1 through BitField64 in code that benefits from the clarification.
Fields may also be specified as bool
, which is laid out equivalently to
B1
but with accessors that use bool
rather than u8
.
use bit_field::*;
#[bitfield]
pub struct MyFourBytes {
a: bool,
b: B3,
c: B4,
d: B24,
}
Fields may be user-defined single element tuple struct with primitive types. Use must specify
the width with #[bits = N]
. This should be used to improve type safety.
use bit_field::*;
#[bitfield]
#[bits = 60]
struct AddressField(u64);
impl AddressField {
pub fn new(addr: u64) -> AddressField {
AddressField(addr >> 4)
}
pub fn get_addr(&self) -> u64 {
self.0 << 4
}
}
Finally, fields may be of user-defined enum types. The enum must satisfy one of the following requirements.
The enum has #[bits = N]
attributes with it. N
will be the width of the field. The getter
function of this enum field will return Result<EnumType, u64>
. Raw value that does not match
any variant will result in an Err(u64)
.
use bit_field::*;
#[bitfield]
#[bits = 2]
#[derive(Debug, PartialEq)]
enum TwoBits {
Zero = 0b00,
One = 0b01,
Three = 0b11,
}
#[bitfield]
struct Struct {
prefix: BitField1,
two_bits: TwoBits,
suffix: BitField5,
}
The enum has a number of variants which is a power of 2 and the discriminant values (explicit or implicit) are 0 through (2^n)-1. In this case the generated getter and setter are defined in terms of the given enum type.
use bit_field::*;
#[bitfield]
#[derive(Debug, PartialEq)]
enum TwoBits {
Zero = 0b00,
One = 0b01,
Two = 0b10,
Three = 0b11,
}
#[bitfield]
struct Struct {
prefix: BitField1,
two_bits: TwoBits,
suffix: BitField5,
}
An optional #[bits = N]
attribute may be used to document the number of
bits in any field. This is intended for fields of enum type whose name does
not clearly indicate the number of bits. The attribute is optional but helps
make it possible to read off the field sizes directly from the definition of
a bitfield struct.
use bit_field::*;
#[bitfield]
#[derive(Debug, PartialEq)]
enum WhoKnows {
Zero = 0b00,
One = 0b01,
Two = 0b10,
Three = 0b11,
}
#[bitfield]
struct Struct {
prefix: BitField1,
#[bits = 2]
two_bits: WhoKnows,
suffix: BitField5,
}
§Derives
Derives may be specified and are applied to the data structure post rewriting by the macro.
use bit_field::*;
#[bitfield]
#[derive(Copy, Clone)]
pub struct ExampleWithDerives {
car: B4,
cdr: B4,
}
§Compile time checks
If the total size is not a multiple of 8 bits, you will receive an error message at compile time mentioning:
the trait
bit_field::checks::TotalSizeIsMultipleOfEightBits
is not implemented
use bit_field::*;
#[bitfield]
pub struct Broken {
field_a: B1,
field_b: B3,
field_c: B6,
}
If a bitfield enum has discriminants that are outside the range 0 through (2^n)-1, it will be caught at compile time.
use bit_field::*;
#[bitfield]
enum Broken {
Zero = 0b00,
One = 0b01,
Two = 0b10,
Nine = 0b1001, // error
}
If the value provided in a #[bits = N] attribute does not match the real number of bits in that field, it will be caught.
use bit_field::*;
#[bitfield]
#[derive(Debug, PartialEq)]
enum OneBit {
No = 0,
Yes = 1,
}
#[bitfield]
struct Struct {
#[bits = 4] // error
two_bits: OneBit,
padding: BitField7,
}
Re-exports§
pub use self::BitField0 as B0;
pub use self::BitField1 as B1;
pub use self::BitField2 as B2;
pub use self::BitField3 as B3;
pub use self::BitField4 as B4;
pub use self::BitField5 as B5;
pub use self::BitField6 as B6;
pub use self::BitField7 as B7;
pub use self::BitField8 as B8;
pub use self::BitField9 as B9;
pub use self::BitField10 as B10;
pub use self::BitField11 as B11;
pub use self::BitField12 as B12;
pub use self::BitField13 as B13;
pub use self::BitField14 as B14;
pub use self::BitField15 as B15;
pub use self::BitField16 as B16;
pub use self::BitField17 as B17;
pub use self::BitField18 as B18;
pub use self::BitField19 as B19;
pub use self::BitField20 as B20;
pub use self::BitField21 as B21;
pub use self::BitField22 as B22;
pub use self::BitField23 as B23;
pub use self::BitField24 as B24;
pub use self::BitField25 as B25;
pub use self::BitField26 as B26;
pub use self::BitField27 as B27;
pub use self::BitField28 as B28;
pub use self::BitField29 as B29;
pub use self::BitField30 as B30;
pub use self::BitField31 as B31;
pub use self::BitField32 as B32;
pub use self::BitField33 as B33;
pub use self::BitField34 as B34;
pub use self::BitField35 as B35;
pub use self::BitField36 as B36;
pub use self::BitField37 as B37;
pub use self::BitField38 as B38;
pub use self::BitField39 as B39;
pub use self::BitField40 as B40;
pub use self::BitField41 as B41;
pub use self::BitField42 as B42;
pub use self::BitField43 as B43;
pub use self::BitField44 as B44;
pub use self::BitField45 as B45;
pub use self::BitField46 as B46;
pub use self::BitField47 as B47;
pub use self::BitField48 as B48;
pub use self::BitField49 as B49;
pub use self::BitField50 as B50;
pub use self::BitField51 as B51;
pub use self::BitField52 as B52;
pub use self::BitField53 as B53;
pub use self::BitField54 as B54;
pub use self::BitField55 as B55;
pub use self::BitField56 as B56;
pub use self::BitField57 as B57;
pub use self::BitField58 as B58;
pub use self::BitField59 as B59;
pub use self::BitField60 as B60;
pub use self::BitField61 as B61;
pub use self::BitField62 as B62;
pub use self::BitField63 as B63;
pub use self::BitField64 as B64;
Modules§
- checks 🔒
Structs§
- Error type for bit field get.
Attribute Macros§
- The function that derives the actual implementation.