-
Notifications
You must be signed in to change notification settings - Fork 189
riscv-macros: modularize code
#374
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
base: master
Are you sure you want to change the base?
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.
Most the codes are great! but I have a few concerns here! take my reviews with a grain of salt since I am not a official maintainer of this crate... thanks :)
| impl PacEnumItem { | ||
| pub fn new(input: &DeriveInput) -> Self { | ||
| let name = input.ident.clone(); | ||
| let (mut numbers, mut max_number) = (HashMap::new(), 0); | ||
|
|
||
| let variants = match &input.data { | ||
| Data::Enum(data) => &data.variants, | ||
| _ => panic!("Input is not an enum"), | ||
| }; | ||
| for v in variants.iter() { | ||
| let ident = v.ident.clone(); | ||
| let value = match v.discriminant.as_ref() { | ||
| Some((_, syn::Expr::Lit(expr_lit))) => match &expr_lit.lit { | ||
| syn::Lit::Int(lit_int) => { | ||
| lit_int.base10_parse::<usize>().unwrap_or_else(|_| { | ||
| panic!("All variant discriminants must be unsigned integers") | ||
| }) | ||
| } | ||
| _ => panic!("All variant discriminants must be unsigned integers"), | ||
| }, | ||
| None => panic!("Variant must have a discriminant"), | ||
| _ => panic!("All variant discriminants must be literal expressions"), | ||
| }; | ||
|
|
||
| if numbers.insert(value, ident).is_some() { | ||
| panic!("Duplicate discriminant value"); | ||
| } |
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.
Hey! @romancardenas looking at the codes, all of them are mostly great! the only thing that concerns me here is the use of panic!() macro for things like missing discriminants and duplicate discriminant. I feel like it would be better to use something like syn::Error what do you think?
take my reviews with a grain of salt since I am not a actual maintainer of these crate.
Thank you :)
This is part of the work for improving the procedural macros used in this repo.
I reduced to the minimum the amount of code in
riscv-macros/src/lib.rs. The idea is leaving there only the public macros, and all the auxiliary code will be in clean modules.Currently,
riscv-macrosonly contain thepac_enummacro, which works in tandem with theriscvcrate. Thus, the auxiliary code has been moved to theriscvmodule.If the
rtfeature is active,pac_enummay also produce code required byriscv-rt. This additional code is now isolated in theriscv::rtmodule.Next steps will be moving code from
riscv-rt/macrostoriscv-macros, under a newriscv_rtmodule.