From 5a8e6e4feb736ec6532060b8fe2d4b3a5797df5f Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Mon, 31 Oct 2022 21:20:57 +0000 Subject: [PATCH] Add `serialize` feature to `bevy_core` (#6423) # Objective `bevy_core` is missing a feature corresponding to the `serialize` feature on the `bevy` crate. Similar to #6378 and https://github.com/bevyengine/bevy/pull/6379 to serialize `Name` easily. ## Solution Add this feature and hand-written serialization for `Name` (to avoid storing `hash` field). --- ## Changelog ### Added * `Serialize` and `Deserialize` derives for `Name` under `serialize` feature. --- crates/bevy_core/Cargo.toml | 4 ++++ crates/bevy_core/src/lib.rs | 2 ++ crates/bevy_core/src/serde.rs | 41 +++++++++++++++++++++++++++++++++ crates/bevy_internal/Cargo.toml | 2 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 crates/bevy_core/src/serde.rs diff --git a/crates/bevy_core/Cargo.toml b/crates/bevy_core/Cargo.toml index 5c26103cb7a828..90745ff093a3c4 100644 --- a/crates/bevy_core/Cargo.toml +++ b/crates/bevy_core/Cargo.toml @@ -20,6 +20,10 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } # other bytemuck = "1.5" +serde = { version = "1.0", optional = true } + +[features] +serialize = ["dep:serde"] [dev-dependencies] crossbeam-channel = "0.5.0" diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index 6badaabed691bc..fb22cbc2ab7323 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -2,6 +2,8 @@ //! This crate provides core functionality for Bevy Engine. mod name; +#[cfg(feature = "serialize")] +mod serde; mod task_pool_options; use bevy_ecs::system::Resource; diff --git a/crates/bevy_core/src/serde.rs b/crates/bevy_core/src/serde.rs new file mode 100644 index 00000000000000..f8835e9a5f8d2d --- /dev/null +++ b/crates/bevy_core/src/serde.rs @@ -0,0 +1,41 @@ +use std::{ + any, + fmt::{self, Formatter}, +}; + +use serde::{ + de::{Error, Visitor}, + Deserialize, Deserializer, Serialize, Serializer, +}; + +use super::name::Name; + +impl Serialize for Name { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(self.as_str()) + } +} + +impl<'de> Deserialize<'de> for Name { + fn deserialize>(deserializer: D) -> Result { + deserializer.deserialize_str(EntityVisitor) + } +} + +struct EntityVisitor; + +impl<'de> Visitor<'de> for EntityVisitor { + type Value = Name; + + fn expecting(&self, formatter: &mut Formatter) -> fmt::Result { + formatter.write_str(any::type_name::()) + } + + fn visit_str(self, v: &str) -> Result { + Ok(Name::new(v.to_string())) + } + + fn visit_string(self, v: String) -> Result { + Ok(Name::new(v)) + } +} diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 472a9f46aac425..735113ec2e8a80 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"] # Enable watching file system for asset hot reload filesystem_watcher = ["bevy_asset/filesystem_watcher"] -serialize = ["bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_transform/serialize", "bevy_math/serialize"] +serialize = ["bevy_core/serialize", "bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_transform/serialize", "bevy_math/serialize"] # Display server protocol support (X11 is enabled by default) wayland = ["bevy_winit/wayland"]