Skip to content

Fix PartialReflect::apply for maps, remove get_at/_mut from Map trait #19802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions crates/bevy_reflect/src/impls/alloc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
error::ReflectCloneError,
generics::{Generics, TypeParamInfo},
kind::{ReflectKind, ReflectMut, ReflectOwned, ReflectRef},
map::{map_apply, map_partial_eq, map_try_apply, Map, MapInfo, MapIter},
map::{map_apply, map_partial_eq, map_try_apply, Map, MapInfo},
prelude::*,
reflect::{impl_full_reflect, ApplyError},
type_info::{MaybeTyped, TypeInfo, Typed},
Expand Down Expand Up @@ -31,27 +31,15 @@ where
.map(|value| value as &mut dyn PartialReflect)
}

fn get_at(&self, index: usize) -> Option<(&dyn PartialReflect, &dyn PartialReflect)> {
self.iter()
.nth(index)
.map(|(key, value)| (key as &dyn PartialReflect, value as &dyn PartialReflect))
}

fn get_at_mut(
&mut self,
index: usize,
) -> Option<(&dyn PartialReflect, &mut dyn PartialReflect)> {
self.iter_mut()
.nth(index)
.map(|(key, value)| (key as &dyn PartialReflect, value as &mut dyn PartialReflect))
}

fn len(&self) -> usize {
Self::len(self)
}

fn iter(&self) -> MapIter {
MapIter::new(self)
fn iter(&self) -> Box<dyn Iterator<Item = (&dyn PartialReflect, &dyn PartialReflect)> + '_> {
Box::new(
self.iter()
.map(|(k, v)| (k as &dyn PartialReflect, v as &dyn PartialReflect)),
)
}

fn drain(&mut self) -> Vec<(Box<dyn PartialReflect>, Box<dyn PartialReflect>)> {
Expand All @@ -68,6 +56,10 @@ where
result
}

fn retain(&mut self, f: &mut dyn FnMut(&dyn PartialReflect, &mut dyn PartialReflect) -> bool) {
self.retain(move |k, v| f(k, v));
}

fn insert_boxed(
&mut self,
key: Box<dyn PartialReflect>,
Expand Down
23 changes: 6 additions & 17 deletions crates/bevy_reflect/src/impls/macros/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,12 @@ macro_rules! impl_reflect_for_hashmap {
.map(|value| value as &mut dyn $crate::reflect::PartialReflect)
}

fn get_at(&self, index: usize) -> Option<(&dyn $crate::reflect::PartialReflect, &dyn $crate::reflect::PartialReflect)> {
self.iter()
.nth(index)
.map(|(key, value)| (key as &dyn $crate::reflect::PartialReflect, value as &dyn $crate::reflect::PartialReflect))
}

fn get_at_mut(
&mut self,
index: usize,
) -> Option<(&dyn $crate::reflect::PartialReflect, &mut dyn $crate::reflect::PartialReflect)> {
self.iter_mut().nth(index).map(|(key, value)| {
(key as &dyn $crate::reflect::PartialReflect, value as &mut dyn $crate::reflect::PartialReflect)
})
}

fn len(&self) -> usize {
Self::len(self)
}

fn iter(&self) -> $crate::map::MapIter {
$crate::map::MapIter::new(self)
fn iter(&self) -> bevy_platform::prelude::Box<dyn Iterator<Item = (&dyn $crate::reflect::PartialReflect, &dyn $crate::reflect::PartialReflect)> + '_> {
bevy_platform::prelude::Box::new(self.iter().map(|(k, v)| (k as &dyn $crate::reflect::PartialReflect, v as &dyn $crate::reflect::PartialReflect)))
}

fn drain(&mut self) -> bevy_platform::prelude::Vec<(bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>, bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>)> {
Expand All @@ -53,6 +38,10 @@ macro_rules! impl_reflect_for_hashmap {
.collect()
}

fn retain(&mut self, f: &mut dyn FnMut(&dyn $crate::reflect::PartialReflect, &mut dyn $crate::reflect::PartialReflect) -> bool) {
self.retain(move |key, value| f(key, value));
}

fn to_dynamic_map(&self) -> $crate::map::DynamicMap {
let mut dynamic_map = $crate::map::DynamicMap::default();
dynamic_map.set_represented_type($crate::reflect::PartialReflect::get_represented_type_info(self));
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/impls/macros/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ macro_rules! impl_reflect_for_hashset {
.collect()
}

fn retain(&mut self, f: &mut dyn FnMut(&dyn $crate::reflect::PartialReflect) -> bool) {
self.retain(move |value| f(value));
}

fn insert_boxed(&mut self, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) -> bool {
let value = V::take_from_reflect(value).unwrap_or_else(|value| {
panic!(
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,6 @@ mod tests {
foo.apply(&foo_patch);

let mut hash_map = <HashMap<_, _>>::default();
hash_map.insert(1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! How did this not lead to a failed test before? o.O

I mean it kind of makes sense that this code

        let mut map = DynamicMap::default();
        map.insert(2usize, 3i8);
        map.insert(3usize, 4i8);
        foo_patch.insert("d", map);

replaces the old map completely and hence doesn't include the (1,1) entry. The change here makes me wonder how it worked before though. The test makes it seem like previously the (1,1) entry would have been preserved.

I probably need to look a bit further into the changes..

hash_map.insert(2, 3);
hash_map.insert(3, 4);

Expand Down
Loading