Skip to content

Commit fab0e5d

Browse files
authored
Sorts the scene entries by path before serializing. (#15047)
# Objective Fixes: #14515 ## Solution Sorts the iterator with itertools' sorted_by function. This is required given that 'self.entries' is an immutable &[Box<dyn PartialReflect] which also doesn't implement Clone or Copy. ## Testing The modifications passed the unit testing only after they were edited to ensure that the items were in alphabetical order. I haven't checked for performance implications.
1 parent 5eca832 commit fab0e5d

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

crates/bevy_scene/src/serde.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ impl<'a> Serialize for EntitySerializer<'a> {
154154
/// Used to serialize scene resources in [`SceneSerializer`] and entity components in [`EntitySerializer`].
155155
/// Note that having several entries of the same type in `entries` will lead to an error when using the RON format and
156156
/// deserializing through [`SceneMapDeserializer`].
157+
///
158+
/// Note: The entries are sorted by type path before they're serialized.
157159
pub struct SceneMapSerializer<'a> {
158160
/// List of boxed values of unique type to serialize.
159161
pub entries: &'a [Box<dyn PartialReflect>],
@@ -167,10 +169,25 @@ impl<'a> Serialize for SceneMapSerializer<'a> {
167169
S: Serializer,
168170
{
169171
let mut state = serializer.serialize_map(Some(self.entries.len()))?;
170-
for reflect in self.entries {
172+
let sorted_entries = {
173+
let mut entries = self
174+
.entries
175+
.iter()
176+
.map(|entry| {
177+
(
178+
entry.get_represented_type_info().unwrap().type_path(),
179+
entry.as_partial_reflect(),
180+
)
181+
})
182+
.collect::<Vec<_>>();
183+
entries.sort_by_key(|(type_path, _partial_reflect)| *type_path);
184+
entries
185+
};
186+
187+
for (type_path, partial_reflect) in sorted_entries {
171188
state.serialize_entry(
172-
reflect.get_represented_type_info().unwrap().type_path(),
173-
&TypedReflectSerializer::new(reflect.as_partial_reflect(), self.registry),
189+
type_path,
190+
&TypedReflectSerializer::new(partial_reflect, self.registry),
174191
)?;
175192
}
176193
state.end()
@@ -598,15 +615,15 @@ mod tests {
598615
),
599616
4294967297: (
600617
components: {
601-
"bevy_scene::serde::tests::Foo": (123),
602618
"bevy_scene::serde::tests::Bar": (345),
619+
"bevy_scene::serde::tests::Foo": (123),
603620
},
604621
),
605622
4294967298: (
606623
components: {
607-
"bevy_scene::serde::tests::Foo": (123),
608624
"bevy_scene::serde::tests::Bar": (345),
609625
"bevy_scene::serde::tests::Baz": (789),
626+
"bevy_scene::serde::tests::Foo": (123),
610627
},
611628
),
612629
},

0 commit comments

Comments
 (0)