macro_rules! open_enum {
(
$(#[$a:meta])*
$v:vis enum $name:ident : $storage:ty {
$(#![$implattr:meta])*
$(
$(#[$vattr:meta])*
$variant:ident = $value:expr,
)*
}
) => { ... };
}
Expand description
This macro creates an underlying struct that behaves like an enum, without
the undefined behavior of trying to match with a value outside of the enum.
The actual object created is a #[repr(transparent)]
struct with a pub
const
value. See comment in example below for an example of the underlying
structure.
This macro implements the following traits: Copy
, Clone
, Debug
, Eq
,
PartialEq
, Hash
, Ord
, PartialOrd
.
An example usage case for this macro is for protocols, when you want to use an enum as a field in a struct that represents a specific type, like u16 or u32. You are also able to convert to/from bytes with this typed enum.
ยงExamples
use open_enum::open_enum;
open_enum! {
#[expect(dead_code)] // This will apply to the generated struct defn
pub enum ExampleEnumName: u32 {
#![expect(missing_docs)] // This will apply to all subfields of the enum
THIS_IS_AN_ENUM = 32,
}
}
// Expands to:
//
// #[repr(transparent)]
// #[expect(dead_code)]
// struct ExampleEnumName(u32);
//
// #[expect(missing_docs)]
// impl ExampleEnumName {
// pub const THIS_IS_AN_ENUM: ExampleEnumName = ExampleEnumName(0)
// }
// To access an element in ExampleEnumName
let example_enum = ExampleEnumName::THIS_IS_AN_ENUM;
assert_eq!(example_enum.0, 32);
let number: u32 = example_enum.0; // enum value is type u32