Sorts the scene entries by path before serializing. (#15047)
# Objective Fixes: https://github.com/bevyengine/bevy/issues/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.
This commit is contained in:
parent
5eca832cee
commit
fab0e5d085
@ -154,6 +154,8 @@ impl<'a> Serialize for EntitySerializer<'a> {
|
|||||||
/// Used to serialize scene resources in [`SceneSerializer`] and entity components in [`EntitySerializer`].
|
/// Used to serialize scene resources in [`SceneSerializer`] and entity components in [`EntitySerializer`].
|
||||||
/// Note that having several entries of the same type in `entries` will lead to an error when using the RON format and
|
/// Note that having several entries of the same type in `entries` will lead to an error when using the RON format and
|
||||||
/// deserializing through [`SceneMapDeserializer`].
|
/// deserializing through [`SceneMapDeserializer`].
|
||||||
|
///
|
||||||
|
/// Note: The entries are sorted by type path before they're serialized.
|
||||||
pub struct SceneMapSerializer<'a> {
|
pub struct SceneMapSerializer<'a> {
|
||||||
/// List of boxed values of unique type to serialize.
|
/// List of boxed values of unique type to serialize.
|
||||||
pub entries: &'a [Box<dyn PartialReflect>],
|
pub entries: &'a [Box<dyn PartialReflect>],
|
||||||
@ -167,10 +169,25 @@ impl<'a> Serialize for SceneMapSerializer<'a> {
|
|||||||
S: Serializer,
|
S: Serializer,
|
||||||
{
|
{
|
||||||
let mut state = serializer.serialize_map(Some(self.entries.len()))?;
|
let mut state = serializer.serialize_map(Some(self.entries.len()))?;
|
||||||
for reflect in self.entries {
|
let sorted_entries = {
|
||||||
|
let mut entries = self
|
||||||
|
.entries
|
||||||
|
.iter()
|
||||||
|
.map(|entry| {
|
||||||
|
(
|
||||||
|
entry.get_represented_type_info().unwrap().type_path(),
|
||||||
|
entry.as_partial_reflect(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
entries.sort_by_key(|(type_path, _partial_reflect)| *type_path);
|
||||||
|
entries
|
||||||
|
};
|
||||||
|
|
||||||
|
for (type_path, partial_reflect) in sorted_entries {
|
||||||
state.serialize_entry(
|
state.serialize_entry(
|
||||||
reflect.get_represented_type_info().unwrap().type_path(),
|
type_path,
|
||||||
&TypedReflectSerializer::new(reflect.as_partial_reflect(), self.registry),
|
&TypedReflectSerializer::new(partial_reflect, self.registry),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
state.end()
|
state.end()
|
||||||
@ -598,15 +615,15 @@ mod tests {
|
|||||||
),
|
),
|
||||||
4294967297: (
|
4294967297: (
|
||||||
components: {
|
components: {
|
||||||
"bevy_scene::serde::tests::Foo": (123),
|
|
||||||
"bevy_scene::serde::tests::Bar": (345),
|
"bevy_scene::serde::tests::Bar": (345),
|
||||||
|
"bevy_scene::serde::tests::Foo": (123),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
4294967298: (
|
4294967298: (
|
||||||
components: {
|
components: {
|
||||||
"bevy_scene::serde::tests::Foo": (123),
|
|
||||||
"bevy_scene::serde::tests::Bar": (345),
|
"bevy_scene::serde::tests::Bar": (345),
|
||||||
"bevy_scene::serde::tests::Baz": (789),
|
"bevy_scene::serde::tests::Baz": (789),
|
||||||
|
"bevy_scene::serde::tests::Foo": (123),
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user