-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
bevy_reflect_derive: Clean up attribute logic #11777
bevy_reflect_derive: Clean up attribute logic #11777
Conversation
Parse ParseStream directly Custom attributes are not fully implemented yet. This just permits their definition via the derive macro.
aebf1cd
to
1472fc9
Compare
Make use of ParseStream directly
1472fc9
to
1837098
Compare
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 love this!
Head branch was pushed to by a user without write access
I updated the compile fail tests to account for the new attribution parsing. A side-effect of these changes is that it's now possible to include I decided not to remove it since we already allow this behavior for other non-registration attributes (i.e. |
Objective
The code in
bevy_reflect_derive
could use some cleanup.Solution
Took some of the changes in #11659 to create a dedicated PR for cleaning up the field and container attribute logic.
Updated Naming
I renamed
ReflectTraits
andReflectFieldAttr
toContainerAttributes
andFieldAttributes
, respectively. I think these are clearer.Updated Parsing
Readability
The parsing logic wasn't too bad before, but it was getting difficult to read. There was some duplicated logic between
Meta::List
andMeta::Path
attributes. Additionally, all the logic was kept inside a large method.To simply things, I replaced the nested meta parsing with
ParseStream
parsing. In my opinion, this is easier to follow since it breaks up the large match statement into a small set of single-line if statements, where each if-block contains a single call to the appropriate attribute parsing method.Flexibility
On top of the added simplicity, this also makes our attribute parsing much more flexible. It allows us to more elegantly handle custom where clauses (i.e.
#[reflect(where T: Foo)]
) and it opens the door for more non-standard attribute syntax (e.g. #11659).Errors
This also allows us to automatically provide certain errors when parsing. For example, since we can use
stream.lookahead1()
, we get errors like the following for free:Changelog
Note
All changes are internal to
bevy_reflect_derive
and should not affect the public API1.ReflectTraits
toContainerAttributes
ReflectMeta::traits
toReflectMeta::attrs
ReflectFieldAttr
toFieldAttributes
ParseStream
directly instead of nested meta parsingbevy_reflect_derive
Footnotes
Does not include errors, which may look slightly different. ↩