Skip to content

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

Closed
chescock wants to merge 1 commit intobevyengine:mainfrom
chescock:get_mut_unchecked
Closed

Provide an unsafe way to get mutable access to multiple components from an EntityMut#20262
chescock wants to merge 1 commit intobevyengine:mainfrom
chescock:get_mut_unchecked

Conversation

@chescock
Copy link
Contributor

Objective

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

Note that this is possible using EntityMut::get_mut_by_id_unchecked by passing a ComponentId and getting a MutUntyped, but there is currently no typed version.

Helps with #13127

Solution

Add an unsafe fn get_mut_unchecked(&self) -> Option<Mut<T>> to EntityMut. Users can call this multiple times to get mutable access to multiple components, and it's sound as long as the components are all distinct.

@chescock chescock added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 23, 2025
/// struct Y(usize);
///
/// # let mut world = World::default();
/// let mut entity = world.spawn((X(0), Y(0))).into_mutable();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should EntityWorldMut deref automatically into EntityMut?

Is into_mutable a new api? I haven't seen that before. I guess it's a new inner type shared between EntityWorldMut and FilteredEntityMut for generic mutable component access?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should EntityWorldMut deref automatically into EntityMut?

I don't think that's possible today, since we don't actually have an EntityMut to borrow from.

Is into_mutable a new api? I haven't seen that before. I guess it's a new inner type shared between EntityWorldMut and FilteredEntityMut for generic mutable component access?

That's the conversion from EntityWorldMut into EntityMut :). There's also as_mutable() to just borrow, but using that here would require another statement to avoid "temporary value dropped while borrowed".

///
/// - [`get_mut`](Self::get_mut) for the safe version.
#[inline]
pub unsafe fn get_mut_unchecked<T: Component<Mutability = Mutable>>(&self) -> Option<Mut<T>> {
Copy link
Contributor

@cBournhonesque cBournhonesque Jul 23, 2025

Choose a reason for hiding this comment

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

I'm sorry if this has been asked before, but why can't we also have an unsafe API like #13375 to query mutably multiple typed components at once?

The safety requirements would ask the user to make sure to not create aliased reference, and we wouldn't need to do the access checks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that's a good idea! We should just do that instead! I don't think this PR has any advantage over a fn get_components_mut_unchecked.

@chescock
Copy link
Contributor Author

Made #20265 based on @cBournhonesque's suggestion above. I plan to close this PR if that one is merged. Thanks!

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-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants