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

Allow deriving Default, and using #[default] variants #57

Merged
merged 3 commits into from
Aug 16, 2021

Conversation

illicitonion
Copy link
Owner

rust-lang/rust#87517 started standardising
#[default] as an attribute on enum variants, support it compatibly
identically to #[num_enum(default)].

Also, allow deriving ::core::default::Default with the num_enum::Default derive until derive_default_enum stabilises.

Fixes #56

Copy link
Collaborator

@danielhenrymantilla danielhenrymantilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job, prefixing $(::)? num_enum :: to the Default derive yields a very slick and intuitive API! LGTM ✅

NumEnumVariantAttributeItem::Alternatives(alternatives) => {
attr_spans.alternatives.push(alternatives.span());
alternative_values
.extend(alternatives.expressions.iter().cloned());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential micro-optimization: I suspect the .iter() at line 280 can be removed to iterate by value so that here we can also directly yield full ownership of alternatives.expressions to .extend() 🙂

let enum_info = parse_macro_input!(stream as EnumInfo);

let default_ident: Ident = match enum_info.default() {
Some(ident) => ident.clone(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Some(ident) => ident.clone(),
Some(ident) => ident,


TokenStream::from(quote! {
impl ::core::default::Default for #name {
fn default() -> Self {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn default() -> Self {
#[inline]
fn default() -> Self {

rust-lang/rust#87517 started standardising
`#[default]` as an attribute on enum variants, support it compatibly
identically to `#[num_enum(default)]`.
@illicitonion illicitonion merged commit 00cb48c into main Aug 16, 2021
@illicitonion illicitonion deleted the standard-default branch August 16, 2021 23:33
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.

How aboyt impl Default when use #[num_enum(default)]?
2 participants