Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide access to world storages when using UnsafeWorldCell #8175

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions crates/bevy_ecs/src/world/unsafe_world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -280,8 +289,7 @@ impl<'w> UnsafeWorldCell<'w> {
pub unsafe fn get_resource_by_id(self, component_id: ComponentId) -> Option<Ptr<'w>> {
// 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()
Expand Down Expand Up @@ -323,8 +331,7 @@ impl<'w> UnsafeWorldCell<'w> {
pub unsafe fn get_non_send_resource_by_id(self, component_id: ComponentId) -> Option<Ptr<'w>> {
// 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()
Expand Down Expand Up @@ -367,8 +374,7 @@ impl<'w> UnsafeWorldCell<'w> {
) -> Option<MutUntyped<'w>> {
// 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()?;
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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]
Expand All @@ -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)
}
}

Expand Down