enum_is
Procedural macro that generates is_* predicate methods for enum variants.
With #[derive(EnumIs)], every variant of your enum gets an is_<variant>()
method (in snake_case) that returns true when self matches that variant.
Works with unit, tuple, and struct variants.
Example
use EnumIs;
Generated methods look like:
Installation
In your Cargo.toml:
[]
= "0.1"
Then:
use EnumIs;
Supported enums
EnumIs works with:
-
Unit variants
let s = Ok; assert!; assert!; -
Tuple variants
let v = Pair; assert!; assert!; -
Struct variants
let m = Data ; assert!; assert!;
Naming rules
For each variant, EnumIs generates a method:
-
Method name:
is_<variant_name_in_snake_case> -
Example mappings:
Variant name Method name Fastis_fast()PostOnlyis_post_only()CPUis_cpu()HTTPRequestErroris_http_request_error()
The methods take &self and return bool.
Internally, the macro uses matches! with:
Self::Variantfor unit variantsSelf::Variant(..)for tuple variantsSelf::Variant { .. }for struct variants
Limitations
#[derive(EnumIs)]must be used on enums only.
Using it on a struct or union will produce a compile error.- Method names are generated purely from the variant identifiers; there is no customization (attributes, renames, ignores) yet.