From 663f95e0fdd98a685526c3827bdae1d1743fff14 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Mon, 25 May 2020 15:35:47 -0700 Subject: [PATCH] impl Property for std collections --- crates/bevy_property/src/property.rs | 275 ++++++++++++++++++++++++--- 1 file changed, 247 insertions(+), 28 deletions(-) diff --git a/crates/bevy_property/src/property.rs b/crates/bevy_property/src/property.rs index 87a654af3e..4ffc0081aa 100644 --- a/crates/bevy_property/src/property.rs +++ b/crates/bevy_property/src/property.rs @@ -1,5 +1,10 @@ use crate::Properties; -use std::any::Any; +use serde::Serialize; +use std::{ + any::Any, + collections::{BTreeMap, HashMap, HashSet, VecDeque}, + hash::Hash, +}; pub trait Property: erased_serde::Serialize + Send + Sync + Any + AsProperties + 'static { fn any(&self) -> &dyn Any; @@ -85,8 +90,7 @@ impl Property for usize { } } -impl AsProperties for usize -{ +impl AsProperties for usize { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -141,8 +145,7 @@ impl Property for u64 { } } -impl AsProperties for u64 -{ +impl AsProperties for u64 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -197,8 +200,7 @@ impl Property for u32 { } } -impl AsProperties for u32 -{ +impl AsProperties for u32 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -253,8 +255,7 @@ impl Property for u16 { } } -impl AsProperties for u16 -{ +impl AsProperties for u16 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -309,8 +310,7 @@ impl Property for u8 { } } -impl AsProperties for u8 -{ +impl AsProperties for u8 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -365,8 +365,7 @@ impl Property for isize { } } -impl AsProperties for isize -{ +impl AsProperties for isize { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -421,8 +420,7 @@ impl Property for i64 { } } -impl AsProperties for i64 -{ +impl AsProperties for i64 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -477,8 +475,7 @@ impl Property for i32 { } } -impl AsProperties for i32 -{ +impl AsProperties for i32 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -533,8 +530,7 @@ impl Property for i16 { } } -impl AsProperties for i16 -{ +impl AsProperties for i16 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -589,8 +585,7 @@ impl Property for i8 { } } -impl AsProperties for i8 -{ +impl AsProperties for i8 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -629,8 +624,7 @@ impl Property for f32 { } } -impl AsProperties for f32 -{ +impl AsProperties for f32 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -669,8 +663,7 @@ impl Property for f64 { } } -impl AsProperties for f64 -{ +impl AsProperties for f64 { fn as_properties(&self) -> Option<&dyn Properties> { None } @@ -705,9 +698,235 @@ impl Property for String { } } -impl AsProperties for String -{ +impl AsProperties for String { fn as_properties(&self) -> Option<&dyn Properties> { None } -} \ No newline at end of file +} + +impl Property for bool { + #[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(); + } + } +} + +impl AsProperties for bool { + fn as_properties(&self) -> Option<&dyn Properties> { + None + } +} + +impl Property for Vec +where + T: Clone + Send + Sync + Serialize + 'static, +{ + #[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(); + } + } +} + +impl AsProperties for Vec { + fn as_properties(&self) -> Option<&dyn Properties> { + None + } +} + +impl Property for VecDeque +where + T: Clone + Send + Sync + Serialize + 'static, +{ + #[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(); + } + } +} + +impl AsProperties for VecDeque { + fn as_properties(&self) -> Option<&dyn Properties> { + None + } +} + +impl Property for HashSet +where + K: Clone + Eq + Send + Sync + Hash + Serialize + 'static, +{ + #[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(); + } + } +} + +impl AsProperties for HashSet { + fn as_properties(&self) -> Option<&dyn Properties> { + None + } +} + +impl Property for HashMap +where + K: Clone + Eq + Send + Sync + Hash + Serialize + 'static, + V: Clone + Send + Sync + Serialize + 'static, +{ + #[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(); + } + } +} + +impl AsProperties for HashMap { + fn as_properties(&self) -> Option<&dyn Properties> { + None + } +} + +impl Property for BTreeMap +where + K: Clone + Ord + Send + Sync + Serialize + 'static, + V: Clone + Send + Sync + Serialize + 'static, +{ + #[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(); + } + } +} + +impl AsProperties for BTreeMap { + fn as_properties(&self) -> Option<&dyn Properties> { + None + } +}