Skip to content

Commit 63a0f41

Browse files
don't use entity_ref::* import in world.rs to distinguish public and private imports, add safety comments
1 parent f850c88 commit 63a0f41

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -808,15 +808,18 @@ fn sorted_remove<T: Eq + Ord + Copy>(source: &mut Vec<T>, remove: &[T]) {
808808

809809
// SAFETY: EntityLocation must be valid
810810
#[inline]
811-
pub(crate) unsafe fn get_mut<T: Component>(
812-
world: &mut World,
811+
pub(crate) unsafe fn get_mut<'w, T: Component>(
812+
world: &'w mut World,
813813
entity: Entity,
814814
location: EntityLocation,
815-
) -> Option<Mut<'_, T>> {
816-
// SAFETY: world access is unique, entity location is valid, and returned component is of type
817-
// T
815+
) -> Option<Mut<'w, T>> {
818816
let change_tick = world.change_tick();
819817
let last_change_tick = world.last_change_tick();
818+
// SAFETY:
819+
// - world access is unique
820+
// - entity location is valid
821+
// - and returned component is of type T
822+
// - archetypes and components comes from the same world
820823
world
821824
.storages
822825
.get_component_and_ticks_with_type(
@@ -827,8 +830,12 @@ pub(crate) unsafe fn get_mut<T: Component>(
827830
location,
828831
)
829832
.map(|(value, ticks)| Mut {
833+
// SAFETY:
834+
// - world access is unique and ties world lifetime to `Mut` lifetime
835+
// - `value` is of type `T`
830836
value: value.assert_unique().deref_mut::<T>(),
831837
ticks: Ticks {
838+
// SAFETY: world access is unique and ties world lifetime to `Mut` lifetime
832839
component_ticks: ticks.deref_mut(),
833840
last_change_tick,
834841
change_tick,
@@ -844,7 +851,11 @@ pub(crate) unsafe fn get_mut_by_id(
844851
location: EntityLocation,
845852
component_id: ComponentId,
846853
) -> Option<MutUntyped> {
847-
// SAFETY: world access is unique, entity location and component_id required to be valid
854+
// SAFETY:
855+
// - world access is unique
856+
// - entity location is valid
857+
// - and returned component is of type T
858+
// - archetypes and components comes from the same world
848859
world
849860
.storages
850861
.get_component_and_ticks(
@@ -855,8 +866,10 @@ pub(crate) unsafe fn get_mut_by_id(
855866
location,
856867
)
857868
.map(|(value, ticks)| MutUntyped {
869+
// SAFETY: world access is unique and ties world lifetime to `MutUntyped` lifetime
858870
value: value.assert_unique(),
859871
ticks: Ticks {
872+
// SAFETY: world access is unique and ties world lifetime to `Mut` lifetime
860873
component_ticks: ticks.deref_mut(),
861874
last_change_tick: world.last_change_tick(),
862875
change_tick: world.read_change_tick(),

crates/bevy_ecs/src/world/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod spawn_batch;
33
mod world_cell;
44

55
pub use crate::change_detection::Mut;
6-
pub use entity_ref::*;
6+
pub use entity_ref::{EntityMut, EntityRef};
77
pub use spawn_batch::*;
88
pub use world_cell::*;
99

@@ -557,8 +557,10 @@ impl World {
557557
/// ```
558558
#[inline]
559559
pub fn get_mut<T: Component>(&mut self, entity: Entity) -> Option<Mut<T>> {
560-
// SAFETY: lifetimes enforce correct usage of returned borrow
561-
unsafe { get_mut(self, entity, self.get_entity(entity)?.location()) }
560+
// SAFETY:
561+
// - lifetimes enforce correct usage of returned borrow
562+
// - entity location is checked in `get_entity`
563+
unsafe { entity_ref::get_mut(self, entity, self.get_entity(entity)?.location()) }
562564
}
563565

564566
/// Despawns the given `entity`, if it exists. This will also remove all of the entity's
@@ -1527,7 +1529,7 @@ impl World {
15271529
self.components().get_info(component_id)?;
15281530
// SAFETY: entity_location is valid, component_id is valid as checked by the line above
15291531
unsafe {
1530-
get_mut_by_id(
1532+
entity_ref::get_mut_by_id(
15311533
self,
15321534
entity,
15331535
self.get_entity(entity)?.location(),

0 commit comments

Comments
 (0)