Skip to content

Commit d6f742b

Browse files
InteriorMutableWorld -> UnsafeWorldCell
1 parent 3b39018 commit d6f742b

File tree

8 files changed

+115
-112
lines changed

8 files changed

+115
-112
lines changed

crates/bevy_asset/src/reflect.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::any::{Any, TypeId};
22

3-
use bevy_ecs::world::{interior_mutable_world::InteriorMutableWorld, World};
3+
use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
44
use bevy_reflect::{FromReflect, FromType, Reflect, Uuid};
55

66
use crate::{Asset, Assets, Handle, HandleId, HandleUntyped};
@@ -21,8 +21,7 @@ pub struct ReflectAsset {
2121
// SAFETY:
2222
// - may only be called with a [`IteriorMutableWorld`] which can be used to access the corresponding `Assets<T>` resource mutably
2323
// - may only be used to access **at most one** access at once
24-
get_unchecked_mut:
25-
unsafe fn(InteriorMutableWorld<'_>, HandleUntyped) -> Option<&mut dyn Reflect>,
24+
get_unchecked_mut: unsafe fn(UnsafeWorldCell<'_>, HandleUntyped) -> Option<&mut dyn Reflect>,
2625
add: fn(&mut World, &dyn Reflect) -> HandleUntyped,
2726
set: fn(&mut World, HandleUntyped, &dyn Reflect) -> HandleUntyped,
2827
len: fn(&World) -> usize,
@@ -58,10 +57,10 @@ impl ReflectAsset {
5857
handle: HandleUntyped,
5958
) -> Option<&'w mut dyn Reflect> {
6059
// SAFETY: unique world access
61-
unsafe { (self.get_unchecked_mut)(world.as_interior_mutable(), handle) }
60+
unsafe { (self.get_unchecked_mut)(world.as_unsafe_world_cell(), handle) }
6261
}
6362

64-
/// Equivalent of [`Assets::get_mut`], but works with an [`InteriorMutableWorld`].
63+
/// Equivalent of [`Assets::get_mut`], but works with an [`UnsafeWorldCell`].
6564
///
6665
/// Only use this method when you have ensured that you are the *only* one with access to the [`Assets`] resource of the asset type.
6766
/// Furthermore, this does *not* allow you to have look up two distinct handles,
@@ -74,9 +73,9 @@ impl ReflectAsset {
7473
/// # let mut world: World = unimplemented!();
7574
/// # let handle_1: HandleUntyped = unimplemented!();
7675
/// # let handle_2: HandleUntyped = unimplemented!();
77-
/// let interior_mutable_world = world.as_interior_mutable();
78-
/// let a = unsafe { reflect_asset.get_unchecked_mut(interior_mutable_world, handle_1).unwrap() };
79-
/// let b = unsafe { reflect_asset.get_unchecked_mut(interior_mutable_world, handle_2).unwrap() };
76+
/// let unsafe_world_cell = world.as_unsafe_world_cell();
77+
/// let a = unsafe { reflect_asset.get_unchecked_mut(unsafe_world_cell, handle_1).unwrap() };
78+
/// let b = unsafe { reflect_asset.get_unchecked_mut(unsafe_world_cell, handle_2).unwrap() };
8079
/// // ^ not allowed, two mutable references through the same asset resource, even though the
8180
/// // handles are distinct
8281
///
@@ -86,11 +85,11 @@ impl ReflectAsset {
8685
/// # Safety
8786
/// This method does not prevent you from having two mutable pointers to the same data,
8887
/// violating Rust's aliasing rules. To avoid this:
89-
/// * Only call this method if you know that the [`InteriorMutableWorld`] may be used to access the corresponding `Assets<T>`
88+
/// * Only call this method if you know that the [`UnsafeWorldCell`] may be used to access the corresponding `Assets<T>`
9089
/// * Don't call this method more than once in the same scope.
9190
pub unsafe fn get_unchecked_mut<'w>(
9291
&self,
93-
world: InteriorMutableWorld<'w>,
92+
world: UnsafeWorldCell<'w>,
9493
handle: HandleUntyped,
9594
) -> Option<&'w mut dyn Reflect> {
9695
// SAFETY: requirements are deferred to the caller
@@ -145,7 +144,7 @@ impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
145144
asset.map(|asset| asset as &dyn Reflect)
146145
},
147146
get_unchecked_mut: |world, handle| {
148-
// SAFETY: `get_unchecked_mut` must be callied with `InteriorMutableWorld` having access to `Assets<A>`,
147+
// SAFETY: `get_unchecked_mut` must be callied with `UnsafeWorldCell` having access to `Assets<A>`,
149148
// and must ensure to only have at most one reference to it live at all times.
150149
let assets = unsafe { world.get_resource_mut::<Assets<A>>().unwrap().into_inner() };
151150
let asset = assets.get_mut(&handle.typed());

crates/bevy_ecs/src/reflect.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
component::Component,
66
entity::{Entity, EntityMap, MapEntities, MapEntitiesError},
77
system::Resource,
8-
world::{interior_mutable_world::InteriorMutableWorld, FromWorld, World},
8+
world::{unsafe_world_cell::UnsafeWorldCell, FromWorld, World},
99
};
1010
use bevy_reflect::{
1111
impl_from_reflect_value, impl_reflect_value, FromType, Reflect, ReflectDeserialize,
@@ -54,8 +54,8 @@ pub struct ReflectComponentFns {
5454
/// Function pointer implementing [`ReflectComponent::reflect_mut()`].
5555
///
5656
/// # Safety
57-
/// The function may only be called with an [`InteriorMutableWorld`] that can be used to mutably access the relevant component on the given entity.
58-
pub reflect_mut: unsafe fn(InteriorMutableWorld<'_>, Entity) -> Option<Mut<'_, dyn Reflect>>,
57+
/// The function may only be called with an [`UnsafeWorldCell`] that can be used to mutably access the relevant component on the given entity.
58+
pub reflect_mut: unsafe fn(UnsafeWorldCell<'_>, Entity) -> Option<Mut<'_, dyn Reflect>>,
5959
/// Function pointer implementing [`ReflectComponent::copy()`].
6060
pub copy: fn(&World, &mut World, Entity, Entity),
6161
}
@@ -120,17 +120,17 @@ impl ReflectComponent {
120120
entity: Entity,
121121
) -> Option<Mut<'a, dyn Reflect>> {
122122
// SAFETY: unique world access
123-
unsafe { (self.0.reflect_mut)(world.as_interior_mutable(), entity) }
123+
unsafe { (self.0.reflect_mut)(world.as_unsafe_world_cell(), entity) }
124124
}
125125

126126
/// # Safety
127127
/// This method does not prevent you from having two mutable pointers to the same data,
128128
/// violating Rust's aliasing rules. To avoid this:
129-
/// * Only call this method with a [`InteriorMutableWorld`] that may be used to mutably access the component on the entity `entity`
129+
/// * Only call this method with a [`UnsafeWorldCell`] that may be used to mutably access the component on the entity `entity`
130130
/// * Don't call this method more than once in the same scope for a given [`Component`].
131131
pub unsafe fn reflect_unchecked_mut<'a>(
132132
&self,
133-
world: InteriorMutableWorld<'a>,
133+
world: UnsafeWorldCell<'a>,
134134
entity: Entity,
135135
) -> Option<Mut<'a, dyn Reflect>> {
136136
// SAFETY: safety requirements deferred to caller
@@ -213,7 +213,7 @@ impl<C: Component + Reflect + FromWorld> FromType<C> for ReflectComponent {
213213
},
214214
reflect_mut: |world, entity| {
215215
// SAFETY: reflect_mut is an unsafe function pointer used by
216-
// 1. `reflect_unchecked_mut` which must be called with an InteriorMutableWorld with access to the the component `C` on the `entity`, and
216+
// 1. `reflect_unchecked_mut` which must be called with an UnsafeWorldCell with access to the the component `C` on the `entity`, and
217217
// 2. `reflect_mut`, which has mutable world access
218218
unsafe {
219219
world.get_entity(entity)?.get_mut::<C>().map(|c| Mut {
@@ -268,8 +268,8 @@ pub struct ReflectResourceFns {
268268
/// Function pointer implementing [`ReflectResource::reflect_unchecked_mut()`].
269269
///
270270
/// # Safety
271-
/// The function may only be called with an [`InteriorMutableWorld`] that can be used to mutably access the relevant resource.
272-
pub reflect_unchecked_mut: unsafe fn(InteriorMutableWorld<'_>) -> Option<Mut<'_, dyn Reflect>>,
271+
/// The function may only be called with an [`UnsafeWorldCell`] that can be used to mutably access the relevant resource.
272+
pub reflect_unchecked_mut: unsafe fn(UnsafeWorldCell<'_>) -> Option<Mut<'_, dyn Reflect>>,
273273
/// Function pointer implementing [`ReflectResource::copy()`].
274274
pub copy: fn(&World, &mut World),
275275
}
@@ -318,17 +318,17 @@ impl ReflectResource {
318318
/// Gets the value of this [`Resource`] type from the world as a mutable reflected reference.
319319
pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option<Mut<'a, dyn Reflect>> {
320320
// SAFETY: unique world access
321-
unsafe { (self.0.reflect_unchecked_mut)(world.as_interior_mutable()) }
321+
unsafe { (self.0.reflect_unchecked_mut)(world.as_unsafe_world_cell()) }
322322
}
323323

324324
/// # Safety
325325
/// This method does not prevent you from having two mutable pointers to the same data,
326326
/// violating Rust's aliasing rules. To avoid this:
327-
/// * Only call this method with an [`InteriorMutableWorld`] which can be used to mutably access the resource.
327+
/// * Only call this method with an [`UnsafeWorldCell`] which can be used to mutably access the resource.
328328
/// * Don't call this method more than once in the same scope for a given [`Resource`].
329329
pub unsafe fn reflect_unchecked_mut<'w>(
330330
&self,
331-
world: InteriorMutableWorld<'w>,
331+
world: UnsafeWorldCell<'w>,
332332
) -> Option<Mut<'w, dyn Reflect>> {
333333
// SAFETY: caller promises to uphold uniqueness guarantees
334334
(self.0.reflect_unchecked_mut)(world)

crates/bevy_ecs/src/system/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
11341134
}
11351135
let world = self.world;
11361136
let entity_ref = world
1137-
.as_interior_mutable_migration_internal()
1137+
.as_unsafe_world_cell_migration_internal()
11381138
.get_entity(entity)
11391139
.ok_or(QueryComponentError::NoSuchEntity)?;
11401140
let component_id = world

crates/bevy_ecs/src/system/system_param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ unsafe impl<'a, T: Resource> SystemParam for ResMut<'a, T> {
541541
change_tick: u32,
542542
) -> Self::Item<'w, 's> {
543543
let value = world
544-
.as_interior_mutable_migration_internal()
544+
.as_unsafe_world_cell_migration_internal()
545545
.get_resource_mut_with_id(component_id)
546546
.unwrap_or_else(|| {
547547
panic!(
@@ -579,7 +579,7 @@ unsafe impl<'a, T: Resource> SystemParam for Option<ResMut<'a, T>> {
579579
change_tick: u32,
580580
) -> Self::Item<'w, 's> {
581581
world
582-
.as_interior_mutable_migration_internal()
582+
.as_unsafe_world_cell_migration_internal()
583583
.get_resource_mut_with_id(component_id)
584584
.map(|value| ResMut {
585585
value: value.value,

crates/bevy_ecs/src/world/entity_ref.rs

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

16-
use super::interior_mutable_world::InteriorMutableEntityRef;
16+
use super::unsafe_world_cell::UnsafeEntityRefCell;
1717

1818
/// A read-only reference to a particular [`Entity`] and all of its components
1919
#[derive(Copy, Clone)]
@@ -42,9 +42,9 @@ impl<'w> EntityRef<'w> {
4242
}
4343
}
4444

45-
fn as_interior_mutable_readonly(&self) -> InteriorMutableEntityRef<'w> {
46-
InteriorMutableEntityRef::new(
47-
self.world.as_interior_mutable_readonly(),
45+
fn as_unsafe_world_cell_readonly(&self) -> UnsafeEntityRefCell<'w> {
46+
UnsafeEntityRefCell::new(
47+
self.world.as_unsafe_world_cell_readonly(),
4848
self.entity,
4949
self.location,
5050
)
@@ -89,15 +89,15 @@ impl<'w> EntityRef<'w> {
8989
#[inline]
9090
pub fn get<T: Component>(&self) -> Option<&'w T> {
9191
// SAFETY: &self implies shared access for duration of returned value
92-
unsafe { self.as_interior_mutable_readonly().get::<T>() }
92+
unsafe { self.as_unsafe_world_cell_readonly().get::<T>() }
9393
}
9494

9595
/// Retrieves the change ticks for the given component. This can be useful for implementing change
9696
/// detection in custom runtimes.
9797
#[inline]
9898
pub fn get_change_ticks<T: Component>(&self) -> Option<ComponentTicks> {
9999
// SAFETY: &self implies shared access
100-
unsafe { self.as_interior_mutable_readonly().get_change_ticks::<T>() }
100+
unsafe { self.as_unsafe_world_cell_readonly().get_change_ticks::<T>() }
101101
}
102102

103103
/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
@@ -110,7 +110,7 @@ impl<'w> EntityRef<'w> {
110110
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
111111
// SAFETY: &self implies shared access
112112
unsafe {
113-
self.as_interior_mutable_readonly()
113+
self.as_unsafe_world_cell_readonly()
114114
.get_change_ticks_by_id(component_id)
115115
}
116116
}
@@ -128,7 +128,7 @@ impl<'w> EntityRef<'w> {
128128
#[inline]
129129
pub fn get_by_id(&self, component_id: ComponentId) -> Option<Ptr<'w>> {
130130
// SAFETY: &self implies shared access for duration of returned value
131-
unsafe { self.as_interior_mutable_readonly().get_by_id(component_id) }
131+
unsafe { self.as_unsafe_world_cell_readonly().get_by_id(component_id) }
132132
}
133133
}
134134

@@ -148,15 +148,19 @@ pub struct EntityMut<'w> {
148148
}
149149

150150
impl<'w> EntityMut<'w> {
151-
fn as_interior_mutable_readonly(&self) -> InteriorMutableEntityRef<'_> {
152-
InteriorMutableEntityRef::new(
153-
self.world.as_interior_mutable_readonly(),
151+
fn as_unsafe_world_cell_readonly(&self) -> UnsafeEntityRefCell<'_> {
152+
UnsafeEntityRefCell::new(
153+
self.world.as_unsafe_world_cell_readonly(),
154154
self.entity,
155155
self.location,
156156
)
157157
}
158-
fn as_interior_mutable(&mut self) -> InteriorMutableEntityRef<'_> {
159-
InteriorMutableEntityRef::new(self.world.as_interior_mutable(), self.entity, self.location)
158+
fn as_unsafe_world_cell(&mut self) -> UnsafeEntityRefCell<'_> {
159+
UnsafeEntityRefCell::new(
160+
self.world.as_unsafe_world_cell(),
161+
self.entity,
162+
self.location,
163+
)
160164
}
161165

162166
/// # Safety
@@ -215,21 +219,21 @@ impl<'w> EntityMut<'w> {
215219
#[inline]
216220
pub fn get<T: Component>(&self) -> Option<&'_ T> {
217221
// SAFETY: &self implies shared access for duration of returned value
218-
unsafe { self.as_interior_mutable_readonly().get::<T>() }
222+
unsafe { self.as_unsafe_world_cell_readonly().get::<T>() }
219223
}
220224

221225
#[inline]
222226
pub fn get_mut<T: Component>(&mut self) -> Option<Mut<'_, T>> {
223227
// SAFETY: &mut self implies exclusive access for duration of returned value
224-
unsafe { self.as_interior_mutable().get_mut() }
228+
unsafe { self.as_unsafe_world_cell().get_mut() }
225229
}
226230

227231
/// Retrieves the change ticks for the given component. This can be useful for implementing change
228232
/// detection in custom runtimes.
229233
#[inline]
230234
pub fn get_change_ticks<T: Component>(&self) -> Option<ComponentTicks> {
231235
// SAFETY: &self implies shared access
232-
unsafe { self.as_interior_mutable_readonly().get_change_ticks::<T>() }
236+
unsafe { self.as_unsafe_world_cell_readonly().get_change_ticks::<T>() }
233237
}
234238

235239
/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
@@ -242,7 +246,7 @@ impl<'w> EntityMut<'w> {
242246
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
243247
// SAFETY: &self implies shared access
244248
unsafe {
245-
self.as_interior_mutable_readonly()
249+
self.as_unsafe_world_cell_readonly()
246250
.get_change_ticks_by_id(component_id)
247251
}
248252
}

crates/bevy_ecs/src/world/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod entity_ref;
2-
pub mod interior_mutable_world;
32
mod spawn_batch;
3+
pub mod unsafe_world_cell;
44
mod world_cell;
55

66
pub use crate::change_detection::{Mut, Ref, CHECK_TICK_THRESHOLD};
@@ -34,7 +34,7 @@ mod identifier;
3434

3535
pub use identifier::WorldId;
3636

37-
use self::interior_mutable_world::InteriorMutableWorld;
37+
use self::unsafe_world_cell::UnsafeWorldCell;
3838

3939
/// Stores and exposes operations on [entities](Entity), [components](Component), resources,
4040
/// and their associated metadata.
@@ -108,21 +108,21 @@ impl World {
108108
self.id
109109
}
110110

111-
/// Creates a new [`InteriorMutableWorld`] view with complete read+write access.
112-
pub fn as_interior_mutable(&mut self) -> InteriorMutableWorld<'_> {
113-
InteriorMutableWorld::new(self)
111+
/// Creates a new [`UnsafeWorldCell`] view with complete read+write access.
112+
pub fn as_unsafe_world_cell(&mut self) -> UnsafeWorldCell<'_> {
113+
UnsafeWorldCell::new(self)
114114
}
115115

116-
/// Creates a new [`InteriorMutableWorld`] view with only read access to everything.
117-
pub fn as_interior_mutable_readonly(&self) -> InteriorMutableWorld<'_> {
118-
InteriorMutableWorld::new(self)
116+
/// Creates a new [`UnsafeWorldCell`] view with only read access to everything.
117+
pub fn as_unsafe_world_cell_readonly(&self) -> UnsafeWorldCell<'_> {
118+
UnsafeWorldCell::new(self)
119119
}
120120

121-
/// Creates a new [`InteriorMutableWorld`] with read+write access from a [&World](World).
122-
/// This is only a temporary measure until every `&World` that is semantically a [`InteriorMutableWorld`]
121+
/// Creates a new [`UnsafeWorldCell`] with read+write access from a [&World](World).
122+
/// This is only a temporary measure until every `&World` that is semantically a [`UnsafeWorldCell`]
123123
/// has been replaced.
124-
pub(crate) fn as_interior_mutable_migration_internal(&self) -> InteriorMutableWorld<'_> {
125-
InteriorMutableWorld::new(self)
124+
pub(crate) fn as_unsafe_world_cell_migration_internal(&self) -> UnsafeWorldCell<'_> {
125+
UnsafeWorldCell::new(self)
126126
}
127127

128128
/// Retrieves this world's [Entities] collection
@@ -1011,7 +1011,7 @@ impl World {
10111011
#[inline]
10121012
pub fn get_resource_mut<R: Resource>(&mut self) -> Option<Mut<'_, R>> {
10131013
// SAFETY: unique world access
1014-
unsafe { self.as_interior_mutable().get_resource_mut() }
1014+
unsafe { self.as_unsafe_world_cell().get_resource_mut() }
10151015
}
10161016

10171017
/// Gets a mutable reference to the resource of type `T` if it exists,
@@ -1108,7 +1108,7 @@ impl World {
11081108
#[inline]
11091109
pub fn get_non_send_resource_mut<R: 'static>(&mut self) -> Option<Mut<'_, R>> {
11101110
// SAFETY: unique world access
1111-
unsafe { self.as_interior_mutable().get_non_send_resource_mut() }
1111+
unsafe { self.as_unsafe_world_cell().get_non_send_resource_mut() }
11121112
}
11131113

11141114
// Shorthand helper function for getting the data and change ticks for a resource.
@@ -1626,7 +1626,7 @@ impl World {
16261626
pub fn get_resource_mut_by_id(&mut self, component_id: ComponentId) -> Option<MutUntyped<'_>> {
16271627
// SAFETY: unique world access
16281628
unsafe {
1629-
self.as_interior_mutable()
1629+
self.as_unsafe_world_cell()
16301630
.get_resource_mut_by_id(component_id)
16311631
}
16321632
}

0 commit comments

Comments
 (0)