Macro macro_bits::bitfield

source ·
macro_rules! bitfield {
    (
        $(#[$attr:meta])*
        $struct_vis:vis struct $struct_name:ident : $struct_representation:ty {
            $(
                $(#[$field_attr:meta])*
                $field_vis:vis $field_name:ident : $field_type:ty => $field_mask:expr
            ),*
        }
    ) => { ... };
}
Expand description

Deconstruct a bitfield.

The parameters specified get compile checked through assertions. Custom types are allowed, as long as they implement conversions from and into the representation specified on the struct.

use macro_bits::bitfield;

bitfield! {
    #[derive(Debug, PartialEq)]
    pub struct Test: u8 {
        /// This is a doc comment.
        pub x: u8 => 0b0000_1111,
        pub y: bool => 0b0001_0000,
        pub z: u16 => 0b1110_0000
    }
}
let target = Test {
    x: 0xf,
    y: true,
    z: 7
};
assert_eq!(target, Test::from_bits(0xff));
assert_eq!(u8::from(target), 0xff);