From e6d19055fea30aaff517f16da163026bcb68f05a Mon Sep 17 00:00:00 2001 From: Ellen Date: Sun, 7 Aug 2022 22:02:01 +0100 Subject: [PATCH] a --- crates/bevy_ecs/src/reflect.rs | 32 ++++--------------------- crates/bevy_ecs/src/system/query.rs | 2 +- crates/bevy_ecs/src/world/entity_ref.rs | 4 +++- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/crates/bevy_ecs/src/reflect.rs b/crates/bevy_ecs/src/reflect.rs index 0b288b3aa7ac0..e90eec2769a1e 100644 --- a/crates/bevy_ecs/src/reflect.rs +++ b/crates/bevy_ecs/src/reflect.rs @@ -23,7 +23,7 @@ pub struct ReflectComponent { apply_or_insert: fn(&mut World, Entity, &dyn Reflect), remove: fn(&mut World, Entity), reflect: fn(&World, Entity) -> Option<&dyn Reflect>, - reflect_mut: unsafe fn(&World, Entity) -> Option, + reflect_mut: fn(&mut World, Entity) -> Option, copy: fn(&World, &mut World, Entity, Entity), } @@ -71,21 +71,6 @@ impl ReflectComponent { /// Gets the value of this [`Component`] type from the entity as a mutable reflected reference. pub fn reflect_mut<'a>(&self, world: &'a mut World, entity: Entity) -> Option> { - // SAFETY: unique world access - unsafe { (self.reflect_mut)(world, entity) } - } - - /// # Safety - /// This method does not prevent you from having two mutable pointers to the same data, - /// violating Rust's aliasing rules. To avoid this: - /// * Only call this method in an exclusive system to avoid sharing across threads (or use a - /// scheduler that enforces safe memory access). - /// * Don't call this method more than once in the same scope for a given [`Component`]. - pub unsafe fn reflect_unchecked_mut<'a>( - &self, - world: &'a World, - entity: Entity, - ) -> Option> { (self.reflect_mut)(world, entity) } @@ -149,17 +134,10 @@ impl FromType for ReflectComponent { .map(|c| c as &dyn Reflect) }, reflect_mut: |world, entity| { - // SAFETY: reflect_mut is an unsafe function pointer used by `reflect_unchecked_mut` which promises to never - // produce aliasing mutable references, and reflect_mut, which has mutable world access - unsafe { - world - .get_entity(entity)? - .get_unchecked_mut::(world.last_change_tick(), world.read_change_tick()) - .map(|c| ReflectMut { - value: c.value as &mut dyn Reflect, - ticks: c.ticks, - }) - } + world.get_mut::(entity).map(|c| ReflectMut { + value: c.value as &mut dyn Reflect, + ticks: c.ticks, + }) }, } } diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 262d12436a5aa..261488dcf9e3d 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -1074,7 +1074,7 @@ impl<'w, 's, Q: WorldQuery, F: WorldQuery> Query<'w, 's, Q, F> { .has_write(archetype_component) { entity_ref - .get_unchecked_mut::(self.last_change_tick, self.change_tick) + .__get_unchecked_mut::(self.last_change_tick, self.change_tick) .ok_or(QueryComponentError::MissingComponent) } else { Err(QueryComponentError::MissingWriteAccess) diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index 7d1051bba1988..082d7355c6668 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -83,6 +83,8 @@ impl<'w> EntityRef<'w> { } } + /// Do not use this. It will be removed eventually and only exists for `Query::get_component_unchecked_mut`. + /// /// Gets a mutable reference to the component of type `T` associated with /// this entity without ensuring there are no other borrows active and without /// ensuring that the returned reference will stay valid. @@ -94,7 +96,7 @@ impl<'w> EntityRef<'w> { /// may happen from **any** `insert_component`, `remove_component` or `despawn` /// operation on this world (non-exhaustive list). #[inline] - pub unsafe fn get_unchecked_mut( + pub(crate) unsafe fn __get_unchecked_mut( &self, last_change_tick: u32, change_tick: u32,