Provide an unsafe way to get mutable access to multiple components from an EntityMut#20262
Provide an unsafe way to get mutable access to multiple components from an EntityMut#20262chescock wants to merge 1 commit intobevyengine:mainfrom
EntityMut#20262Conversation
…om an `EntityMut`.
| /// struct Y(usize); | ||
| /// | ||
| /// # let mut world = World::default(); | ||
| /// let mut entity = world.spawn((X(0), Y(0))).into_mutable(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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_mutablea 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>> { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
Made #20265 based on @cBournhonesque's suggestion above. I plan to close this PR if that one is merged. Thanks! |
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_uncheckedby passing aComponentIdand getting aMutUntyped, but there is currently no typed version.Helps with #13127
Solution
Add an
unsafe fn get_mut_unchecked(&self) -> Option<Mut<T>>toEntityMut. 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.