#predicate #variant #derive #enum #is

macro enum-methods

Generates methods for each enum variant

8 releases

Uses old Rust 2015

0.0.8 Sep 15, 2017
0.0.7 Aug 18, 2017
0.0.6 Jul 24, 2017

#445 in Procedural macros

Download history 117/week @ 2023-10-16 126/week @ 2023-10-23 113/week @ 2023-10-30 117/week @ 2023-11-06 92/week @ 2023-11-13 116/week @ 2023-11-20 105/week @ 2023-11-27 87/week @ 2023-12-04 80/week @ 2023-12-11 118/week @ 2023-12-18 106/week @ 2023-12-25 49/week @ 2024-01-01 102/week @ 2024-01-08 98/week @ 2024-01-15 96/week @ 2024-01-22 84/week @ 2024-01-29

398 downloads per month

Apache-2.0

25KB
441 lines

enum-methods

Build Status crates.io

Enum getter/is_* method generation.

Please note that this crate is unstable and is subject to change frequently.

I will attempt to prevent seriously breaking changes after we hit 0.1.0.

Links

Usage

In your Cargo.toml, add this line under your [dependencies] section:

enum-methods = "0.0.8"

To use, simply derive and call methods (see the example below).

Why?

Usually when you write an enum with one or zero values, you might want to add a set of getters for them. As such:

#[derive(Debug)]
enum MyEnum {
    FooBarBaz(i64),
    BazBarFoo(String),
    // ... and others
}

impl MyEnum {
    pub fn foo_bar_baz(&self) -> i64 {
        if let &MyEnum::FooBarBaz(i) = self {
            i
        }
        else {
            panic!("called MyEnum::FooBarBaz() on {:?}", self)
        }
    }
    // et cetera
}

But this gets tedious, and adds a lot code for this simple functionality. Enter enum-methods.

Instead of doing the above with the if let ... else { panic!(...) }, you simply derive from the EnumIntoGetters

#[macro_use]
extern crate enum_methods;

#[derive(EnumIntoGetters, EnumAsGetters, EnumIsA, Debug)]
enum MyEnum {
    FooBarBaz(i64),
    BazBarFoo(String),
    // ... and others
}

fn main() {
    let my_foo = MyEnum::FooBarBaz(42);
    // EnumIsA - creates is_* methods for every member
    if my_foo.is_foo_bar_baz() {
        // EnumAsGetters - gets a reference to the enum, panicking if it is
        // not the specified variant
        assert_eq!(*my_foo.as_foo_bar_baz(), 42);
        // EnumIntoGetters - consumes the enum, yielding its owned value,
        // and panicking if it is not the specified variant
        assert_eq!(my_foo.into_foo_bar_baz(), 42);
    }
}

Requirements and gotchas

Right now, enum-methods has four derivable options:

  • EnumAsGetters for generating as_* methods, which return a reference.
  • EnumIntoGetters for generating into_* methods, which consume the enum and returns the data held by the variant.
  • EnumToGetters for generating to_* methods, which returns a clone of the data held by the variant.
  • EnumIsA for generating is_* methods, which return a boolean indicating whether the enum matches that variant.

EnumAsGetters, EnumIntoGetters, and EnumToGetters have some limitations.

  • Any enum variant which has exactly 1 member will have a getter generated for it. All other variants are ignored.
  • Enums which derive from EnumIntoGetters must also derive from Debug - this is for when a method is called for the wrong variant and needs to panic!.

Furthermore, EnumToGetters is only for enums whose variants implement Clone. There is not yet support for th

EnumIsA is much simpler than the previous; it simply adds is_* methods returning a boolean for whether the variant matches or not.

For all generated methods, all names are automatically converted to snake_case.

License

This software is released under the Apache license 2.0. See the LICENSE file for more details.

Dependencies

~1.5MB
~39K SLoC