[][src]Crate enum_as_derive

The EnumAs derive macros automatically generates methods on an enum to conditionally get a variant.

For any newtype variant Variant(T), the following methods are generated:

  • fn as_{variant}(&self) -> Option<&T>
  • fn as_{variant}_mut(&mut self) -> Option<&mut T>
  • fn into_{variant}(self) -> Option<T>

In addition, the following methods are generated for all variants:

  • fn is_variant(&self) -> bool

The name is generated by converting the variant name into snake case (e.g. MyVariant gets methods is_my_variant, as_my_variant etc.).


enum StringOrNumber {

let value = StringOrNumber::Number(123);

assert_eq!(value.as_string(), None);
assert_eq!(value.as_number(), Some(&123));

Derive Macros


Generates methods on an enum to conditinally retrieve variants.