2 releases
0.1.1 | Jun 28, 2019 |
---|---|
0.1.0 | Jun 28, 2019 |
#236 in #methods
7KB
99 lines
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.).
Example
#[derive(EnumAs)]
enum StringOrNumber {
String(String),
Number(i32),
}
let value = StringOrNumber::Number(123);
assert_eq!(value.as_string(), None);
assert_eq!(value.as_number(), Some(&123));
Dependencies
~2.5MB
~53K SLoC