-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Make Parent
and Children
components immutable
#16662
base: main
Are you sure you want to change the base?
Make Parent
and Children
components immutable
#16662
Conversation
It looks like your PR is a breaking change, but you didn't provide a migration guide. Could you add some context on what users should update when this change get released in a new version of Bevy? |
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.
Just some documentation that needs to be deleted now that apply_or_insert
doesn't panic, otherwise I think this is good to go! A follow-up that adds hooks for upholding the Parent
/Children
relationship invariants would be the next logical step.
#[expect(unsafe_code)] | ||
impl GetHierarchyComponentsMut for EntityWorldMut<'_> { | ||
fn get_parent_mut(&mut self) -> Option<Mut<Parent>> { | ||
// SAFETY: The Parent component has no invariants. |
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.
The doc comments for get_mut_assume_mutable()
give the safety invariant as `T` must be a mutable component
, which this safety comment doesn't satisfy. If this is intended to be legal, then I think we need to update the documentation for that method.
I thought the plan was that immutable components could only be updated by re-inserting them, though, and that this was not intended to be legal.
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 think updating the safety requirement for the *_assume_mutable
methods is appropriate here in the interim. The ideal solution would be to use swapping to actually preserve immutability and avoid the unsafe methods, but that's a little cumbersome at the moment.
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 agree that the safety messages should be changed, it confused me too.
The ideal solution would be to use swapping
What does 'swapping' mean here?
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.
"swapping" as in temporarily replacing Children
/Parent
with a dummy value so you can just have ownership over the component while you're editing it. Basically core::mem::swap
. As an alternative, #16668 will add a with_component
method which does this for you without the temporary value by directly triggering hooks/observers.
Objective
Minimal changes to make the hierarchy components immutable.