-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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_derive: Add #[deref]
attribute
#8552
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.
I like this, and I like that the attribute is shared.
Once the examples are beefed up this LGTM.
If we're going to include this feature I think it makes sense to extend the macro in uncontroversial ways.
Updated macro logic to give a proper error for field-less structs. |
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.
Seems useful, and the implementation looks correct.
Have you considered making the #[deref]
attribute unnecessary if all but one field name starts with an underscore?
|
If we're adding compile fail tests I think that one that fails on multiple deref attributes would be nice too :) |
I'm very opposed to this! At least before we merge #8573 or another PR updating |
Could be an interesting way of achieving this, but I worry it's a little too implicit and might trip up some users (though, I suppose the error message would help guide them). Doing this would also add a difference between tuple structs and standard structs which may not be ideal. For now maybe we just keep the attribute and consider underscores in the future?
Good idea! Should those go in I originally tried doing this as a test module in the same file but I think
Makes sense, I don't mind waiting and rebasing. That gives me some time to add the compile fail tests 😄 Tasks
|
one possible way to resolve most of the |
That could help in some cases, but unfortunately it doesn't really help out when the type is in the same module as the access. And this is especially likely for new Rust/Bevy users who may just copy+paste code from an example or tutorial to start with and suddenly become confused with the field access. In such cases, I feel an explicit |
#8573 is in the merge queue for you :) |
Created with compile tests for the Deref/DerefMut derives
@alice-i-cecile Should be ready to go! Thanks @mockersf for suggesting waiting on #8573, since this actually ended up needing a fix for Also added two more compile tests for both derives that I forgot about:
|
Objective
Bevy code tends to make heavy use of the newtype pattern, which is why we have a dedicated derive for
Deref
andDerefMut
. This derive works for any struct with a single field:One reason for the single-field limitation is to prevent confusion and footguns related that would arise from allowing multi-field structs:
However, there are certainly cases where it's useful to allow for structs with multiple fields. Such as for structs with one "real" field and one
PhantomData
to allow for generics:Additionally, the possible confusion and footguns are mainly an issue for newer Rust/Bevy users. Those familiar with
Deref
andDerefMut
understand what adding the derive really means and can anticipate its behavior.Solution
Allow users to opt into multi-field
Deref
/DerefMut
derives using a#[deref]
attribute:This prevents the footgun pointed out in the first issue described in the previous section, but it still leaves the possible confusion surrounding
.0
-vs-.#
. However, the idea is that by making this behavior explicit with an attribute, users will be more aware of it and can adapt appropriately.Changelog
#[deref]
attribute toDeref
andDerefMut
derives