diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index fc863651c2b994..727aab46282c7a 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -23,7 +23,6 @@ use bevy_ptr::{OwningPtr, Ptr}; use bevy_utils::tracing::warn; use std::{ any::TypeId, - cell::UnsafeCell, fmt, sync::atomic::{AtomicU32, Ordering}, }; @@ -61,7 +60,7 @@ pub struct World { pub(crate) bundles: Bundles, pub(crate) removed_components: SparseSet>, /// Access cache used by [WorldCell]. Is only accessed in the `Drop` impl of `WorldCell`. - pub(crate) archetype_component_access: UnsafeCell, + pub(crate) archetype_component_access: ArchetypeComponentAccess, pub(crate) change_tick: AtomicU32, pub(crate) last_change_tick: u32, pub(crate) last_check_tick: u32, diff --git a/crates/bevy_ecs/src/world/world_cell.rs b/crates/bevy_ecs/src/world/world_cell.rs index 2af1548e0ccfe2..f838c0162b3db5 100644 --- a/crates/bevy_ecs/src/world/world_cell.rs +++ b/crates/bevy_ecs/src/world/world_cell.rs @@ -80,10 +80,10 @@ impl<'w> Drop for WorldCell<'w> { let mut access = self.access.borrow_mut(); { - // SAFETY: we only swap `archetype_component_access` - let world = unsafe { self.world.world() }; - // SAFETY: the WorldCell has exclusive world access - let world_cached_access = unsafe { &mut *world.archetype_component_access.get() }; + // SAFETY: `WorldCell` does not hand out `UnsafeWorldCell` to anywhere else so this is the only + // `UnsafeWorldCell` and we have exclusive access to it. + let world = unsafe { self.world.world_mut() }; + let world_cached_access = &mut world.archetype_component_access; // give world ArchetypeComponentAccess back to reuse allocations std::mem::swap(world_cached_access, &mut *access); @@ -185,7 +185,7 @@ impl<'w> WorldCell<'w> { pub(crate) fn new(world: &'w mut World) -> Self { // this is cheap because ArchetypeComponentAccess::new() is const / allocation free let access = std::mem::replace( - world.archetype_component_access.get_mut(), + &mut world.archetype_component_access, ArchetypeComponentAccess::new(), ); // world's ArchetypeComponentAccess is recycled to cut down on allocations @@ -435,11 +435,10 @@ mod tests { let u32_archetype_component_id = world .get_resource_archetype_component_id(u32_component_id) .unwrap(); - assert_eq!(world.archetype_component_access.get_mut().access.len(), 1); + assert_eq!(world.archetype_component_access.access.len(), 1); assert_eq!( world .archetype_component_access - .get_mut() .access .get(u32_archetype_component_id), Some(&BASE_ACCESS),