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

Const enum variants names #75

Merged
merged 7 commits into from Dec 12, 2019
Merged

Const enum variants names #75

merged 7 commits into from Dec 12, 2019

Conversation

WaffleLapkin
Copy link
Contributor

@WaffleLapkin WaffleLapkin commented Nov 6, 2019

I tried to write code like this:

pub const LETTERS: &'static [&'static str] = Letter::variants();

#[derive(EnumVariantNames)]
pub enum Letter { A, B, C }

But I've found that variants fn is not const.

This pr changes the behaviour of EnumVariantNames in that way:

  1. Add pub const VARIANTS: &'static [&'static str] = ... to enum
  2. make variants fn const
  3. deprecate variants fn with a message like "Use `Letter::VARIANTS` instead"

@hobofan
Copy link

hobofan commented Nov 22, 2019

deprecate variants fn with a message like "Use Letter::VARIANTS instead"

Why deprecate the function if you made the access via the function const too?

@WaffleLapkin
Copy link
Contributor Author

Because I see no reasons to use the function, when you can use associated constant.

@Peternator7
Copy link
Owner

Thanks for the PR! Sorry I thought I responded to this last week, but it looks like I forgot to. Based on #74, I don't think we should deprecate the function because it will be extracted into a trait, and those can't be const yet.

Let's definitely get the associated constant added though :)

@WaffleLapkin
Copy link
Contributor Author

Based on #74, I don't think we should deprecate the function because it will be extracted into a trait, and those can't be const yet.

So I must undeprecate variants and make it non-const again? Ok, I'll make these changes tomorrow :)

@Peternator7
Copy link
Owner

Thanks! Merging #74 created a conflict so here's the resolution you'll want in the enum variants file.

    quote! {
        impl #impl_generics ::strum::VariantNames for #name #ty_generics #where_clause {
            /// Return a slice containing the names of the variants of this enum
            #[allow(dead_code)]
            fn variants() -> &'static [&'static str] {
                Self::VARIANTS
            }
        }

        impl #impl_generics #name #ty_generics #where_clause {
            /// Names of the variants of this enum
            #[allow(dead_code)]
            pub const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
        }
    }

@WaffleLapkin
Copy link
Contributor Author

@Peternator7 shouldn't I add VARIANTS const to the trait?

@Peternator7
Copy link
Owner

@WaffleLapkin, that would be preferred, but if I remember correctly, const in traits isn't supported on stable yet :(

@WaffleLapkin
Copy link
Contributor Author

@Peternator7 const in traits is supported on stable

@WaffleLapkin
Copy link
Contributor Author

@Peternator7 I've added VARIANTS const to VariantNames trait and it seems like there is still no need in variants function... Am I wrong?

@WaffleLapkin
Copy link
Contributor Author

@Peternator7 ping

@Peternator7
Copy link
Owner

Thanks for the ping; I agree. Let's go ahead and remove the variants function

@Peternator7 Peternator7 merged commit eea32db into Peternator7:master Dec 12, 2019
@Peternator7
Copy link
Owner

Looks great! Thanks for the feature :)

@WaffleLapkin
Copy link
Contributor Author

Yay! Btw, don't forget that these changes are breaking now ;)

@WaffleLapkin WaffleLapkin deleted the enum_variant_names_const branch December 12, 2019 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants