Skip to content

Commit b065073

Browse files
implement EntityMut::get_mut in terms of InteriorMutableEntityRef
1 parent 17afd9b commit b065073

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use bevy_ptr::{OwningPtr, Ptr};
1313
use bevy_utils::tracing::debug;
1414
use std::any::TypeId;
1515

16+
use super::interior_mutable_world::InteriorMutableEntityRef;
17+
1618
/// A read-only reference to a particular [`Entity`] and all of its components
1719
#[derive(Copy, Clone)]
1820
pub struct EntityRef<'w> {
@@ -254,24 +256,13 @@ impl<'w> EntityMut<'w> {
254256

255257
#[inline]
256258
pub fn get_mut<T: Component>(&mut self) -> Option<Mut<'_, T>> {
257-
// SAFETY: world access is unique, and lifetimes enforce correct usage of returned borrow
258-
unsafe {
259-
self.world
260-
.get_component_and_ticks_with_type(
261-
TypeId::of::<T>(),
262-
T::Storage::STORAGE_TYPE,
263-
self.entity,
264-
self.location,
265-
)
266-
.map(|(value, cells)| Mut {
267-
value: value.assert_unique().deref_mut::<T>(),
268-
ticks: TicksMut::from_tick_cells(
269-
cells,
270-
self.world.last_change_tick,
271-
self.world.read_change_tick(),
272-
),
273-
})
274-
}
259+
let interior_mutable = InteriorMutableEntityRef::new(
260+
self.world.as_interior_mutable(),
261+
self.entity,
262+
self.location,
263+
);
264+
// SAFETY: interiormutableentityref has exclusive access
265+
unsafe { interior_mutable.get_mut() }
275266
}
276267

277268
/// Retrieves the change ticks for the given component. This can be useful for implementing change

crates/bevy_ecs/src/world/interior_mutable_world.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,7 @@ impl<'w> InteriorMutableWorld<'w> {
139139
/// Similar to the [`InteriorMutableWorld`], you are in charge of making sure that no aliasing rules are violated.
140140
pub fn get_entity(self, entity: Entity) -> Option<InteriorMutableEntityRef<'w>> {
141141
let location = self.0.entities.get(entity)?;
142-
Some(InteriorMutableEntityRef {
143-
world: InteriorMutableWorld(self.0),
144-
entity,
145-
location,
146-
})
142+
Some(InteriorMutableEntityRef::new(self, entity, location))
147143
}
148144

149145
/// Gets a reference to the resource of the given type if it exists
@@ -367,6 +363,18 @@ pub struct InteriorMutableEntityRef<'w> {
367363
}
368364

369365
impl<'w> InteriorMutableEntityRef<'w> {
366+
pub(crate) fn new(
367+
world: InteriorMutableWorld<'w>,
368+
entity: Entity,
369+
location: EntityLocation,
370+
) -> Self {
371+
InteriorMutableEntityRef {
372+
world,
373+
entity,
374+
location,
375+
}
376+
}
377+
370378
#[inline]
371379
#[must_use = "Omit the .id() call if you do not need to store the `Entity` identifier."]
372380
pub fn id(self) -> Entity {

0 commit comments

Comments
 (0)