diff --git a/crates/bevy_property/src/impl_property_std.rs b/crates/bevy_property/src/impl_property_std.rs index 61add8d478..1bde946c13 100644 --- a/crates/bevy_property/src/impl_property_std.rs +++ b/crates/bevy_property/src/impl_property_std.rs @@ -1,5 +1,9 @@ -use crate::{impl_property, Properties, Property, PropertyIter, property_serde::{Serializable, SeqSerializer}, PropertyType}; -use serde::{Serialize, Deserialize}; +use crate::{ + impl_property, + property_serde::{SeqSerializer, Serializable}, + Properties, Property, PropertyIter, PropertyType, +}; +use serde::{Deserialize, Serialize}; use std::{ any::Any, collections::{BTreeMap, HashMap, HashSet}, @@ -47,7 +51,7 @@ where fn any_mut(&mut self) -> &mut dyn Any { self } - + fn clone_prop(&self) -> Box { Box::new(self.clone()) } @@ -84,8 +88,6 @@ where } } -impl_property!(String); -impl_property!(bool); // impl_property!(SEQUENCE, VecDeque where T: Clone + Send + Sync + Serialize + 'static); impl_property!(HashSet where T: Clone + Eq + Send + Sync + Hash + Serialize + for<'de> Deserialize<'de> + 'static); impl_property!(HashMap where @@ -96,8 +98,86 @@ impl_property!(BTreeMap where V: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static); impl_property!(Range where T: Clone + Send + Sync + Serialize + for<'de> Deserialize<'de> + 'static); - // TODO: Implement lossless primitive types in RON and remove all of these primitive "cast checks" +impl Property for String { + #[inline] + fn type_name(&self) -> &str { + std::any::type_name::() + } + + #[inline] + fn any(&self) -> &dyn Any { + self + } + + #[inline] + fn any_mut(&mut self) -> &mut dyn Any { + self + } + + #[inline] + fn clone_prop(&self) -> Box { + Box::new(self.clone()) + } + + #[inline] + fn apply(&mut self, value: &dyn Property) { + self.set(value); + } + + fn set(&mut self, value: &dyn Property) { + let value = value.any(); + if let Some(prop) = value.downcast_ref::() { + *self = prop.clone(); + } else { + panic!("prop value is not {}", std::any::type_name::()); + } + } + + fn serializable(&self) -> Serializable { + Serializable::Borrowed(self) + } +} + +impl Property for bool { + #[inline] + fn type_name(&self) -> &str { + std::any::type_name::() + } + + #[inline] + fn any(&self) -> &dyn Any { + self + } + + #[inline] + fn any_mut(&mut self) -> &mut dyn Any { + self + } + + #[inline] + fn clone_prop(&self) -> Box { + Box::new(self.clone()) + } + + #[inline] + fn apply(&mut self, value: &dyn Property) { + self.set(value); + } + + fn set(&mut self, value: &dyn Property) { + let value = value.any(); + if let Some(prop) = value.downcast_ref::() { + *self = *prop; + } else { + panic!("prop value is not {}", std::any::type_name::()); + } + } + + fn serializable(&self) -> Serializable { + Serializable::Borrowed(self) + } +} impl Property for usize { #[inline] diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index fa367b168f..eeb0008c3f 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -48,6 +48,7 @@ use mesh::mesh_resource_provider_system; use render_graph::RenderGraph; use render_resource::EntitiesWaitingForAssets; use texture::{PngTextureLoader, TextureResourceSystemState}; +use std::ops::Range; pub static RENDER_RESOURCE_STAGE: &str = "render_resource"; pub static RENDER_STAGE: &str = "render"; @@ -79,6 +80,8 @@ impl AppPlugin for RenderPlugin { .register_component::() .register_component::() .register_property_type::() + .register_property_type::() + .register_property_type::>() .init_resource::() .init_resource::() .init_resource::()