cm_types

Macro symmetrical_enums

Source
macro_rules! symmetrical_enums {
    ($a:ty , $b:ty, $($id: ident),*) => { ... };
}
Expand description

Generate impl From for two trivial enums with identical values, allowing converting to/from each other. This is useful if you have a FIDL-generated enum and a hand-rolled one that contain the same values.

§Arguments

  • $a, $b - The enums to generate impl From for. Order doesn’t matter because implementation will be generated for both. Enums should be trivial.
  • id - Exhaustive list of all enum values.

§Examples

mod a {
    #[derive(Debug, PartialEq, Eq)]
    pub enum Streetlight {
        Green,
        Yellow,
        Red,
    }
}

mod b {
    #[derive(Debug, PartialEq, Eq)]
    pub enum Streetlight {
        Green,
        Yellow,
        Red,
    }
}

symmetrical_enums!(a::Streetlight, b::Streetlight, Green, Yellow, Red);

assert_eq!(a::Streetlight::Green, b::Streetlight::Green.into());
assert_eq!(b::Streetlight::Green, a::Streetlight::Green.into());