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:
parent
a839c525c1
commit
d1e5702020
@ -75,11 +75,7 @@ impl<'w> DeferredWorld<'w> {
|
||||
&mut self,
|
||||
entity: Entity,
|
||||
) -> Option<Mut<T>> {
|
||||
// SAFETY:
|
||||
// - `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() }
|
||||
self.get_entity_mut(entity).ok()?.into_mut()
|
||||
}
|
||||
|
||||
/// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and
|
||||
@ -491,13 +487,10 @@ impl<'w> DeferredWorld<'w> {
|
||||
entity: Entity,
|
||||
component_id: ComponentId,
|
||||
) -> Option<MutUntyped<'_>> {
|
||||
// SAFETY: &mut self ensure that there are no outstanding accesses to the resource
|
||||
unsafe {
|
||||
self.world
|
||||
.get_entity(entity)?
|
||||
.get_mut_by_id(component_id)
|
||||
.ok()
|
||||
}
|
||||
self.get_entity_mut(entity)
|
||||
.ok()?
|
||||
.into_mut_by_id(component_id)
|
||||
.ok()
|
||||
}
|
||||
|
||||
/// Triggers all `on_add` hooks for [`ComponentId`] in target.
|
||||
|
@ -1230,11 +1230,7 @@ impl World {
|
||||
&mut self,
|
||||
entity: Entity,
|
||||
) -> Option<Mut<T>> {
|
||||
// SAFETY:
|
||||
// - `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() }
|
||||
self.get_entity_mut(entity).ok()?.into_mut()
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[inline]
|
||||
pub fn get_by_id(&self, entity: Entity, component_id: ComponentId) -> Option<Ptr<'_>> {
|
||||
// SAFETY:
|
||||
// - `&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)
|
||||
}
|
||||
self.get_entity(entity).ok()?.get_by_id(component_id).ok()
|
||||
}
|
||||
|
||||
/// Retrieves a mutable untyped reference to the given `entity`'s [`Component`] of the given [`ComponentId`].
|
||||
@ -3530,15 +3519,10 @@ impl World {
|
||||
entity: Entity,
|
||||
component_id: ComponentId,
|
||||
) -> Option<MutUntyped<'_>> {
|
||||
// SAFETY:
|
||||
// - `&mut self` ensures that all accessed data is unaliased
|
||||
// - `as_unsafe_world_cell` provides mutable permission to the whole world
|
||||
unsafe {
|
||||
self.as_unsafe_world_cell()
|
||||
.get_entity(entity)?
|
||||
.get_mut_by_id(component_id)
|
||||
.ok()
|
||||
}
|
||||
self.get_entity_mut(entity)
|
||||
.ok()?
|
||||
.into_mut_by_id(component_id)
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user