Replace unsafe blocks in World and DeferredWorld with safe equivalents (#17206)

# Objective

Reduce the number of unsafe blocks.

## Solution

Replaced 5 unsafe blocks with safe equivalents.

## Testing

Reusing current tests
This commit is contained in:
Christian Hughes 2025-01-06 21:21:47 -06:00 committed by GitHub
parent a839c525c1
commit d1e5702020
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 34 deletions

View File

@ -75,11 +75,7 @@ impl<'w> DeferredWorld<'w> {
&mut self, &mut self,
entity: Entity, entity: Entity,
) -> Option<Mut<T>> { ) -> Option<Mut<T>> {
// SAFETY: self.get_entity_mut(entity).ok()?.into_mut()
// - `as_unsafe_world_cell` is the only thing that is borrowing world
// - `as_unsafe_world_cell` provides mutable permission to everything
// - `&mut self` ensures no other borrows on world data
unsafe { self.world.get_entity(entity)?.get_mut() }
} }
/// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and /// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and
@ -491,14 +487,11 @@ impl<'w> DeferredWorld<'w> {
entity: Entity, entity: Entity,
component_id: ComponentId, component_id: ComponentId,
) -> Option<MutUntyped<'_>> { ) -> Option<MutUntyped<'_>> {
// SAFETY: &mut self ensure that there are no outstanding accesses to the resource self.get_entity_mut(entity)
unsafe { .ok()?
self.world .into_mut_by_id(component_id)
.get_entity(entity)?
.get_mut_by_id(component_id)
.ok() .ok()
} }
}
/// Triggers all `on_add` hooks for [`ComponentId`] in target. /// Triggers all `on_add` hooks for [`ComponentId`] in target.
/// ///

View File

@ -1230,11 +1230,7 @@ impl World {
&mut self, &mut self,
entity: Entity, entity: Entity,
) -> Option<Mut<T>> { ) -> Option<Mut<T>> {
// SAFETY: self.get_entity_mut(entity).ok()?.into_mut()
// - `as_unsafe_world_cell` is the only thing that is borrowing world
// - `as_unsafe_world_cell` provides mutable permission to everything
// - `&mut self` ensures no other borrows on world data
unsafe { self.as_unsafe_world_cell().get_entity(entity)?.get_mut() }
} }
/// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and /// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and
@ -3509,14 +3505,7 @@ impl World {
/// This function will panic if it isn't called from the same thread that the resource was inserted from. /// This function will panic if it isn't called from the same thread that the resource was inserted from.
#[inline] #[inline]
pub fn get_by_id(&self, entity: Entity, component_id: ComponentId) -> Option<Ptr<'_>> { pub fn get_by_id(&self, entity: Entity, component_id: ComponentId) -> Option<Ptr<'_>> {
// SAFETY: self.get_entity(entity).ok()?.get_by_id(component_id).ok()
// - `&self` ensures that all accessed data is not mutably aliased
// - `as_unsafe_world_cell_readonly` provides shared/readonly permission to the whole world
unsafe {
self.as_unsafe_world_cell_readonly()
.get_entity(entity)?
.get_by_id(component_id)
}
} }
/// Retrieves a mutable untyped reference to the given `entity`'s [`Component`] of the given [`ComponentId`]. /// Retrieves a mutable untyped reference to the given `entity`'s [`Component`] of the given [`ComponentId`].
@ -3530,16 +3519,11 @@ impl World {
entity: Entity, entity: Entity,
component_id: ComponentId, component_id: ComponentId,
) -> Option<MutUntyped<'_>> { ) -> Option<MutUntyped<'_>> {
// SAFETY: self.get_entity_mut(entity)
// - `&mut self` ensures that all accessed data is unaliased .ok()?
// - `as_unsafe_world_cell` provides mutable permission to the whole world .into_mut_by_id(component_id)
unsafe {
self.as_unsafe_world_cell()
.get_entity(entity)?
.get_mut_by_id(component_id)
.ok() .ok()
} }
}
} }
// Schedule-related methods // Schedule-related methods