Macro lib_bitfield

Source
macro_rules! lib_bitfield {
    (
        $(#[$doc:meta])+
        $ty_vis:vis $ty:ident ($base:ty): $ret:ty {
            $(
                $(#[$field_doc:meta])*
                $field_vis:vis $field:ident: $msb:expr $(, $lsb:expr)?;
            )+
        }
    ) => { ... };
    (
        $(#[$doc:meta])+
        $ty_vis:vis $ty:ident (MSB0 [$base:ty; $N:expr]): $ret:ty {
            $(
                $(#[$field_doc:meta])*
                $field_vis:vis $field:ident: $msb:expr $(, $lsb:expr)?;
            )+
        }
    ) => { ... };
    (
        $(#[$doc:meta])+
        $bitfield_ty:ident: $base:ty,
        mask: $mask:expr,
        default: $default:expr,
        {
            $(
                $(#[$field_doc:meta])*
                $field:ident: $msb:literal$(, $lsb:literal)?;
            )+
        }$(,)?
    ) => { ... };
    (
        $(#[$doc:meta])+
        $bitfield_ty:ident: $base:ty,
        mask: $mask:expr,
        default: $default:expr,
        {
            $(
                $(#[$field_doc:meta])*
                $field:ident: $field_ty:ident, $msb:expr$(, $lsb:expr)?;
            )+
        }$(,)?
    ) => { ... };
}
Expand description

Macro to define a bitfield struct.

For types using a base integer type:

  • fields access bits in big-endian bit order (MSB = $base::BITS - 1).

For types using a base byte-array type:

  • MSB0 indicates big-endian byte access (Most-Significant-Byte = 0)
  • fields access bits in big-endian bit order (Most-Significant-Bit = $base::BITS - 1)