Skip to content

Commit

Permalink
Implement Serialize/Deserialize for entity collections (#17620)
Browse files Browse the repository at this point in the history
# Objective

Follow-up to #17615.

Bevy's entity collection types like `EntityHashSet` no longer implement
serde's `Serialize` and `Deserialize` after becoming newtypes instead of
type aliases in #16912. This broke some types that support serde for me
in Avian.

I also missed creating const constructors for `EntityIndexMap` and
`EntityIndexSet` in #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.
  • Loading branch information
Jondolf authored Feb 2, 2025
1 parent 9c5ce33 commit 9165fb0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
7 changes: 6 additions & 1 deletion crates/bevy_ecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ default = ["std", "bevy_reflect", "async_executor"]
multi_threaded = ["bevy_tasks/multi_threaded", "dep:arrayvec"]

## Adds serialization support through `serde`.
serialize = ["dep:serde", "bevy_utils/serde", "bevy_platform_support/serialize"]
serialize = [
"dep:serde",
"bevy_utils/serde",
"bevy_platform_support/serialize",
"indexmap/serde",
]

## Adds runtime reflection support using `bevy_reflect`.
bevy_reflect = ["dep:bevy_reflect"]
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/entity/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{Entity, EntityHash, EntitySetIterator, TrustedEntityBorrow};

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

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/entity/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::{Entity, EntityHash, EntitySet, EntitySetIterator, FromEntitySetItera

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

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/entity/index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{Entity, EntityHash, EntitySetIterator, TrustedEntityBorrow};

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

Expand All @@ -23,7 +24,7 @@ impl<V> EntityIndexMap<V> {
/// Equivalent to [`IndexMap::with_hasher(EntityHash)`].
///
/// [`IndexMap::with_hasher(EntityHash)`]: IndexMap::with_hasher
pub fn new() -> Self {
pub const fn new() -> Self {
Self(IndexMap::with_hasher(EntityHash))
}

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ecs/src/entity/index_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use indexmap::set::{self, IndexSet};
use super::{Entity, EntityHash, EntitySetIterator};

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

Expand All @@ -20,7 +21,7 @@ impl EntityIndexSet {
/// Equivalent to [`IndexSet::with_hasher(EntityHash)`].
///
/// [`IndexSet::with_hasher(EntityHash)`]: IndexSet::with_hasher
pub fn new() -> Self {
pub const fn new() -> Self {
Self(IndexSet::with_hasher(EntityHash))
}

Expand Down

0 comments on commit 9165fb0

Please sign in to comment.