Fix reflected serialization/deserialization on Name component (#11447)

# Objective

- This PR makes it so that `ReflectSerialize` and `ReflectDeserialize`
traits are properly derived on `Name`. This avoids having the internal
hash “leak” into the serialization when using reflection.

## Solution

- Added a conditional derive for `ReflectDeserialize` and
`ReflectSerialize` via `#[cfg_attr()]`

---

## Changelog

- `Name` now implements `ReflectDeserialize` and `ReflectSerialize`
whenever the `serialize` feature is enabled.
This commit is contained in:
Marco Buono 2024-01-21 15:04:13 -03:00 committed by GitHub
parent ffb6faafc2
commit 18833fa67c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -10,6 +10,9 @@ use std::{
ops::Deref,
};
#[cfg(feature = "serialize")]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// Component used to identify an entity. Stores a hash for faster comparisons.
///
/// The hash is eagerly re-computed upon each update to the name.
@ -19,8 +22,9 @@ use std::{
/// used instead as the default unique identifier.
#[derive(Reflect, Component, Clone)]
#[reflect(Component, Default, Debug)]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
pub struct Name {
hash: u64, // TODO: Shouldn't be serialized
hash: u64, // Won't be serialized (see: `bevy_core::serde` module)
name: Cow<'static, str>,
}