alekratz / enum-methods

Method generation for enumerated types in Rust
Apache License 2.0
12 stars 3 forks source link

Exclusion attributes #10

Open alekratz opened 7 years ago

alekratz commented 7 years ago

Sometimes it is not desirable to generate a method for a variant of an enum. Presently, there is no way to skip over some specific element. I think we should add a per-element attribute that tells a given derive to skip that element. For example:

struct MyType;

#[derive(EnumToGetters, EnumAsGetters)]
enum MyEnum {
    #[enum_as_getters(skip)]
    Foo(i32), // EnumAsGetters will return a reference to this; we we override `as_foo()` below
    #[enum_to_getters(skip)]
    Bar(MyType), // MyType is not `Clone`, so EnumToGetters would fail to compile on this
}

impl MyEnum {
    pub fn as_foo(&self) -> i32 {
        if let &MyEnum::Foo(i) = self {
            *i
        }
        else { panic!( ... ) }
    }
}

This is also similar to how Serde works.

alekratz commented 7 years ago

Some attributes that could be useful: