Skip to content

EntityMut::get_components_mut_unchecked()#20265

Merged
alice-i-cecile merged 1 commit intobevyengine:mainfrom
chescock:get_components_mut_unchecked
Jul 23, 2025
Merged

EntityMut::get_components_mut_unchecked()#20265
alice-i-cecile merged 1 commit intobevyengine:mainfrom
chescock:get_components_mut_unchecked

Conversation

@chescock
Copy link
Contributor

Objective

Provide an unsafe way to get mutable access to multiple components from an EntityMut.

This is the API proposed in #13375, but marked unsafe as it does not check for conflicting access.

Solution

Add unsafe fn get_components_mut_unchecked(&mut self) -> Option<Q::Item> and unsafe fn into_components_mut_unchecked(self) -> Option<Q::Item> to EntityMut and EntityWorldMut.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use X-Uncontroversial This work is generally agreed upon D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 23, 2025
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

This is clean, and I'm happy with it existing. Good safety comments! If you can find a way to add type-id backed debug asserts (or maybe not debug actually) to catch the simple aliasing cases, I would appreciate it.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes and removed C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jul 23, 2025
@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Jul 23, 2025
@alice-i-cecile
Copy link
Member

@tychedelia, I'd appreciate your review here as a user, especially on the safety comments. Are they clear enough that you're comfortable following them?

Copy link
Member

@tychedelia tychedelia left a comment

Choose a reason for hiding this comment

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

Lgtm! Docs are robust and the safety qualifications are about as straightforward as it gets for Bevy.

@tychedelia tychedelia 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 Jul 23, 2025
@chescock
Copy link
Contributor Author

If you can find a way to add type-id backed debug asserts (or maybe not debug actually) to catch the simple aliasing cases, I would appreciate it.

I agree that would be good to have, but I don't have any ideas on how to implement it (short of something like #17837). I don't think there is any way to get a TypeId out of a QueryData today.

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jul 23, 2025
Copy link
Contributor

@cBournhonesque cBournhonesque left a comment

Choose a reason for hiding this comment

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

Awesome, this is a huge ergonomic improvement

Merged via the queue into bevyengine:main with commit 9445481 Jul 23, 2025
49 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Sep 29, 2025
# Objective

#20265 introduced a way to fetch
multiple mutable components from an `EntityMut`, but it's still
impossible to do so via an `FilteredEntityMut`.

I believe it is currently impossible to get two mutable components from
a `FilteredEntityMut`, which somewhat limits use cases with dynamic
queries.


## Solution

A similar solution is harder to implement for `FilteredEntityMut`
because the QueryData must go through the access checks, and it's not
obvious to get the ComponentIds from a `ReleaseStateQueryData`.

Instead, I opt in to provide a similar abstraction as `UnsafeEntityCell`
and `UnsafeWorldCell`, which are both public and Clone: an opt-in escape
catch for advanced users that can guarantee that they are not causing
any aliasing violations.

Here, instead we provide a method that copies the underlying
`UnsafeEntityCell`, so the safety requirements are similar to
`UnsafeEntityCell` and `UnsafeWorldCell`.


## Testing

Added a doctest.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Chris Russell <8494645+chescock@users.noreply.github.com>
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-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes 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 X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants