macro_rules! newtype_enum { ( $(#[$type_attrs:meta])* $visibility:vis enum $type:ident : $base_integer:ty => $(#[$impl_attrs:meta])* { $( $(#[$variant_attrs:meta])* $variant:ident = $value:expr, )* } ) => { ... }; }
Expand description
Interface a C-style enum as an integer newtype.
This macro implements Debug for you, the way you would expect it to work on Rust enums (printing the variant name instead of its integer value). It also derives Clone, Copy, Eq, PartialEq, Ord, PartialOrd, and Hash, since that always makes sense for C-style enums. If you want anything else to be derived, you can ask for it by adding extra derives as shown in the example below.
One minor annoyance is that since variants will be translated into associated constants in a separate impl block, you need to discriminate which attributes should go on the type and which should go on the impl block. The latter should go on the right-hand side of the arrow operator.
Usage example:
newtype_enum! {
#[derive(Default)]
pub enum UnixBool: i32 => #[allow(missing_docs)] {
FALSE = 0,
TRUE = 1,
/// Nobody expects the Unix inquisition!
FILE_NOT_FOUND = -1,
}}