-
Notifications
You must be signed in to change notification settings - Fork 34
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
Conversation
There was a problem hiding this 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 ✅
num_enum_derive/src/lib.rs
Outdated
NumEnumVariantAttributeItem::Alternatives(alternatives) => { | ||
attr_spans.alternatives.push(alternatives.span()); | ||
alternative_values | ||
.extend(alternatives.expressions.iter().cloned()); |
There was a problem hiding this comment.
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()
🙂
num_enum_derive/src/lib.rs
Outdated
let enum_info = parse_macro_input!(stream as EnumInfo); | ||
|
||
let default_ident: Ident = match enum_info.default() { | ||
Some(ident) => ident.clone(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some(ident) => ident.clone(), | |
Some(ident) => ident, |
|
||
TokenStream::from(quote! { | ||
impl ::core::default::Default for #name { | ||
fn default() -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)]`.
f4fabf9
to
deb11d4
Compare
rust-lang/rust#87517 started standardising
#[default]
as an attribute on enum variants, support it compatiblyidentically to
#[num_enum(default)]
.Also, allow deriving
::core::default::Default
with thenum_enum::Default
derive untilderive_default_enum
stabilises.Fixes #56