From f0a98645d0dcc3dc76ad335755adb074f6fbe5db Mon Sep 17 00:00:00 2001 From: Yasha Borevich Date: Sun, 10 Mar 2024 13:46:50 +0100 Subject: [PATCH] Implement MutUntyped::from(mut_typed) (#12406) # Objective Allow to create MutUntyped<'a> instance from Mut<'a, T>. Fixes #12405 ## Solution Added impl<'a, T> From> for MutUntyped<'> --- crates/bevy_ecs/src/change_detection.rs | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 7fda0a13f9..bca6aa2a38 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -938,6 +938,15 @@ impl std::fmt::Debug for MutUntyped<'_> { } } +impl<'w, T> From> for MutUntyped<'w> { + fn from(value: Mut<'w, T>) -> Self { + MutUntyped { + value: value.value.into(), + ticks: value.ticks, + } + } +} + #[cfg(test)] mod tests { use bevy_ecs_macros::Resource; @@ -1254,4 +1263,29 @@ mod tests { assert!(new.is_changed()); } + + #[test] + fn mut_untyped_from_mut() { + let mut component_ticks = ComponentTicks { + added: Tick::new(1), + changed: Tick::new(2), + }; + let ticks = TicksMut { + added: &mut component_ticks.added, + changed: &mut component_ticks.changed, + last_run: Tick::new(3), + this_run: Tick::new(4), + }; + let mut c = C {}; + let mut_typed = Mut { + value: &mut c, + ticks, + }; + + let into_mut: MutUntyped = mut_typed.into(); + assert_eq!(1, into_mut.ticks.added.get()); + assert_eq!(2, into_mut.ticks.changed.get()); + assert_eq!(3, into_mut.ticks.last_run.get()); + assert_eq!(4, into_mut.ticks.this_run.get()); + } }