Skip to content

Commit

Permalink
Add serialize feature to bevy_core
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 30, 2022
1 parent aa8c745 commit 1b7d521
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" }

# other
bytemuck = "1.5"
serde = { version = "1.0", features = ["derive"], optional = true }

[features]
serialize = ["dep:serde"]

[dev-dependencies]
crossbeam-channel = "0.5.0"
2 changes: 2 additions & 0 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions crates/bevy_core/src/serde.rs
Original file line number Diff line number Diff line change
@@ -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<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}

impl<'de> Deserialize<'de> for Name {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
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::<Name>())
}

fn visit_str<E: Error>(self, v: &str) -> Result<Self::Value, E> {
Ok(Name::new(v.to_string()))
}

fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
Ok(Name::new(v))
}
}
2 changes: 1 addition & 1 deletion crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
serialize = ["bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_core/serialize"]

# Display server protocol support (X11 is enabled by default)
wayland = ["bevy_winit/wayland"]
Expand Down

0 comments on commit 1b7d521

Please sign in to comment.