#enums #derive #variant #is

macro derive_is_enum_variant

Automatically derives is_dog and is_cat methods for enum Pet { Dog, Cat }

2 releases

Uses old Rust 2015

0.1.1 Sep 14, 2017
0.1.0 Sep 14, 2017

#2449 in Rust patterns

Download history 2745/week @ 2024-03-14 2562/week @ 2024-03-21 3030/week @ 2024-03-28 2055/week @ 2024-04-04 3121/week @ 2024-04-11 3201/week @ 2024-04-18 3053/week @ 2024-04-25 2344/week @ 2024-05-02 2099/week @ 2024-05-09 2872/week @ 2024-05-16 2661/week @ 2024-05-23 2785/week @ 2024-05-30 1834/week @ 2024-06-06 3114/week @ 2024-06-13 3064/week @ 2024-06-20 2412/week @ 2024-06-27

10,767 downloads per month
Used in 28 crates (8 directly)

Apache-2.0/MIT

12KB
135 lines

derive_is_enum_variant

derive_is_enum_variant

Build Status

Stop writing pub is_whatever(&self) -> bool for your enums by hand -- it's a pain! Just #[derive(is_enum_variant)] instead!

Usage

Add derive_is_enum_variant to your crate's Cargo.toml:

[dependencies]
derive_is_enum_variant = "<insert-latest-version-here>"

And then add #[derive(is_enum_variant)] to your enum definitions:

#[macro_use]
extern crate derive_is_enum_variant;

#[derive(is_enum_variant)]
pub enum Pet {
    Doggo,
    Kitteh,
}

fn main() {
    let pet = Pet::Doggo;

    assert!(pet.is_doggo());
    assert!(!pet.is_kitteh());
}

Customizing Predicate Names

By default, the predicates are named is_snake_case_of_variant_name. You can use any name you want instead with #[is_enum_variant(name = "..")]:


#[derive(is_enum_variant)]
pub enum Pet {
    #[is_enum_variant(name = "is_real_good_boy")]
    Doggo,
    Kitteh,
}

let pet = Pet::Doggo;
assert!(pet.is_real_good_boy());

Skipping Predicates for Certain Variants

If you don't want to generate a predicate for a certain variant, you can use #[is_enum_variant(skip)]:


#[derive(is_enum_variant)]
pub enum Errors {
    Io(::std::io::Error),

    #[doc(hidden)]
    #[is_enum_variant(skip)]
    __NonExhaustive,
}

License

Licensed under either of

at your option.

Contribution

See CONTRIBUTING.md for hacking!

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

License: Apache-2.0/MIT

Dependencies

~2.5MB
~49K SLoC