| Crates.io | poly_enum |
| lib.rs | poly_enum |
| version | 0.1.0 |
| created_at | 2025-06-03 06:52:38.465395+00 |
| updated_at | 2025-06-03 06:52:38.465395+00 |
| description | Hierarchical polymorphism with enums |
| homepage | |
| repository | https://github.com/jesmaz/poly_enum |
| max_upload_size | |
| id | 1698693 |
| size | 9,622 |
Hierarchical polymorphism with enums
This crate provides an easy way to create complex polymorphic hierarchies powered by enums.
use poly_enum::PolyEnum;
#[derive(PolyEnum)]
enum Elements {
#[poly_enum(NonMetal)]
Carbon,
#[poly_enum(Oxidizer, NonMetal)]
Florine,
#[poly_enum(Metal)]
Iron,
#[poly_enum(Alkali, Metal)]
Lithium,
#[poly_enum(Oxidizer, NonMetal)]
Oxygen,
#[poly_enum(Alkali, Metal)]
Sodium,
}
impl Alkali {
fn explodes_in_water(&self) -> bool {true}
}
let metal = Metal::Sodium;
let alkali: Alkali = metal.cast().unwrap();
assert!(alkali.explodes_in_water());
This will generate 4 enums in addition to the base Elements enum: NonMetal, Oxidizer, Metal, and Alkali.
enum Alkali {
Sodium,
Lithium,
}
enum Metal {
Iron,
Lithium,
Sodium,
}
enum NonMetal {
Carbon,
Oxygen,
Florine,
}
enum Oxidizer {
Oxygen,
Florine,
}
In addition it will implement PolyEnum to allow casting between these enums.