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.
- 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
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
Variant | output |
---|---|
Zero | 0 |
One | 1 |
Two | 2 |
Three | 3 |
from_primitive Truth Table
input | Variant |
---|---|
0 | Zero |
1 | One |
2 | Two |
3..=MAX | Three |
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
Variant | output |
---|---|
Zero | 0 |
One | 1 |
Two | 2 |
Three | 3 |
from_primitive Truth Table
input | Variant |
---|---|
0 | Zero |
1 | One |
2 | Two |
3 | Three |
4..=MAX | One |
Catch Value Example
use bondrewd::*;
#[derive(BitfieldEnum, PartialEq, Debug)]
enum SimpleEnum {
Zero,
One,
Two,
Three(u8),
}
into_primitive Truth Table
Variant | output |
---|---|
Zero | 0 |
One | 1 |
Two | 2 |
Three(value) | value |
from_primitive Truth Table
input | Variant |
---|---|
0 | Zero |
1 | One |
2 | Two |
3..=MAX | Three(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
Variant | output |
---|---|
Nine | 9 |
One | 1 |
Zero | 0 |
Five | 5 |
Two | 2 |
from_primitive Truth Table
input | Variant |
---|---|
0 | Zero |
1 | One |
2 | Two |
3 or 4 | Zero |
5 | Five |
6..8 | Zero |
9 | Nine |
10..=MAX | Zero |