Skip to content

Commit 053929d

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

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

crates/bevy_ecs/src/world/entity_ref.rs

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

856856
// SAFETY: EntityLocation must be valid
857857
#[inline]
858-
pub(crate) unsafe fn get_mut<T: Component>(
859-
world: &mut World,
858+
pub(crate) unsafe fn get_mut<'w, T: Component>(
859+
world: &'w mut World,
860860
entity: Entity,
861861
location: EntityLocation,
862862
) -> Option<Mut<'_, T>> {
863-
// SAFETY: world access is unique, entity location is valid, and returned component is of type
864-
// T, storage type is correct
865863
let change_tick = world.change_tick();
866864
let last_change_tick = world.last_change_tick();
865+
// SAFETY:
866+
// - world access is unique
867+
// - entity location is valid
868+
// - and returned component is of type T
869+
// - archetypes and components comes from the same world
867870
world
868871
.storages
869872
.get_component_and_ticks_with_type(
@@ -875,6 +878,9 @@ pub(crate) unsafe fn get_mut<T: Component>(
875878
location,
876879
)
877880
.map(|(value, ticks)| Mut {
881+
// SAFETY:
882+
// - world access is unique and ties world lifetime to `Mut` lifetime
883+
// - `value` is of type `T`
878884
value: value.assert_unique().deref_mut::<T>(),
879885
ticks: TicksMut::from_tick_cells(ticks, last_change_tick, change_tick),
880886
})
@@ -887,10 +893,15 @@ pub(crate) unsafe fn get_mut_by_id(
887893
entity: Entity,
888894
location: EntityLocation,
889895
component_id: ComponentId,
890-
) -> Option<MutUntyped> {
896+
) -> Option<MutUntyped<'_>> {
891897
let change_tick = world.change_tick();
898+
// SAFETY: component_id is valid
892899
let info = world.components.get_info_unchecked(component_id);
893-
// SAFETY: world access is unique, entity location and component_id required to be valid, storage_type is correct
900+
// SAFETY:
901+
// - world access is unique
902+
// - entity location is valid
903+
// - and returned component is of type T
904+
// - archetypes and components comes from the same world
894905
world
895906
.storages
896907
.get_component_and_ticks(
@@ -901,6 +912,7 @@ pub(crate) unsafe fn get_mut_by_id(
901912
location,
902913
)
903914
.map(|(value, ticks)| MutUntyped {
915+
// SAFETY: world access is unique and ties world lifetime to `MutUntyped` lifetime
904916
value: value.assert_unique(),
905917
ticks: TicksMut::from_tick_cells(ticks, world.last_change_tick(), change_tick),
906918
})

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, Ref};
6-
pub use entity_ref::*;
6+
pub use entity_ref::{EntityMut, EntityRef};
77
pub use spawn_batch::*;
88
pub use world_cell::*;
99

@@ -564,8 +564,10 @@ impl World {
564564
/// ```
565565
#[inline]
566566
pub fn get_mut<T: Component>(&mut self, entity: Entity) -> Option<Mut<T>> {
567-
// SAFETY: lifetimes enforce correct usage of returned borrow
568-
unsafe { get_mut(self, entity, self.get_entity(entity)?.location()) }
567+
// SAFETY:
568+
// - lifetimes enforce correct usage of returned borrow
569+
// - entity location is checked in `get_entity`
570+
unsafe { entity_ref::get_mut(self, entity, self.get_entity(entity)?.location()) }
569571
}
570572

571573
/// Despawns the given `entity`, if it exists. This will also remove all of the entity's
@@ -1751,7 +1753,7 @@ impl World {
17511753
self.components().get_info(component_id)?;
17521754
// SAFETY: entity_location is valid, component_id is valid as checked by the line above
17531755
unsafe {
1754-
get_mut_by_id(
1756+
entity_ref::get_mut_by_id(
17551757
self,
17561758
entity,
17571759
self.get_entity(entity)?.location(),

0 commit comments

Comments
 (0)