remove SerializableProperties wrapper struct

This commit is contained in:
Carter Anderson 2020-05-23 22:39:23 -07:00
parent 4c306e6d48
commit b7305046cf
2 changed files with 4 additions and 29 deletions

View file

@ -1,5 +1,4 @@
use crate::{DynamicProperties, Property, PropertyVal}; use crate::{DynamicProperties, Property, PropertyVal};
use serde::{ser::SerializeMap, Serialize};
pub trait Properties: Property { pub trait Properties: Property {
fn type_name(&self) -> &str; fn type_name(&self) -> &str;
@ -75,22 +74,4 @@ where
panic!("prop does not exist or is incorrect type: {}", name); panic!("prop does not exist or is incorrect type: {}", name);
} }
} }
}
pub struct SerializableProperties<'a> {
pub props: &'a dyn Properties,
}
impl<'a> Serialize for SerializableProperties<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_map(Some(self.props.prop_len()))?;
state.serialize_entry("type", self.props.type_name())?;
for (name, prop) in self.props.iter_props() {
state.serialize_entry(name, prop)?;
}
state.end()
}
} }

View file

@ -1,7 +1,4 @@
use bevy::{ use bevy::prelude::*;
prelude::*,
property::SerializableProperties,
};
fn main() { fn main() {
App::build() App::build()
@ -24,9 +21,7 @@ pub struct Nested {
fn setup() { fn setup() {
let mut test = Test { let mut test = Test {
a: 1, a: 1,
nested: Nested { nested: Nested { b: 8 },
b: 8,
}
}; };
// You can set a property value like this. The type must match exactly or this will fail. // You can set a property value like this. The type must match exactly or this will fail.
@ -47,11 +42,10 @@ fn setup() {
test.apply(&patch); test.apply(&patch);
assert_eq!(test.a, 4); assert_eq!(test.a, 4);
// Properties implement the serde Serialize trait. You don't need to derive it yourself! // Properties implement the serde Serialize trait. You don't need to derive it yourself!
let ser = SerializableProperties { props: &test };
let pretty_config = ron::ser::PrettyConfig::default().with_decimal_floats(true); let pretty_config = ron::ser::PrettyConfig::default().with_decimal_floats(true);
let ron_string = ron::ser::to_string_pretty(&ser, pretty_config.clone()).unwrap(); let ron_string = ron::ser::to_string_pretty(&test, pretty_config.clone()).unwrap();
println!("{}\n", ron_string); println!("{}\n", ron_string);
// Dynamic properties can be deserialized // Dynamic properties can be deserialized