Skip to content

Commit

Permalink
Replace unsafe blocks in World and DeferredWorld with safe equiva…
Browse files Browse the repository at this point in the history
…lents (#17206)

# Objective

Reduce the number of unsafe blocks.

## Solution

Replaced 5 unsafe blocks with safe equivalents.

## Testing

Reusing current tests
  • Loading branch information
ItsDoot authored Jan 7, 2025
1 parent a839c52 commit d1e5702
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 34 deletions.
17 changes: 5 additions & 12 deletions crates/bevy_ecs/src/world/deferred_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 6 additions & 22 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`].
Expand All @@ -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()
}
}

Expand Down

0 comments on commit d1e5702

Please sign in to comment.