| Continuous Integration | Documentation | Crates.io |
|---|---|---|
#[derive(EnumTag)]
This crate provides a proc. macro to derive the EnumTag trait for the given Rust enum.
The #derive(EnumTag) proc. macro only works on Rust enum types and generates both
- a C-like
enumtype with the same variants as the input Rustenumbut without all the associated data. - a derived implementation of the
EnumTagtrait for the Rustenum
The derived EnumTag trait makes it possible to create instances of the generated
C-like enum type as well as link to its definition via <RustEnum as EnumTag>::Tag.
When is this useful?
This is mostly useful for crates that profit from having a distinct enum tag type
while at the same time hosting Rust enum types with lots of variants which would
make it burdensome to maintain the mirroring between both enum type and enum tag type.
The motivation for this crate was a Wasm interpreter that represents its instructions
as an enum but also wants to access the opcodes of the instructions without their data.
In this example the opcodes are the instruction enum tag.
Example
use EnumTag;
// Rust needs this for `B = 42`
/// This is how we can access the generated C-like enum type and name it.
type FooTag = Tag;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
The above #[derive(EnumTag)] generates the following Rust code:
const _: = ;