props: move AsProperties into Property

This commit is contained in:
Carter Anderson 2020-05-25 18:20:36 -07:00
parent 83889c44e7
commit a837741c64
9 changed files with 52 additions and 180 deletions

View File

@ -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<T> where T: 'static {
pub struct Handle<T>
where
T: 'static,
{
pub id: HandleId,
#[prop(ignore)]
marker: PhantomData<T>,

View File

@ -51,7 +51,7 @@ pub fn derive_properties(input: TokenStream) -> TokenStream {
.collect::<Vec<(&Field, Option<PropAttributeArgs>, 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)
}

View File

@ -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)
}
}
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
use crate::{impl_property, AsProperties, Properties, Property};
use crate::{impl_property, Property};
use legion::prelude::Entity;
use std::any::Any;

View File

@ -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<T, I> Property for SmallVec<T>
where
T: Clone + Send + Sync + Serialize + 'static + Array<Item=I>,
I: Send + Sync + Clone + Serialize + 'static
T: Clone + Send + Sync + Serialize + 'static + Array<Item = I>,
I: Send + Sync + Clone + Serialize + 'static,
{
#[inline]
fn any(&self) -> &dyn Any {
@ -35,9 +35,3 @@ where
}
}
}
impl<T> AsProperties for SmallVec<T> where T: Array {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}

View File

@ -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<T> where T: Clone + Send + Sync + Serialize + 'static);
impl_property!(VecDeque<T> where T: Clone + Send + Sync + Serialize + 'static);
impl_property!(HashSet<T> where T: Clone + Eq + Send + Sync + Hash + Serialize + 'static);
impl_property!(HashMap<K, V> where
K: Clone + Eq + Send + Sync + Hash + Serialize + 'static,
V: Clone + Send + Sync + Serialize + 'static,);
impl_property!(BTreeMap<K, V> 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::<Self>());
}
}
}
impl AsProperties for f64 {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}
impl_property!(String);
impl_property!(bool);
impl_property!(Vec<T> where T: Clone + Send + Sync + Serialize + 'static);
impl_property!(VecDeque<T> where T: Clone + Send + Sync + Serialize + 'static);
impl_property!(HashSet<T> where T: Clone + Eq + Send + Sync + Hash + Serialize + 'static);
impl_property!(HashMap<K, V> where
K: Clone + Eq + Send + Sync + Hash + Serialize + 'static,
V: Clone + Send + Sync + Serialize + 'static,);
impl_property!(BTreeMap<K, V> where
K: Clone + Ord + Send + Sync + Serialize + 'static,
V: Clone + Send + Sync + Serialize + 'static);
impl<T> AsProperties for Vec<T> {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}
impl<T> AsProperties for VecDeque<T> {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}
impl<K> AsProperties for HashSet<K> {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}
impl<K, V> AsProperties for HashMap<K, V> {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}
impl<K, V> AsProperties for BTreeMap<K, V> {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}
}
}

View File

@ -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<dyn Property>;
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<T: 'static>(&self) -> Option<&T>;
fn set_val<T: 'static>(&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<dyn Property> {
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>() {
@ -77,13 +75,7 @@ macro_rules! impl_property {
panic!("prop value is not {}", std::any::type_name::<Self>());
}
}
}
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<dyn Property> {
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>() {
@ -118,7 +110,7 @@ macro_rules! impl_property {
panic!("prop value is not {}", std::any::type_name::<Self>());
}
}
}
}
}
};
(@as_item $i:item) => { $i };

View File

@ -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<PropertyTypeRegistryContext>) {
let mut test = Test {
a: 1,