-
Notifications
You must be signed in to change notification settings - Fork 124
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 AsRef
and AsMut
derives
#286
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.
Thanks for the contribution. Just to be clear, this isn't adding any new features yet right? Only doing refactoring to allow future features. (I like that approach, just making sure I understand correctly)
Yes, that's correct, this PR is refactoring only |
Co-authored-by: Jelte Fennema <github-tech@jeltef.nl>
Co-authored-by: Jelte Fennema <github-tech@jeltef.nl>
AsRef
macro input parserAsRef
and AsMut
macro input parser
Since they're essentially identical, I think it makes sense to use the same deriving function for I can't think of any hypothetical features that should apply to only one of them, and this would facilitate keeping them in sync. |
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.
@MegaBluejay not bad, but needs polishing.
I've restructured modules in the way we have for fmt
macros.
Also, please look at tests/as_ref.rs
and tests/as_mut.rs
, whether it makes sense to extend their test cases to cover more edge cases.
@JelteF the interesting question: Since I think it's quite reasonable. May be done as a separate PR, though, after merging this one. |
Yes sounds good to me. But indeed let's do that in a follow-up PR. |
@tyranron feel free to sign off on after you are happy. I took a quick look and it looks reasonable to me. |
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.
@MegaBluejay I've heavily refactored your implementation. Consider the changes.
Regarding the implementation itself, it's good enough for merging already. However, the tests are still lacking features coverage very much, which I requested in previous review. No tests for #[as_ref]
and #[as_ref(skip)]
were added at all. I've restructured the existing tests in as_mut
, so they have some logical sequence, but I think it's not good enough yet.
Let's break it by modules:
mod single_field {
mod tuple {
// Clean
// Forward
// FieldForward
mod generic {
// Clean
// Forward
// FieldForward
}
}
mod named {
// Clean
// Forward
// FieldForward
mod generic {
// Clean
// Forward
// FieldForward
}
}
}
mod multi_field {
mod tuple {
// Clean
// Forward
// Field (multiple impls)
// Ignored (multiple impls)
mod generic {
// Clean
// Forward
// Field (multiple impls)
// Ignored (multiple impls)
}
}
mod named {
// Clean
// Forward
// Field (multiple impls)
// Ignored (multiple impls)
mod generic {
// Clean
// Forward
// Field (multiple impls)
// Ignored (multiple impls)
}
}
}
```
impl/src/as/mod.rs
Outdated
return Err(syn::Error::new( | ||
attr.attr.span(), | ||
format!( | ||
"`#[{0}(ignore)]` cannot be used in the same struct as other `#[{0}(...)]` \ |
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.
@MegaBluejay in other derive macros (see compile_fail
for them) we always talk about skip
attribute and keep ignore
as an alias. Why do the opposite here?
I've created utils::skip::Attribute
, which can be reused, so we don't repeat the same logic everywhere.
After merging this PR, please open a new one which refactors From
/Into
/Debug
/Display
to reuse utils::skip
and utils::forward
where applicable.
AsRef
and AsMut
macro input parserAsRef
and AsMut
derives
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.
@MegaBluejay good! 👍
After merging this PR, please do as subsequent ones:
-
Since
AsRef
andAsMut
are effectively the same regarding implementation, should we unite them under a singleas_ref
Cargo feature as we do forDisplay
-like derives?I think it's quite reasonable. May be done as a separate PR, though, after merging this one.
-
After merging this PR, please open a new one which refactors
From
/Into
/Debug
/Display
to reuseutils::skip
andutils::forward
where applicable.
First suggested [here](#286 (comment)). ## Synopsis `AsRef` and `AsMut` derives are effectively the same regarding implementation, so using separate features for them doesn't make sense. ## Solution Use the `as_ref` feature for both derives.
First suggested [here](#286 (comment)). ## Synopsis There's some repetition around parsing the same attributes in different derives. ## Solution Refactor `Into`, `From`, and `Debug` derives to use the `skip` and `forward` attribute parsing from `utils` module. ## Additionally Update documentation by mentioning an `ignore` alias for a`skip` attribute. Co-authored-by: Kai Ren <tyranron@gmail.com>
First suggested [here](JelteF/derive_more#286 (comment)). ## Synopsis `AsRef` and `AsMut` derives are effectively the same regarding implementation, so using separate features for them doesn't make sense. ## Solution Use the `as_ref` feature for both derives.
First suggested [here](JelteF/derive_more#286 (comment)). ## Synopsis There's some repetition around parsing the same attributes in different derives. ## Solution Refactor `Into`, `From`, and `Debug` derives to use the `skip` and `forward` attribute parsing from `utils` module. ## Additionally Update documentation by mentioning an `ignore` alias for a`skip` attribute. Co-authored-by: Kai Ren <tyranron@gmail.com>
Synopsis
This PR is a part of replacing all attributes having
syn::Meta
syntax to custom parsing, similarly to #241, #248Paves the way for #285, and possibly other enhancements such as an opt-in #123
Solution
Implement custom attribute parsing without
State
Checklist