odin-union
odin-union provides a dependency-free attribute macro for declaring Rust
enums whose variants contain same-named types.
Instead of repeating each type:
write:
use odin_union;
The macro expands the declaration to the ordinary payload enum above. It also
generates From<Person> for Thing and From<Animal> for Thing, so values can
be wrapped with .into():
# use odin_union;
#
#
#
#
let person = Person ;
let thing: Thing = person.into;
match thing
This is declaration shorthand, not a new runtime representation. The result is a normal Rust enum: its tag and active payload are stored together, matching, derives, visibility, and attributes continue to work normally, and no runtime support library is involved.
Rules
- Every listed variant must be a bare unit variant such as
Person. - A same-named type must be in scope at the enum declaration.
- The source enum cannot be generic. The macro cannot infer which generic arguments should be supplied to each same-named payload type.
- A variant-level
#[cfg(...)]attribute is copied to the generatedFromimplementation. - Manually implementing the same
From<T>conversion will conflict with the generated implementation. - Rust does not perform implicit user-defined conversions. Use
.into(),Thing::from(value), orThing::Person(value).
The crate has no dependencies and supports Rust 1.85 or newer.