From 19a9a4d3a7c2ba6bbc25478df807399d80f39867 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Tue, 9 Aug 2022 16:19:34 +0000 Subject: [PATCH] remove `ReflectMut` in favor of `Mut` (#5630) # Objective - `ReflectMut` served no purpose that wasn't met by `Mut` which is easier to understand since you have to deal with fewer types - there is another `ReflectMut` type that could be confused with this one ## Solution/Changelog - relax `T: ?Sized` bound in `Mut` - replace all instances of `ReflectMut` with `Mut` --- crates/bevy_ecs/src/change_detection.rs | 15 +---------- crates/bevy_ecs/src/reflect.rs | 33 ++++++++++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index f74ed12fa6645e..b35d996f1a5e08 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -2,7 +2,6 @@ use crate::{component::ComponentTicks, ptr::PtrMut, system::Resource}; #[cfg(feature = "bevy_reflect")] -use bevy_reflect::Reflect; use std::ops::{Deref, DerefMut}; /// The (arbitrarily chosen) minimum number of world tick increments between `check_tick` scans. @@ -230,7 +229,7 @@ impl<'a, T: 'static> From> for Mut<'a, T> { } /// Unique mutable borrow of an entity's component -pub struct Mut<'a, T> { +pub struct Mut<'a, T: ?Sized> { pub(crate) value: &'a mut T, pub(crate) ticks: Ticks<'a>, } @@ -239,18 +238,6 @@ change_detection_impl!(Mut<'a, T>, T,); impl_into_inner!(Mut<'a, T>, T,); impl_debug!(Mut<'a, T>,); -/// Unique mutable borrow of a reflected component or resource -#[cfg(feature = "bevy_reflect")] -pub struct ReflectMut<'a> { - pub(crate) value: &'a mut dyn Reflect, - pub(crate) ticks: Ticks<'a>, -} - -#[cfg(feature = "bevy_reflect")] -change_detection_impl!(ReflectMut<'a>, dyn Reflect,); -#[cfg(feature = "bevy_reflect")] -impl_into_inner!(ReflectMut<'a>, dyn Reflect,); - /// Unique mutable borrow of resources or an entity's component. /// /// Similar to [`Mut`], but not generic over the component type, instead diff --git a/crates/bevy_ecs/src/reflect.rs b/crates/bevy_ecs/src/reflect.rs index 0b288b3aa7ac0f..b1ad42b4d1899f 100644 --- a/crates/bevy_ecs/src/reflect.rs +++ b/crates/bevy_ecs/src/reflect.rs @@ -1,7 +1,7 @@ //! Types that enable reflection support. -pub use crate::change_detection::ReflectMut; use crate::{ + change_detection::Mut, component::Component, entity::{Entity, EntityMap, MapEntities, MapEntitiesError}, system::Resource, @@ -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: unsafe fn(&World, Entity) -> Option>, copy: fn(&World, &mut World, Entity, Entity), } @@ -70,7 +70,11 @@ 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> { + pub fn reflect_mut<'a>( + &self, + world: &'a mut World, + entity: Entity, + ) -> Option> { // SAFETY: unique world access unsafe { (self.reflect_mut)(world, entity) } } @@ -85,7 +89,7 @@ impl ReflectComponent { &self, world: &'a World, entity: Entity, - ) -> Option> { + ) -> Option> { (self.reflect_mut)(world, entity) } @@ -155,7 +159,7 @@ impl FromType for ReflectComponent { world .get_entity(entity)? .get_unchecked_mut::(world.last_change_tick(), world.read_change_tick()) - .map(|c| ReflectMut { + .map(|c| Mut { value: c.value as &mut dyn Reflect, ticks: c.ticks, }) @@ -176,7 +180,7 @@ pub struct ReflectResource { apply_or_insert: fn(&mut World, &dyn Reflect), remove: fn(&mut World), reflect: fn(&World) -> Option<&dyn Reflect>, - reflect_unchecked_mut: unsafe fn(&World) -> Option, + reflect_unchecked_mut: unsafe fn(&World) -> Option>, copy: fn(&World, &mut World), } @@ -211,7 +215,7 @@ impl ReflectResource { } /// Gets the value of this [`Resource`] type from the world as a mutable reflected reference. - pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option> { + pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option> { // SAFETY: unique world access unsafe { (self.reflect_unchecked_mut)(world) } } @@ -222,7 +226,10 @@ impl ReflectResource { /// * 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 [`Resource`]. - pub unsafe fn reflect_unchecked_mut<'a>(&self, world: &'a World) -> Option> { + pub unsafe fn reflect_unchecked_mut<'a>( + &self, + world: &'a World, + ) -> Option> { // SAFETY: caller promises to uphold uniqueness guarantees (self.reflect_unchecked_mut)(world) } @@ -266,12 +273,10 @@ impl FromType for ReflectResource { // SAFETY: all usages of `reflect_unchecked_mut` guarantee that there is either a single mutable // reference or multiple immutable ones alive at any given point unsafe { - world - .get_resource_unchecked_mut::() - .map(|res| ReflectMut { - value: res.value as &mut dyn Reflect, - ticks: res.ticks, - }) + world.get_resource_unchecked_mut::().map(|res| Mut { + value: res.value as &mut dyn Reflect, + ticks: res.ticks, + }) } }, copy: |source_world, destination_world| {