Skip to content

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

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

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
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
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
Loading