From a837741c647b0fb015d337988842d7911c783f5f Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 25 May 2020 18:20:36 -0700 Subject: [PATCH] props: move AsProperties into Property --- crates/bevy_asset/src/handle.rs | 16 +-- .../bevy_property_derive/src/lib.rs | 5 +- .../bevy_property/src/dynamic_properties.rs | 6 +- .../bevy_property/src/impl_property_glam.rs | 2 +- .../bevy_property/src/impl_property_legion.rs | 2 +- .../src/impl_property_smallvec.rs | 16 +-- crates/bevy_property/src/impl_property_std.rs | 129 ++---------------- crates/bevy_property/src/property.rs | 48 +++---- examples/scene/properties.rs | 8 +- 9 files changed, 52 insertions(+), 180 deletions(-) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index 870554225a..f042ea92e5 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -3,10 +3,10 @@ use std::{ hash::{Hash, Hasher}, }; +use bevy_property::{Properties, Property}; +use serde::{Deserialize, Serialize}; use std::{any::TypeId, marker::PhantomData}; -use serde::{Serialize, Deserialize}; use uuid::Uuid; -use bevy_property::{Properties, Property, AsProperties}; #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct HandleId(pub Uuid); @@ -42,15 +42,11 @@ impl Property for HandleId { } } -impl AsProperties for HandleId { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } - -} - #[derive(Properties)] -pub struct Handle where T: 'static { +pub struct Handle +where + T: 'static, +{ pub id: HandleId, #[prop(ignore)] marker: PhantomData, diff --git a/crates/bevy_property/bevy_property_derive/src/lib.rs b/crates/bevy_property/bevy_property_derive/src/lib.rs index ceee52461b..2fe91032ce 100644 --- a/crates/bevy_property/bevy_property_derive/src/lib.rs +++ b/crates/bevy_property/bevy_property_derive/src/lib.rs @@ -51,7 +51,7 @@ pub fn derive_properties(input: TokenStream) -> TokenStream { .collect::, usize)>>(); let active_fields = fields_and_args .iter() - .filter(|(_field, attrs, i)| { + .filter(|(_field, attrs, _i)| { attrs.is_none() || match attrs.as_ref().unwrap().ignore { Some(ignore) => !ignore, @@ -173,9 +173,8 @@ pub fn derive_properties(input: TokenStream) -> TokenStream { panic!("attempted to apply non-Properties type to Properties type"); } } - } - impl #impl_generics #bevy_property_path::AsProperties for #struct_name#ty_generics { + #[inline] fn as_properties(&self) -> Option<&dyn #bevy_property_path::Properties> { Some(self) } diff --git a/crates/bevy_property/src/dynamic_properties.rs b/crates/bevy_property/src/dynamic_properties.rs index dac3a44ed2..6d7e940018 100644 --- a/crates/bevy_property/src/dynamic_properties.rs +++ b/crates/bevy_property/src/dynamic_properties.rs @@ -1,5 +1,5 @@ use crate::{ - AsProperties, Properties, Property, PropertyIter, PropertyTypeRegistration, + Properties, Property, PropertyIter, PropertyTypeRegistration, PropertyTypeRegistry, PropertyVal, }; use serde::{ @@ -365,10 +365,8 @@ impl Property for DynamicProperties { panic!("attempted to apply non-Properties type to Properties type"); } } -} -impl AsProperties for DynamicProperties { fn as_properties(&self) -> Option<&dyn Properties> { Some(self) } -} +} \ No newline at end of file diff --git a/crates/bevy_property/src/impl_property_glam.rs b/crates/bevy_property/src/impl_property_glam.rs index 1270bf9455..9d61c025e7 100644 --- a/crates/bevy_property/src/impl_property_glam.rs +++ b/crates/bevy_property/src/impl_property_glam.rs @@ -1,4 +1,4 @@ -use crate::{impl_property, AsProperties, Properties, Property}; +use crate::{impl_property, Property}; use glam::{Mat3, Mat4, Quat, Vec2, Vec3}; use std::any::Any; diff --git a/crates/bevy_property/src/impl_property_legion.rs b/crates/bevy_property/src/impl_property_legion.rs index 227e10528f..e3cc32165c 100644 --- a/crates/bevy_property/src/impl_property_legion.rs +++ b/crates/bevy_property/src/impl_property_legion.rs @@ -1,4 +1,4 @@ -use crate::{impl_property, AsProperties, Properties, Property}; +use crate::{impl_property, Property}; use legion::prelude::Entity; use std::any::Any; diff --git a/crates/bevy_property/src/impl_property_smallvec.rs b/crates/bevy_property/src/impl_property_smallvec.rs index d9752e28c4..9fb65bee0d 100644 --- a/crates/bevy_property/src/impl_property_smallvec.rs +++ b/crates/bevy_property/src/impl_property_smallvec.rs @@ -1,12 +1,12 @@ -use crate::{AsProperties, Properties, Property}; -use smallvec::{SmallVec, Array}; -use std::any::Any; +use crate::Property; use serde::Serialize; +use smallvec::{Array, SmallVec}; +use std::any::Any; impl Property for SmallVec where - T: Clone + Send + Sync + Serialize + 'static + Array, - I: Send + Sync + Clone + Serialize + 'static + T: Clone + Send + Sync + Serialize + 'static + Array, + I: Send + Sync + Clone + Serialize + 'static, { #[inline] fn any(&self) -> &dyn Any { @@ -35,9 +35,3 @@ where } } } - -impl AsProperties for SmallVec where T: Array { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} \ No newline at end of file diff --git a/crates/bevy_property/src/impl_property_std.rs b/crates/bevy_property/src/impl_property_std.rs index f4b6654fea..df6e45565d 100644 --- a/crates/bevy_property/src/impl_property_std.rs +++ b/crates/bevy_property/src/impl_property_std.rs @@ -1,4 +1,4 @@ -use crate::{AsProperties, Properties, Property, impl_property}; +use crate::{Property, impl_property}; use serde::Serialize; use std::{ any::Any, @@ -6,6 +6,18 @@ use std::{ hash::Hash, }; +impl_property!(String); +impl_property!(bool); +impl_property!(Vec where T: Clone + Send + Sync + Serialize + 'static); +impl_property!(VecDeque where T: Clone + Send + Sync + Serialize + 'static); +impl_property!(HashSet where T: Clone + Eq + Send + Sync + Hash + Serialize + 'static); +impl_property!(HashMap where + K: Clone + Eq + Send + Sync + Hash + Serialize + 'static, + V: Clone + Send + Sync + Serialize + 'static,); +impl_property!(BTreeMap where + K: Clone + Ord + Send + Sync + Serialize + 'static, + V: Clone + Send + Sync + Serialize + 'static); + impl Property for usize { #[inline] fn any(&self) -> &dyn Any { @@ -55,12 +67,6 @@ impl Property for usize { } } -impl AsProperties for usize { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for u64 { #[inline] fn any(&self) -> &dyn Any { @@ -110,12 +116,6 @@ impl Property for u64 { } } -impl AsProperties for u64 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for u32 { #[inline] fn any(&self) -> &dyn Any { @@ -165,12 +165,6 @@ impl Property for u32 { } } -impl AsProperties for u32 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for u16 { #[inline] fn any(&self) -> &dyn Any { @@ -220,12 +214,6 @@ impl Property for u16 { } } -impl AsProperties for u16 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for u8 { #[inline] fn any(&self) -> &dyn Any { @@ -275,12 +263,6 @@ impl Property for u8 { } } -impl AsProperties for u8 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for isize { #[inline] fn any(&self) -> &dyn Any { @@ -330,12 +312,6 @@ impl Property for isize { } } -impl AsProperties for isize { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for i64 { #[inline] fn any(&self) -> &dyn Any { @@ -385,12 +361,6 @@ impl Property for i64 { } } -impl AsProperties for i64 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for i32 { #[inline] fn any(&self) -> &dyn Any { @@ -440,11 +410,6 @@ impl Property for i32 { } } -impl AsProperties for i32 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} impl Property for i16 { #[inline] @@ -495,12 +460,6 @@ impl Property for i16 { } } -impl AsProperties for i16 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for i8 { #[inline] fn any(&self) -> &dyn Any { @@ -550,12 +509,6 @@ impl Property for i8 { } } -impl AsProperties for i8 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for f32 { #[inline] fn any(&self) -> &dyn Any { @@ -589,12 +542,6 @@ impl Property for f32 { } } -impl AsProperties for f32 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - impl Property for f64 { #[inline] fn any(&self) -> &dyn Any { @@ -626,52 +573,4 @@ impl Property for f64 { panic!("prop value is not {}", std::any::type_name::()); } } -} - -impl AsProperties for f64 { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - -impl_property!(String); -impl_property!(bool); -impl_property!(Vec where T: Clone + Send + Sync + Serialize + 'static); -impl_property!(VecDeque where T: Clone + Send + Sync + Serialize + 'static); -impl_property!(HashSet where T: Clone + Eq + Send + Sync + Hash + Serialize + 'static); -impl_property!(HashMap where - K: Clone + Eq + Send + Sync + Hash + Serialize + 'static, - V: Clone + Send + Sync + Serialize + 'static,); -impl_property!(BTreeMap where - K: Clone + Ord + Send + Sync + Serialize + 'static, - V: Clone + Send + Sync + Serialize + 'static); - -impl AsProperties for Vec { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - -impl AsProperties for VecDeque { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - -impl AsProperties for HashSet { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - -impl AsProperties for HashMap { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - -impl AsProperties for BTreeMap { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} +} \ No newline at end of file diff --git a/crates/bevy_property/src/property.rs b/crates/bevy_property/src/property.rs index 31e8764ecd..9f691a4b99 100644 --- a/crates/bevy_property/src/property.rs +++ b/crates/bevy_property/src/property.rs @@ -1,25 +1,19 @@ use crate::Properties; -use serde::Serialize; -use std::{ - any::Any, - collections::{BTreeMap, HashMap, HashSet, VecDeque}, - hash::Hash, -}; +use std::any::Any; -pub trait Property: erased_serde::Serialize + Send + Sync + Any + AsProperties + 'static { +pub trait Property: erased_serde::Serialize + Send + Sync + Any + 'static { fn any(&self) -> &dyn Any; fn any_mut(&mut self) -> &mut dyn Any; fn clone_prop(&self) -> Box; fn set(&mut self, value: &dyn Property); fn apply(&mut self, value: &dyn Property); + fn as_properties(&self) -> Option<&dyn Properties> { + None + } } erased_serde::serialize_trait_object!(Property); -pub trait AsProperties { - fn as_properties(&self) -> Option<&dyn Properties>; -} - pub trait PropertyVal { fn val(&self) -> Option<&T>; fn set_val(&mut self, value: T); @@ -43,7 +37,11 @@ impl PropertyVal for dyn Property { // used by impl_property #[allow(unused_macros)] -macro_rules! as_item { ($i:item) => {$i} } +macro_rules! as_item { + ($i:item) => { + $i + }; +} #[macro_export] macro_rules! impl_property { @@ -53,22 +51,22 @@ macro_rules! impl_property { 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::() { @@ -77,13 +75,7 @@ macro_rules! impl_property { panic!("prop value is not {}", std::any::type_name::()); } } - } - - impl AsProperties for $ty { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } - } + } }; (@$trait_:ident [$($args:ident,)*] where [$($preds:tt)+]) => { impl_property! { @@ -94,22 +86,22 @@ macro_rules! impl_property { 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::() { @@ -118,7 +110,7 @@ macro_rules! impl_property { panic!("prop value is not {}", std::any::type_name::()); } } - } + } } }; (@as_item $i:item) => { $i }; diff --git a/examples/scene/properties.rs b/examples/scene/properties.rs index 1d21835fc6..839426b61e 100644 --- a/examples/scene/properties.rs +++ b/examples/scene/properties.rs @@ -1,7 +1,7 @@ use bevy::{ component_registry::PropertyTypeRegistryContext, prelude::*, - property::{ron::deserialize_dynamic_properties, AsProperties}, + property::{ron::deserialize_dynamic_properties}, }; use serde::{Deserialize, Serialize}; @@ -54,12 +54,6 @@ impl Property for CustomProperty { } } -impl AsProperties for CustomProperty { - fn as_properties(&self) -> Option<&dyn Properties> { - None - } -} - fn setup(property_type_registry: Res) { let mut test = Test { a: 1,