2 releases

Uses new Rust 2024

0.1.1 Jun 5, 2025
0.1.0 Jun 3, 2025

#6 in #proc-macro-helper

Download history 203/week @ 2025-05-31 32/week @ 2025-06-07 1/week @ 2025-06-14

55 downloads per month
Used in poly_enum

Apache-2.0

15KB
311 lines

Hierarchical polymorphism with enums

This crate provides an easy way to create complex polymorphic hierarchies powered by enums.

Examples

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.

Limitations

  • No dynamic dispatch other than the match expression
  • Separate hierarchies are not cross compatible

Dependencies

~170–580KB
~14K SLoC