-
Notifications
You must be signed in to change notification settings - Fork 1
Added the ability to use #[default] and added Default in the derive trait #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,6 +187,25 @@ fn concat<T>(mut v1: Vec<T>, mut v2: Vec<T>) -> Vec<T> { | |
v1 | ||
} | ||
|
||
fn check_for_default(triples: &mut Vec<(TokenStream, Ident, TokenTree)>) { | ||
let mut default_position: Option<usize> = None; | ||
for (attributes, _variant_name, variant_value) in triples.into_iter() { | ||
if attributes.to_string().contains("default") { | ||
if default_position.is_some() { | ||
// error!("Multiple variants marked as default"); | ||
} | ||
default_position = Some(variant_value.to_string().parse::<usize>().unwrap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do a return here instead. And if you do, you won't need to do |
||
} | ||
} | ||
if !default_position.is_some() { | ||
// No default specified, so we'll just use the first variant | ||
triples[0].0.extend(vec![ | ||
punct_token('#'), | ||
bracket_token(vec![ident_token("default")]).into(), | ||
]); | ||
} | ||
} | ||
|
||
#[proc_macro] | ||
pub fn primitive_enum(tokens: TokenStream) -> TokenStream { | ||
let mut iter = tokens.into_iter(); | ||
|
@@ -302,6 +321,7 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream { | |
offset += 1; | ||
triples.push((variant_attributes, variant_name, value)); | ||
} | ||
check_for_default(&mut triples); // make sure there's a default, if the user didn't specify one | ||
triples | ||
}; | ||
|
||
|
@@ -322,7 +342,6 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream { | |
ident_token("repr"), | ||
paren_token(repr_type.clone()), | ||
])); | ||
|
||
// #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
out.push(punct_token('#')); | ||
out.push(bracket_token(vec![ | ||
|
@@ -339,8 +358,11 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream { | |
ident_token("Eq"), | ||
punct_token(','), | ||
ident_token("Hash"), | ||
punct_token(','), | ||
ident_token("Default"), | ||
]), | ||
])); | ||
|
||
out.push(ident_token("pub")); | ||
out.push(ident_token("enum")); | ||
out.push(TokenTree::Ident(enum_identifier.clone())); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did you mean to get rid of this if statement? I'm not 100% sure, but I would assume that
derive(Default)
itself will trigger an error if it sees multiple defaults.Also it looks like we don't actually need to do anything with the default variant, so you probably don't need the
default_position
variable.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.
So, um, it kinda triggers a error
Uh oh!
There was an error while loading. Please reload this page.
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.
Got it. Looks like someone opened a pr to fix the error message here in response to the issue you opened.
Still, I suppose I'd prefer it if we propagated the error, because even after that pr lands, I'm assuming it might take a little while for that fix to be in a release.
Also, I just realized, as you probably noticed, we can't just use the
error!
macro here because your function doesn't return a TokenStream. I think the "correct" thing to do here would be to returnResult<(), String>
fromcheck_for_default
and check for an error when you call it, then callerror!
from there.