From 59751d6e33d94eff6e1fc20c9ae155974b3860b1 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:47:54 +0000 Subject: [PATCH] Add a method for converting `MutUntyped` -> `Mut` (#7113) # Objective `MutUntyped` is a struct that stores a `PtrMut` alongside change tick metadata. Working with this type is cumbersome, and has few benefits over storing the pointer and change ticks separately. Related: #6430 (title is out of date) ## Solution Add a convenience method for transforming an untyped change detection pointer into its typed counterpart. --- ## Changelog - Added the method `MutUntyped::with_type`. --- crates/bevy_ecs/src/change_detection.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 4b81fd455b..3802af55dd 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -635,6 +635,17 @@ impl<'a> MutUntyped<'a> { pub fn as_ref(&self) -> Ptr<'_> { self.value.as_ref() } + + /// Transforms this [`MutUntyped`] into a [`Mut`] with the same lifetime. + /// + /// # Safety + /// - `T` must be the erased pointee type for this [`MutUntyped`]. + pub unsafe fn with_type(self) -> Mut<'a, T> { + Mut { + value: self.value.deref_mut(), + ticks: self.ticks, + } + } } impl<'a> DetectChanges for MutUntyped<'a> {