diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 8eeac36461817..ce56cef43f219 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -394,6 +394,17 @@ impl Map for HashMap { self.insert(key, value) .map(|old_value| Box::new(old_value) as Box) } + + fn remove(&mut self, key: &dyn Reflect) -> Option> { + let mut from_reflect = None; + key.downcast_ref::() + .or_else(|| { + from_reflect = K::from_reflect(key); + from_reflect.as_ref() + }) + .and_then(|key| self.remove(key)) + .map(|value| Box::new(value) as Box) + } } impl Reflect for HashMap { diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index 9618b5cd2dd44..f5e4a8695f2a2 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -57,6 +57,12 @@ pub trait Map: Reflect { key: Box, value: Box, ) -> Option>; + + /// Removes an entry from the map. + /// + /// If the map did not have this key present, `None` is returned. + /// If the map did have this key present, the removed value is returned. + fn remove(&mut self, key: &dyn Reflect) -> Option>; } /// A container for compile-time map info. @@ -246,6 +252,14 @@ impl Map for DynamicMap { } } + fn remove(&mut self, key: &dyn Reflect) -> Option> { + let index = self + .indices + .remove(&key.reflect_hash().expect(HASH_ERROR))?; + let (_key, value) = self.values.remove(index); + Some(value) + } + fn drain(self: Box) -> Vec<(Box, Box)> { self.values }