bitenum

Attribute Macro bitenum 

Source
#[bitenum]
Expand description

Implements the constant into_bits and from_bits functions for the given enum.

§Parameters

  • into: Enable/disable the into_bits function generation.
  • from: Enable/disable the from_bits function generation.
  • all: Enable/disable the all function generation.

For into, from, and all, you can either use booleans (#[bitfield(u8, debug = false)]) or cfg attributes (#[bitfield(u8, debug = cfg(test))]) to enable/disable them.

§Examples

use bitfield_struct::bitenum;

#[bitenum(all = false)]
#[repr(u8)]
#[derive(Debug, PartialEq, Eq)]
enum MyEnum {
    #[fallback]
    A = 0,
    B = 1,
    C = 2,
}

assert_eq!(MyEnum::from_bits(1), MyEnum::B);
assert_eq!(MyEnum::C.into_bits(), 2);

Use the #[fallback] attribute to specify a fallback variant for invalid bit patterns.