macro_rules! serializable_enum {
(
$(#[$enum_attr:meta])*
$enum_visibility:vis enum $enum_name:ident : $representation:ty {
$(
$(#[$field_attr:meta])*
$field_name:ident => $field_value:expr
),*
}
) => { ... };
}
Expand description
Generate a serializable enum.
An Unknown field is always generated, which contains a variable, which type is the enum representation.
use macro_bits::serializable_enum;
serializable_enum! {
#[derive(Debug, PartialEq)]
pub enum ABC: u8 {
/// This is a doc comment too.
A => 0,
B => 1,
C => 2
}
}
assert_eq!(ABC::from_bits(2), ABC::C);
assert_eq!(ABC::from_bits(3), ABC::Unknown(3));
assert_eq!((ABC::C).into_bits(), 2);