diff --git a/crates/bevy_ecs/src/world/unsafe_world_cell.rs b/crates/bevy_ecs/src/world/unsafe_world_cell.rs index 99231fe3f32ef..f30b6873ada15 100644 --- a/crates/bevy_ecs/src/world/unsafe_world_cell.rs +++ b/crates/bevy_ecs/src/world/unsafe_world_cell.rs @@ -10,7 +10,7 @@ use crate::{ }, entity::{Entities, Entity, EntityLocation}, prelude::Component, - storage::{Column, ComponentSparseSet}, + storage::{Column, ComponentSparseSet, Storages}, system::Resource, }; use bevy_ptr::Ptr; @@ -183,15 +183,24 @@ impl<'w> UnsafeWorldCell<'w> { &unsafe { self.world_metadata() }.bundles } + /// Provides access to the underlying data storages of the world. + /// + /// # Safety + /// The caller must only access world data that this [`UnsafeWorldCell`] + /// has been given access to. + pub unsafe fn storages(self) -> &'w Storages { + // SAFETY: + // - Caller ensures they will only access world + // data that this instance is allowed to access. + unsafe { self.unsafe_world() }.storages() + } + /// Reads the current change tick of this world. #[inline] pub fn read_change_tick(self) -> Tick { // SAFETY: // - we only access world metadata - let tick = unsafe { self.world_metadata() } - .change_tick - .load(Ordering::Acquire); - Tick::new(tick) + unsafe { self.world_metadata() }.read_change_tick() } #[inline] @@ -280,8 +289,7 @@ impl<'w> UnsafeWorldCell<'w> { pub unsafe fn get_resource_by_id(self, component_id: ComponentId) -> Option> { // SAFETY: caller ensures that `self` has permission to access `R` // caller ensures that no mutable reference exists to `R` - unsafe { self.unsafe_world() } - .storages + unsafe { self.storages() } .resources .get(component_id)? .get_data() @@ -323,8 +331,7 @@ impl<'w> UnsafeWorldCell<'w> { pub unsafe fn get_non_send_resource_by_id(self, component_id: ComponentId) -> Option> { // SAFETY: we only access data on world that the caller has ensured is unaliased and we have // permission to access. - unsafe { self.unsafe_world() } - .storages + unsafe { self.storages() } .non_send_resources .get(component_id)? .get_data() @@ -367,8 +374,7 @@ impl<'w> UnsafeWorldCell<'w> { ) -> Option> { // SAFETY: we only access data that the caller has ensured is unaliased and `self` // has permission to access. - let (ptr, ticks) = unsafe { self.unsafe_world() } - .storages + let (ptr, ticks) = unsafe { self.storages() } .resources .get(component_id)? .get_with_ticks()?; @@ -428,8 +434,7 @@ impl<'w> UnsafeWorldCell<'w> { let change_tick = self.read_change_tick(); // SAFETY: we only access data that the caller has ensured is unaliased and `self` // has permission to access. - let (ptr, ticks) = unsafe { self.unsafe_world() } - .storages + let (ptr, ticks) = unsafe { self.storages() } .non_send_resources .get(component_id)? .get_with_ticks()?; @@ -462,8 +467,7 @@ impl<'w> UnsafeWorldCell<'w> { // - caller ensures there is no `&mut World` // - caller ensures there are no mutable borrows of this resource // - caller ensures that we have permission to access this resource - unsafe { self.unsafe_world() } - .storages + unsafe { self.storages() } .resources .get(component_id)? .get_with_ticks() @@ -487,8 +491,7 @@ impl<'w> UnsafeWorldCell<'w> { // - caller ensures there is no `&mut World` // - caller ensures there are no mutable borrows of this resource // - caller ensures that we have permission to access this resource - unsafe { self.unsafe_world() } - .storages + unsafe { self.storages() } .non_send_resources .get(component_id)? .get_with_ticks() @@ -758,7 +761,7 @@ impl<'w> UnsafeWorldCell<'w> { ) -> Option<&'w Column> { // SAFETY: caller ensures returned data is not misused and we have not created any borrows // of component/resource data - unsafe { self.unsafe_world() }.storages.tables[location.table_id].get_column(component_id) + unsafe { self.storages() }.tables[location.table_id].get_column(component_id) } #[inline] @@ -769,10 +772,7 @@ impl<'w> UnsafeWorldCell<'w> { unsafe fn fetch_sparse_set(self, component_id: ComponentId) -> Option<&'w ComponentSparseSet> { // SAFETY: caller ensures returned data is not misused and we have not created any borrows // of component/resource data - unsafe { self.unsafe_world() } - .storages - .sparse_sets - .get(component_id) + unsafe { self.storages() }.sparse_sets.get(component_id) } }