Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

tim-blackbird
Copy link
Contributor

Objective

Minimal changes to make the hierarchy components immutable.

@tim-blackbird tim-blackbird added A-ECS Entities, components, systems, and events A-Hierarchy Parent-child entity hierarchies S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 5, 2024
@rparrett rparrett added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Dec 5, 2024
Copy link
Contributor

github-actions bot commented Dec 5, 2024

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?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

Copy link
Contributor

@bushrat011899 bushrat011899 left a 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.

crates/bevy_ecs/src/reflect/component.rs Show resolved Hide resolved
#[expect(unsafe_code)]
impl GetHierarchyComponentsMut for EntityWorldMut<'_> {
fn get_parent_mut(&mut self) -> Option<Mut<Parent>> {
// SAFETY: The Parent component has no invariants.
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events A-Hierarchy Parent-child entity hierarchies M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants