Skip to content

Commit b52858e

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

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
@@ -800,15 +800,18 @@ fn sorted_remove<T: Eq + Ord + Copy>(source: &mut Vec<T>, remove: &[T]) {
800800

801801
// SAFETY: EntityLocation must be valid
802802
#[inline]
803-
pub(crate) unsafe fn get_mut<T: Component>(
804-
world: &mut World,
803+
pub(crate) unsafe fn get_mut<'w, T: Component>(
804+
world: &'w mut World,
805805
entity: Entity,
806806
location: EntityLocation,
807-
) -> Option<Mut<'_, T>> {
808-
// SAFETY: world access is unique, entity location is valid, and returned component is of type
809-
// T
807+
) -> Option<Mut<'w, T>> {
810808
let change_tick = world.change_tick();
811809
let last_change_tick = world.last_change_tick();
810+
// SAFETY:
811+
// - world access is unique
812+
// - entity location is valid
813+
// - and returned component is of type T
814+
// - archetypes and components comes from the same world
812815
world
813816
.storages
814817
.get_component_and_ticks_with_type(
@@ -819,8 +822,12 @@ pub(crate) unsafe fn get_mut<T: Component>(
819822
location,
820823
)
821824
.map(|(value, ticks)| Mut {
825+
// SAFETY:
826+
// - world access is unique and ties world lifetime to `Mut` lifetime
827+
// - `value` is of type `T`
822828
value: value.assert_unique().deref_mut::<T>(),
823829
ticks: Ticks {
830+
// SAFETY: world access is unique and ties world lifetime to `Mut` lifetime
824831
component_ticks: ticks.deref_mut(),
825832
last_change_tick,
826833
change_tick,
@@ -836,7 +843,11 @@ pub(crate) unsafe fn get_mut_by_id(
836843
location: EntityLocation,
837844
component_id: ComponentId,
838845
) -> Option<MutUntyped> {
839-
// SAFETY: world access is unique, entity location and component_id required to be valid
846+
// SAFETY:
847+
// - world access is unique
848+
// - entity location is valid
849+
// - and returned component is of type T
850+
// - archetypes and components comes from the same world
840851
world
841852
.storages
842853
.get_component_and_ticks(
@@ -847,8 +858,10 @@ pub(crate) unsafe fn get_mut_by_id(
847858
location,
848859
)
849860
.map(|(value, ticks)| MutUntyped {
861+
// SAFETY: world access is unique and ties world lifetime to `MutUntyped` lifetime
850862
value: value.assert_unique(),
851863
ticks: Ticks {
864+
// SAFETY: world access is unique and ties world lifetime to `Mut` lifetime
852865
component_ticks: ticks.deref_mut(),
853866
last_change_tick: world.last_change_tick(),
854867
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)