-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Component requirements shall need either Default or to be a unit struct. #18911
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?
Component requirements shall need either Default or to be a unit struct. #18911
Conversation
Not certain how small you want each commit to be? I'll just commit every now and again I suppose.
Continuous integration complains that my let binding has unit value, but if I remove it, then it claims that my code is a path statement with no effect.
This gets rid of the continuous integration errors.
Because this is inside a trait, I need to use a slightly different approach. Whoops. Should be fixed next commit.
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
Had to switch to const deref specialisation, but it caused the error message to improve, which is quite nice.
Added a check to make 100% sure that only 0 sized types don't use default.
It doesn't work yet, but it is along the right track. Also why are there so many clippy issues? I didn't even touch the code they are complaining about!
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'm curious about the motivation for this feature, as opposed to deriving Default
on the required component. Is this meant for components that are defined in external crates?
EDIT: I see there's a linked issue but this seems to be an open question there as well.
.cargo/config_fast_builds.toml
Outdated
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 file was probably not meant to be deleted.
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 wasn't exactly sure how to handle the fast build toml. I don't yet know enough about git to know how to exclude my changes to that directory. I suppose I'll just revert those files back to normal at the end.
} | ||
|
||
/// Checks if a component's requirement is valid. | ||
/// It is valid if it has Default, or if it has a size of 0 and is therefore a unit struct. |
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.
Technically not all ZSTs are unit structs. For example, struct Foo { x: () }
is a ZST but not a unit struct. Also empty types (like !
and enum Bar {}
) and functions are ZSTs.
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.
Yeah. I am worried about that, but at the same time, it seems unlikely for such structs to be marked as a component. Are there any foot guns I haven't noticed?
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 struct has a single private field that is a zst, so that people cannot construct it, then this bypasses it. Yikes. Unlikely to be a problem, but still not great.
We could solve this by making the component derive implement a UnitStruct trait when it encounters a unit struct.
crates/bevy_ecs/compile_fail/tests/ui/non_unit_without_default_required_components.rs
Outdated
Show resolved
Hide resolved
crates/bevy_ecs/compile_fail/tests/ui/non_unit_without_default_required_components.rs
Outdated
Show resolved
Hide resolved
…_required_components.rs Thanks benfrankel! That was left over from when I accidentally put the error checker in the wrong place... Co-authored-by: Ben Frankel <ben.frankel7@gmail.com>
…_required_components.rs Thanks benfrankel! Co-authored-by: Ben Frankel <ben.frankel7@gmail.com>
I have no idea why this would be wanted. I simply saw an issue that it was claimed was not possible to solve, and as such I just had to solve it. (It was fun! Although, I did spend 90% of my time fighting with continuous integration, because I had never used it before!) |
This makes me very nervous. I don't think that the value to risk / complexity is worth it here. I've marked the upstream issue Nominated-to-Close, and think we should do the same here. Super cool that you could do this though! |
Objective
Solution
Testing