From a9f766ceb2c1d81147d761a93b01ce4a23a5a488 Mon Sep 17 00:00:00 2001 From: Noah Date: Fri, 21 Apr 2023 22:55:53 -0400 Subject: [PATCH] Fix Box struct with a hashmap in it panicking when clone_value is called on it (#8184) # Objective - Fix the issue described in #8183: Box structs with a hashmap in them will panic when clone_value is called on it - Fixes: #8183 ## Solution - Updates the implementation of Reflect for Hashmaps to make clone_value call from_reflect on the key before inserting it into the new struct --- crates/bevy_reflect/src/impls/std.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 0ccebbd10a..0b42a70793 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -399,7 +399,10 @@ macro_rules! impl_reflect_for_hashmap { let mut dynamic_map = DynamicMap::default(); dynamic_map.set_name(self.type_name().to_string()); for (k, v) in self { - dynamic_map.insert_boxed(k.clone_value(), v.clone_value()); + let key = K::from_reflect(k).unwrap_or_else(|| { + panic!("Attempted to clone invalid key of type {}.", k.type_name()) + }); + dynamic_map.insert_boxed(Box::new(key), v.clone_value()); } dynamic_map }