Skip to content

Simplify usage of EntityWorldMut::move_entity_from_remove by making it a proper method #17360

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

Closed
wants to merge 9 commits into from

Conversation

JaySpruce
Copy link
Member

Objective

Fixes #17345

The "probably more safety stuff" was probably referring to the EntityLocation needing to be valid for the given Entity or something like that, but that's not necessary if we just give it the actual EntityWorldMut.

Solution

  • Added &mut self to move_entity_from_remove parameters.
  • Removed most of the method's other parameters and replaced them with local variables derived from self.
  • The method now directly updates the location field of the EntityWorldMut, which wasn't necessarily the case before.
    • EntityWorldMut::take passed in a mutable reference to the field, so it already worked like that.
    • EntityWorldMut::remove_bundle passed in a mutable reference to a local variable, which it then returned so that its caller could use it to update the location field. This seems unnecessarily convoluted.
  • EntityWorldMut::remove_bundle no longer returns the entity's location, as it is now updated internally.
  • Updated safety notes.
  • Added unsafe blocks to some calls that had a safety note with no block.

@BenjaminBrienen BenjaminBrienen added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 14, 2025
Copy link
Contributor

@Carter0 Carter0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broadly makes sense to me. Someone with more knowledge of our ECS internals to take a look tho.

self.location = unsafe { self.remove_bundle(bundle_info, caller) };
// SAFETY: the `BundleInfo` for this `bundle_id` is initialized above
unsafe {
self.remove_bundle(bundle_id, caller);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but if you put the ; outside the unsafe block then rustfmt might put it all on one line. (Here and below.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly different semantics though; semicolon-outside is used to return something from the block to a variable.

I don't know how much we actually care about that, but we seem to follow it in this file at least.

/// Moves the entity to a new archetype, where the new archetype
/// was a result of removing components from the entity's current archetype.
///
/// When `DROP` is `true`, removed components will be dropped.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notes about DROP may have been in the "Safety" section because it would be unsound to drop the values if they've already been moved out.

On the other hand, having the component data be valid is the normal state. The fact that this doesn't drop when false is just an extra behavior used to justify the safety requirements for the take_component call.

So, yup, moving the notes on DROP out of the "safety" section is an improvement! (Sorry for the noise; the safety logic here is a little subtle and I wanted to write down my train of thought when reviewing.)

@JaySpruce
Copy link
Member Author

@alice-i-cecile, think this is small enough to slip into 0.16?

Also, I'd like to formally request the powers of labelling, if you wouldn't mind.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Mar 25, 2025
@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Mar 25, 2025
@alice-i-cecile
Copy link
Member

The power of labelling has been bequeathed onto you (once you accept the invite) 👸🏽

As for 0.16 vs 0.17, I don't think this has any end user impact, so I'd like to leave this until 0.17 to avoid introducing unforeseen last-minute bugs <3 It'll get merged in my round-up once we start that though!

@chescock chescock mentioned this pull request May 1, 2025
@ElliottjPierce
Copy link
Contributor

This looks good to me, and I'm on board with the change, but if we plan to merge #18521, this would be a moot point. (That pr removes move_entity_from_remove entirely.)

In other words, if we merge this, fixing merge conflicts with #18521 would just be undoing this. I'd be happy to do that if we're still undecided on that PR, but in the end, I think this is an either or situation. I don't think we can do both IIUC. Up to y'all which one you want to do.

@alice-i-cecile
Copy link
Member

I prefer #18521, so I'm going to close this out :)

@JaySpruce JaySpruce deleted the simple_move_from_remove branch May 4, 2025 16:50
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 C-Code-Quality A section of code that is hard to understand or change D-Straightforward Simple bug fixes and API improvements, docs, test and examples D-Unsafe Touches with unsafe code in some way S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bevy_ecs::world::entity_ref::EntityWorldMut::take(), a safe function, has an undocumented unsafe block
6 participants