Skip to content

Commit d1e5702

Browse files
authored
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
1 parent a839c52 commit d1e5702

File tree

2 files changed

+11
-34
lines changed

2 files changed

+11
-34
lines changed

crates/bevy_ecs/src/world/deferred_world.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ impl<'w> DeferredWorld<'w> {
7575
&mut self,
7676
entity: Entity,
7777
) -> Option<Mut<T>> {
78-
// SAFETY:
79-
// - `as_unsafe_world_cell` is the only thing that is borrowing world
80-
// - `as_unsafe_world_cell` provides mutable permission to everything
81-
// - `&mut self` ensures no other borrows on world data
82-
unsafe { self.world.get_entity(entity)?.get_mut() }
78+
self.get_entity_mut(entity).ok()?.into_mut()
8379
}
8480

8581
/// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and
@@ -491,13 +487,10 @@ impl<'w> DeferredWorld<'w> {
491487
entity: Entity,
492488
component_id: ComponentId,
493489
) -> Option<MutUntyped<'_>> {
494-
// SAFETY: &mut self ensure that there are no outstanding accesses to the resource
495-
unsafe {
496-
self.world
497-
.get_entity(entity)?
498-
.get_mut_by_id(component_id)
499-
.ok()
500-
}
490+
self.get_entity_mut(entity)
491+
.ok()?
492+
.into_mut_by_id(component_id)
493+
.ok()
501494
}
502495

503496
/// Triggers all `on_add` hooks for [`ComponentId`] in target.

crates/bevy_ecs/src/world/mod.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,11 +1230,7 @@ impl World {
12301230
&mut self,
12311231
entity: Entity,
12321232
) -> Option<Mut<T>> {
1233-
// SAFETY:
1234-
// - `as_unsafe_world_cell` is the only thing that is borrowing world
1235-
// - `as_unsafe_world_cell` provides mutable permission to everything
1236-
// - `&mut self` ensures no other borrows on world data
1237-
unsafe { self.as_unsafe_world_cell().get_entity(entity)?.get_mut() }
1233+
self.get_entity_mut(entity).ok()?.into_mut()
12381234
}
12391235

12401236
/// Temporarily removes a [`Component`] `T` from the provided [`Entity`] and
@@ -3509,14 +3505,7 @@ impl World {
35093505
/// This function will panic if it isn't called from the same thread that the resource was inserted from.
35103506
#[inline]
35113507
pub fn get_by_id(&self, entity: Entity, component_id: ComponentId) -> Option<Ptr<'_>> {
3512-
// SAFETY:
3513-
// - `&self` ensures that all accessed data is not mutably aliased
3514-
// - `as_unsafe_world_cell_readonly` provides shared/readonly permission to the whole world
3515-
unsafe {
3516-
self.as_unsafe_world_cell_readonly()
3517-
.get_entity(entity)?
3518-
.get_by_id(component_id)
3519-
}
3508+
self.get_entity(entity).ok()?.get_by_id(component_id).ok()
35203509
}
35213510

35223511
/// Retrieves a mutable untyped reference to the given `entity`'s [`Component`] of the given [`ComponentId`].
@@ -3530,15 +3519,10 @@ impl World {
35303519
entity: Entity,
35313520
component_id: ComponentId,
35323521
) -> Option<MutUntyped<'_>> {
3533-
// SAFETY:
3534-
// - `&mut self` ensures that all accessed data is unaliased
3535-
// - `as_unsafe_world_cell` provides mutable permission to the whole world
3536-
unsafe {
3537-
self.as_unsafe_world_cell()
3538-
.get_entity(entity)?
3539-
.get_mut_by_id(component_id)
3540-
.ok()
3541-
}
3522+
self.get_entity_mut(entity)
3523+
.ok()?
3524+
.into_mut_by_id(component_id)
3525+
.ok()
35423526
}
35433527
}
35443528

0 commit comments

Comments
 (0)