Use map_unchanged
in reflection instead of creating a Mut
manually. (#14692)
# Objective The code to create `ReflectComponent` and `ReflectResource` instances manually constructs a `Mut<dyn Reflect>` by copying everything but `value`. That can be done more concisely and better respecting encapsulation by calling the `map_unchanged()` method. ## Solution Use `map_unchanged` instead of creating a `Mut` manually. --------- Co-authored-by: radiish <cb.setho@gmail.com>
This commit is contained in:
parent
d849941dac
commit
0ea46663b0
@ -293,24 +293,15 @@ impl<C: Component + Reflect + TypePath> FromType<C> for ReflectComponent {
|
|||||||
},
|
},
|
||||||
reflect: |entity| entity.get::<C>().map(|c| c as &dyn Reflect),
|
reflect: |entity| entity.get::<C>().map(|c| c as &dyn Reflect),
|
||||||
reflect_mut: |entity| {
|
reflect_mut: |entity| {
|
||||||
entity.into_mut::<C>().map(|c| Mut {
|
entity
|
||||||
value: c.value as &mut dyn Reflect,
|
.into_mut::<C>()
|
||||||
ticks: c.ticks,
|
.map(|c| c.map_unchanged(|value| value as &mut dyn Reflect))
|
||||||
#[cfg(feature = "track_change_detection")]
|
|
||||||
changed_by: c.changed_by,
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
reflect_unchecked_mut: |entity| {
|
reflect_unchecked_mut: |entity| {
|
||||||
// SAFETY: reflect_unchecked_mut is an unsafe function pointer used by
|
// SAFETY: reflect_unchecked_mut is an unsafe function pointer used by
|
||||||
// `reflect_unchecked_mut` which must be called with an UnsafeEntityCell with access to the component `C` on the `entity`
|
// `reflect_unchecked_mut` which must be called with an UnsafeEntityCell with access to the component `C` on the `entity`
|
||||||
unsafe {
|
let c = unsafe { entity.get_mut::<C>() };
|
||||||
entity.get_mut::<C>().map(|c| Mut {
|
c.map(|c| c.map_unchanged(|value| value as &mut dyn Reflect))
|
||||||
value: c.value as &mut dyn Reflect,
|
|
||||||
ticks: c.ticks,
|
|
||||||
#[cfg(feature = "track_change_detection")]
|
|
||||||
changed_by: c.changed_by,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -208,14 +208,8 @@ impl<R: Resource + FromReflect + TypePath> FromType<R> for ReflectResource {
|
|||||||
reflect_unchecked_mut: |world| {
|
reflect_unchecked_mut: |world| {
|
||||||
// SAFETY: all usages of `reflect_unchecked_mut` guarantee that there is either a single mutable
|
// 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
|
// reference or multiple immutable ones alive at any given point
|
||||||
unsafe {
|
let res = unsafe { world.get_resource_mut::<R>() };
|
||||||
world.get_resource_mut::<R>().map(|res| Mut {
|
res.map(|res| res.map_unchanged(|value| value as &mut dyn Reflect))
|
||||||
value: res.value as &mut dyn Reflect,
|
|
||||||
ticks: res.ticks,
|
|
||||||
#[cfg(feature = "track_change_detection")]
|
|
||||||
changed_by: res.changed_by,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
copy: |source_world, destination_world, registry| {
|
copy: |source_world, destination_world, registry| {
|
||||||
let source_resource = source_world.resource::<R>();
|
let source_resource = source_world.resource::<R>();
|
||||||
|
Loading…
Reference in New Issue
Block a user