Derive Macro bondrewd_derive::BitfieldEnum[][src]

#[derive(BitfieldEnum)]
{
    // Attributes available to this derive:
    #[bondrewd_enum]
}
Expand description

Generates an implementation of bondrewd::BitfieldEnum trait.

Features

  • Generates code for the BitfieldEnum trait which allows an enum to be used by Bitfield structs.
  • Literal values. ex. Variant = 0,
  • Automatic Value Assignment for non-literal variants. Variants are assigned values starting from 0 incrementing by 1 skipping values taken by literal definitions (That means you can mix and match inferred values a code defined literal values).
  • Catch Variants
    • Catch Value is a variant that will store values that don’t match the reset of the variants. using a Catch Value is as simple as making a variant with a primitive value (if the bondrewd_enum attribute is present the primitive types must match). ex InvalidVariant(u8),.
    • Catch All variant is used to insure that Results are not needed. Catch all will generate a _ => {..} match arm so that enums don’t need to have as many variants as there are values in the defined primitive. Catch all can be defined with a #[bondrewd_enum(invalid)] attribute or last variant will Automatically become a catch all if no Catch is defined.

Other Features

  • Support for implementation of std::cmp::PartialEq for the given primitive (currently only u8)

Typical Example

use bondrewd::*;
// the primitive type will be assumed to be u8 because there are less than
// 256 variants. also any value above 3 passed into from_primitive will
// will be caught as a Three due to the catch all system.
#[derive(BitfieldEnum, PartialEq, Debug)]
enum SimpleEnum {
    Zero,
    One,
    Two,
    Three,
}

into_primitive Truth Table

Variantoutput
Zero0
One1
Two2
Three3

from_primitive Truth Table

inputVariant
0Zero
1One
2Two
3..=MAXThree

Custom Catch All Example

use bondrewd::*;
// in this case three will no longer be a catch all because one is...
#[derive(BitfieldEnum, PartialEq, Debug)]
enum SimpleEnum {
    Zero,
    #[bondrewd_enum(invalid)]
    One,
    Two,
    Three,
}

into_primitive Truth Table

Variantoutput
Zero0
One1
Two2
Three3

from_primitive Truth Table

inputVariant
0Zero
1One
2Two
3Three
4..=MAXOne

Catch Value Example

use bondrewd::*;
#[derive(BitfieldEnum, PartialEq, Debug)]
enum SimpleEnum {
    Zero,
    One,
    Two,
    Three(u8),
}

into_primitive Truth Table

Variantoutput
Zero0
One1
Two2
Three(value)value

from_primitive Truth Table

inputVariant
0Zero
1One
2Two
3..=MAXThree(input)

Literals Example

use bondrewd::*;
#[derive(BitfieldEnum, PartialEq, Debug)]
enum SimpleEnum {
    Nine = 9,
    // because variant `One` is the first non-literal variant it will be
    // given the first available value
    One,
    // Literals can still be a catch all.
    #[bondrewd_enum(invalid)]
    Zero = 0,
    Five = 5,
    // because variant `One` is the second non-literal variant it will be
    // given the second available value
    Two,
}

into_primitive Truth Table

Variantoutput
Nine9
One1
Zero0
Five5
Two2

from_primitive Truth Table

inputVariant
0Zero
1One
2Two
3 or 4Zero
5Five
6..8Zero
9Nine
10..=MAXZero