poly_enum 0.1.0

Hierarchical polymorphism with enums
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 3 items with examples
  • Size
  • Source code size: 9.12 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.15 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • jesmaz/poly_enum
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jesmaz

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