Skip to content

Commit 2fa434a

Browse files
Jondolfmrchantey
authored andcommitted
Implement Serialize/Deserialize for entity collections (bevyengine#17620)
# Objective Follow-up to bevyengine#17615. Bevy's entity collection types like `EntityHashSet` no longer implement serde's `Serialize` and `Deserialize` after becoming newtypes instead of type aliases in bevyengine#16912. This broke some types that support serde for me in Avian. I also missed creating const constructors for `EntityIndexMap` and `EntityIndexSet` in bevyengine#17615. Oops! ## Solution Implement `Serialize` and `Deserialize` for Bevy's entity collection types, and add const constructors for `EntityIndexMap` and `EntityIndexSet`. I didn't implement `ReflectSerialize` or `ReflectDeserialize` here, because I had some trouble fixing the resulting errors, and they were not implemented previously either.
1 parent 3b3dd9c commit 2fa434a

File tree

5 files changed

+12
-3
lines changed

5 files changed

+12
-3
lines changed

crates/bevy_ecs/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ default = ["std", "bevy_reflect", "async_executor"]
2020
multi_threaded = ["bevy_tasks/multi_threaded", "dep:arrayvec"]
2121

2222
## Adds serialization support through `serde`.
23-
serialize = ["dep:serde", "bevy_utils/serde", "bevy_platform_support/serialize"]
23+
serialize = [
24+
"dep:serde",
25+
"bevy_utils/serde",
26+
"bevy_platform_support/serialize",
27+
"indexmap/serde",
28+
]
2429

2530
## Adds runtime reflection support using `bevy_reflect`.
2631
bevy_reflect = ["dep:bevy_reflect"]

crates/bevy_ecs/src/entity/hash_map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use super::{Entity, EntityHash, EntitySetIterator, TrustedEntityBorrow};
1717

1818
/// A [`HashMap`] pre-configured to use [`EntityHash`] hashing.
1919
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
20+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
2021
#[derive(Debug, Clone, PartialEq, Eq)]
2122
pub struct EntityHashMap<V>(pub(crate) HashMap<Entity, V, EntityHash>);
2223

crates/bevy_ecs/src/entity/hash_set.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use super::{Entity, EntityHash, EntitySet, EntitySetIterator, FromEntitySetItera
2020

2121
/// A [`HashSet`] pre-configured to use [`EntityHash`] hashing.
2222
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
23+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
2324
#[derive(Debug, Clone, Default, PartialEq, Eq)]
2425
pub struct EntityHashSet(pub(crate) HashSet<Entity, EntityHash>);
2526

crates/bevy_ecs/src/entity/index_map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::{Entity, EntityHash, EntitySetIterator, TrustedEntityBorrow};
1414

1515
/// A [`IndexMap`] pre-configured to use [`EntityHash`] hashing.
1616
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
17+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
1718
#[derive(Debug, Clone)]
1819
pub struct EntityIndexMap<V>(pub(crate) IndexMap<Entity, V, EntityHash>);
1920

@@ -23,7 +24,7 @@ impl<V> EntityIndexMap<V> {
2324
/// Equivalent to [`IndexMap::with_hasher(EntityHash)`].
2425
///
2526
/// [`IndexMap::with_hasher(EntityHash)`]: IndexMap::with_hasher
26-
pub fn new() -> Self {
27+
pub const fn new() -> Self {
2728
Self(IndexMap::with_hasher(EntityHash))
2829
}
2930

crates/bevy_ecs/src/entity/index_set.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use indexmap::set::{self, IndexSet};
1111
use super::{Entity, EntityHash, EntitySetIterator};
1212

1313
/// An [`IndexSet`] pre-configured to use [`EntityHash`] hashing.
14+
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
1415
#[derive(Debug, Clone, Default)]
1516
pub struct EntityIndexSet(pub(crate) IndexSet<Entity, EntityHash>);
1617

@@ -20,7 +21,7 @@ impl EntityIndexSet {
2021
/// Equivalent to [`IndexSet::with_hasher(EntityHash)`].
2122
///
2223
/// [`IndexSet::with_hasher(EntityHash)`]: IndexSet::with_hasher
23-
pub fn new() -> Self {
24+
pub const fn new() -> Self {
2425
Self(IndexSet::with_hasher(EntityHash))
2526
}
2627

0 commit comments

Comments
 (0)