Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum name getters #11

Open
alekratz opened this issue Jul 25, 2017 · 0 comments
Open

Enum name getters #11

alekratz opened this issue Jul 25, 2017 · 0 comments

Comments

@alekratz
Copy link
Owner

One problem we have with a lot of the getters is that they require derive(Debug), which is all well and good until you start working with weird types that aren't Debug (e.g. function pointers). Since Debug is only used to print out the name of the variant in the event of a panic, and not the data, I think it makes the most sense to generate methods which return a &'static str of its name. Then, Enum{As,Into,To}Getters would just use that generated name method to display in the panic!.

For example,

#[derive(EnumNames)]
enum MyEnum {
    Foo(i32),
    Bar(i64),
    Baz(String),
}

// generates
impl MyEnum {
    fn enum_names(&self) -> &'static str {
        match &self {
            &MyEnum::Foo(_) -> "Foo",
            &MyEnum::Bar(_) -> "Bar",
            &MyEnum::Baz(_) -> "Baz",
        }
    }
}

Gotchas:

  • This would require exactly one instance of enum_name() to be generated. It may be difficult to keep track of when we're mixing and matching Enum*Getters because they are not aware of each other.
  • The above means we probably can't automatically derive from it, and it would become some extra boilerplate for users of the library to include, which I'm not a big fan of.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant