Macro bitfield::bitfield [] [src]

macro_rules! bitfield {
    ($(#[$attribute:meta])* pub struct $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* struct $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident([$t:ty]); impl Debug; $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident([$t:ty]); $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident(MSB0 [$t:ty]); impl Debug;
     $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident(MSB0 [$t:ty]); $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident($t:ty); impl Debug; no default BitRange; $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident($t:ty); no default BitRange; impl Debug; $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident($t:ty); impl Debug; $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident($t:ty); no default BitRange; $($rest:tt)*) => { ... };
    ($(#[$attribute:meta])* ($($vis:tt)*) struct $name:ident($t:ty); $($rest:tt)*) => { ... };
}

Combines bitfield_struct and bitfield_fields.

The syntax of this macro is the syntax of bitfield_struct, a semicolon, and then the syntax of bitfield_fields.

If you put no default BitRange after the first semicolon, no implementation of BitRange will be generated.

If you put impl Debug; an implementation of fmt::Debug will be generated with the bitfield_debug macro.

The difference with calling those macros separately is that bitfield_fields is called from an appropriate impl block. If you use the non-slice form of bitfield_struct, the default type for bitfield_fields will be set to the wrapped fields.

See the documentation of these macros more information on their respective syntax.

Example

bitfield!{
  pub struct BitField1(u16);
  impl Debug;
  // The fields default to u16
  field1, set_field1: 10, 0;
  pub field2, _ : 12, 3;
}

or with a custom BitRange implementation :

bitfield!{
  pub struct BitField1(u16);
  no default BitRange;
  impl Debug;
  u8;
  field1, set_field1: 10, 0;
  pub field2, _ : 12, 3;
}
impl BitRange<u8> for BitField1 {
    fn bit_range(&self, msb: usize, lsb: usize) -> u8 {
        let width = msb - lsb + 1;
        let mask = (1 << width) - 1;
        ((self.0 >> lsb) & mask) as u8
    }
    fn set_bit_range(&mut self, msb: usize, lsb: usize, value: u8) {
        self.0 = (value as u16) << lsb;
    }
}