-
Notifications
You must be signed in to change notification settings - Fork 1
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
refactor derive - oliver #59
Conversation
…n into refactor-derive
This looks good! If you find something else that's duplicated like this can you de-duplicate it? |
Yes I spotted several other clear opportunities to simplify the module constructions. |
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.
I think I should have been more clear, if you find any pieces of code that are mostly duplicated across multiple places with only a small amount of variation, then extract that out to a function.
For example gen_header
takes some code that was duplicated twice, and extracts it out. However, gen_header_unnamed
just shifts some code around.
One other place that could benefit from extraction:
generic-field-projection/derive/src/lib.rs
Lines 387 to 393 in e46e228
for field in fields.named { | |
let ident = field.ident.unwrap(); | |
contents.push(item!( | |
#[allow(non_camel_case_types)] | |
pub struct #ident<T>(::gfp_core::derive::Invariant<T>); | |
)); |
generic-field-projection/derive/src/lib.rs
Lines 490 to 497 in e46e228
for (i, field) in fields.unnamed.iter().enumerate() { | |
use syn::spanned::Spanned; | |
let ident = quote::format_ident!("_{}", i, span = field.span()); | |
contents.push(item!( | |
#[allow(non_camel_case_types)] | |
pub struct #ident<T>(::gfp_core::derive::Invariant<T>); | |
)); |
generic-field-projection/derive/src/lib.rs
Lines 598 to 604 in e46e228
for field in fields.named { | |
let ident = field.ident.unwrap(); | |
contents.push(item!( | |
#[allow(non_camel_case_types)] | |
pub struct #ident<T>(::gfp_core::derive::Invariant<T>); | |
)); |
These for loops are very similar, with only a few minor differences between them. If you can find a way to unify them, that would be incredible.
merge_imports = true | ||
imports_granularity = "Crate" |
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.
What's the difference between these two? I'm not too familiar with rustfmt config
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.
this was the suggested resolution to a cli warning
fn gen_header_unnamed( | ||
generic_header: &syn::ImplGenerics, | ||
ident: &syn::Ident, | ||
input_ident: &syn::Ident, | ||
generic: &syn::TypeGenerics<'_>, | ||
ty: &&syn::Type, | ||
index: &syn::Member, | ||
) -> syn::Item { |
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.
If a function is only called once, then it doesn't pull its weight.
} | ||
|
||
// `TokenStream` construction for `derive_struct()` and `derive_named()` | ||
fn token_fields( |
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.
same as above
} | ||
|
||
// `TokenStream` construction for `derive_unnamed()` | ||
fn token_fields_unnamed( |
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.
same as above
} | ||
|
||
// `TokenStream` construction for `derive_union()` | ||
fn token_fields_union( |
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.
same as above
I think the only marginal benefit to functions holding macro code are the type signatures for all of the elements. It's interesting to learn it should be possible to harmonize all of these only slightly differing macros contents, so I'll look into doing that and refactor these properly. Thanks for the review! |
To me, that's not enough of a benefit to justify extracting it to a function. However, if you think it makes the code easier to follow, hopefully, it will make it easier to get new contributors like yourself! |
I agree with you about it, though yes I think there are some benefits to code readability having the types shown. If I can get the refactor done properly it shouldn't be an issue. |
Hey @kw-fn, are you still working on this PR? If you're busy that's OK, I just want an update. |
I have just been distracted with other projects, I hope to make progress this weekend |
Ok, thanks for the update! |
Looking at this right now! 🍰 |
Currently this approach fails with the following compiler error: