-
Notifications
You must be signed in to change notification settings - Fork 136
Introduce HasField trait
#2842
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: main
Are you sure you want to change the base?
Introduce HasField trait
#2842
Conversation
Summary of ChangesHello @jswrenn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a foundational Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces the HasField trait, which allows compile-time reflection on struct fields. The implementation includes a new derive macro for HasField and a compile-time hashing function for field identifiers. The changes are well-structured, but I found a bug in the ToTokens implementation for the Trait enum that will cause compilation failures for existing derives. My review includes a suggestion to fix this.
9526d5d to
2b6d45e
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2842 +/- ##
==========================================
- Coverage 92.33% 92.05% -0.29%
==========================================
Files 20 20
Lines 5794 5812 +18
==========================================
Hits 5350 5350
- Misses 444 462 +18 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
2b6d45e to
e5dae4c
Compare
cff59b0 to
e738a09
Compare
The `HasField` trait permits privacy-preserving field projections from structs, enums and unions. `HasField` preserves privacy by leveraging the [scoped trait pattern]; for each field `f`, `HasField` is implemented with the `Field` parameter set to a type with identical visibility as `f`. In many useful contexts, this `Field` parameter is inferable, and need not be actually reachable. Consequently, `HasField` can be used to index a field from only the name of a field. See `zerocopy-derive/tests/ui-nightly/privacy.rs` for examples. [scoped trait pattern]: https://jack.wrenn.fyi/blog/private-trait-impls/ gherrit-pr-id: G2238bc341570838db412d880017b3f0c25ac09fa
e738a09 to
bced22a
Compare
| // Since Rust does not presently support explicit visibility | ||
| // modifiers on enum fields, any public type is suitable | ||
| // here; we use `()`. | ||
| field: parse_quote!(()), |
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.
Feel free to disregard, but it might be worth considering whether we could put this in a const or static or something to avoid recomputing it for every field.
| FieldBounds::TRAILING_SELF | ||
| }; | ||
| let require_trait_bound_on_field_types = | ||
| if matches!(self_bounds, SelfBounds::All(&[Trait::Sized])) { |
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.
Is this a breaking change?
|
|
||
| if fields.is_empty() { | ||
| return quote! {}; | ||
| } |
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.
Style uber-nit; feel free to disregard.
| if fields.is_empty() { | |
| return quote! {}; | |
| } | |
| if fields.is_empty() { | |
| return quote! {}; | |
| } |
| }); | ||
|
|
||
| let has_fields = fields.iter().map(|(_, ident, ty)| { | ||
| let field_token = Ident::new(&format!("ẕ{}", ident), ident.span()); |
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 here re: __Zerocopy_ prefix.
| ast, | ||
| data, | ||
| Trait::HasField { | ||
| variant_id: parse_quote!({ #zerocopy_crate::ident_id!(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.
If this is for a variant, why is it always 0? Shouldn't it be the ID of the variant in question?
| @@ -0,0 +1,102 @@ | |||
| // Copyright 2019 The Fuchsia Authors | |||
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.
| // Copyright 2019 The Fuchsia Authors | |
| // Copyright 2025 The Fuchsia Authors |
| // These tests cause errors which are generated by a later compilation pass than | ||
| // the other errors we generate, and so if they're compiled in the same file, | ||
| // the compiler will never get to that pass, and so we won't get the errors. |
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.
That comment only makes sense in the late_compile_pass.rs test IMO. Is it true for this file too?
The
HasFieldtrait permits privacy-preserving field projectionsfrom structs, enums and unions.
HasFieldpreserves privacy by leveraging the scoped trait pattern;for each field
f,HasFieldis implemented with theFieldparameterset to a type with identical visibility as
f. In many useful contexts,this
Fieldparameter is inferable, and need not be actually reachable.Consequently,
HasFieldcan be used to index a field from only the nameof a field. See
zerocopy-derive/tests/ui-nightly/privacy.rsforexamples.
This PR is on branch fields.
HasField::project; simplifyis_bit_valid#2843HasFieldtrait #2842