props: rename prop/props to property/properties

its longer but a bit clearer
This commit is contained in:
Carter Anderson 2020-05-22 15:36:48 -07:00
parent e514bd14fe
commit 159acf52af
14 changed files with 125 additions and 116 deletions

View file

@ -6,7 +6,7 @@ edition = "2018"
[features]
default = ["headless", "wgpu", "winit"]
headless = ["asset", "core", "derive", "diagnostic", "gltf", "input", "pbr", "props", "render", "scene", "text", "transform", "ui", "window"]
headless = ["asset", "core", "derive", "diagnostic", "gltf", "input", "pbr", "property", "render", "scene", "text", "transform", "ui", "window"]
asset = ["bevy_asset"]
core = ["bevy_core"]
derive = ["bevy_derive"]
@ -14,7 +14,7 @@ diagnostic = ["bevy_diagnostic"]
gltf = ["bevy_gltf"]
input = ["bevy_input"]
pbr = ["bevy_pbr"]
props = ["bevy_props"]
property = ["bevy_property"]
render = ["bevy_render"]
scene = ["bevy_scene"]
text = ["bevy_text"]
@ -41,7 +41,7 @@ bevy_diagnostic = { path = "crates/bevy_diagnostic", optional = true }
bevy_gltf = { path = "crates/bevy_gltf", optional = true }
bevy_input = { path = "crates/bevy_input", optional = true }
bevy_pbr = { path = "crates/bevy_pbr", optional = true }
bevy_props = { path = "crates/bevy_props", optional = true }
bevy_property = { path = "crates/bevy_property", optional = true }
bevy_render = { path = "crates/bevy_render", optional = true }
bevy_scene = { path = "crates/bevy_scene", optional = true }
bevy_transform = { path = "crates/bevy_transform", optional = true }
@ -154,8 +154,8 @@ name = "load_scene"
path = "examples/scene/load_scene.rs"
[[example]]
name = "props"
path = "examples/scene/props.rs"
name = "properties"
path = "examples/scene/properties.rs"
[[example]]
name = "shader_custom_material"

View file

@ -62,7 +62,7 @@ pub fn derive_props(input: TokenStream) -> TokenStream {
};
let modules = get_modules(&ast);
let bevy_props_path = get_path(&modules.bevy_props);
let bevy_property_path = get_path(&modules.bevy_property);
let field_names = fields.iter().map(|field| field.ident
.as_ref()
@ -79,32 +79,32 @@ pub fn derive_props(input: TokenStream) -> TokenStream {
let struct_name = &ast.ident;
TokenStream::from(quote! {
impl #impl_generics #bevy_props_path::Props for #struct_name#ty_generics {
impl #impl_generics #bevy_property_path::Properties for #struct_name#ty_generics {
fn type_name(&self) -> &str {
std::any::type_name::<Self>()
}
fn prop(&self, name: &str) -> Option<&dyn #bevy_props_path::Prop> {
fn prop(&self, name: &str) -> Option<&dyn #bevy_property_path::Property> {
match name {
#(#field_names => Some(&self.#field_idents),)*
_ => None,
}
}
fn prop_mut(&mut self, name: &str) -> Option<&mut dyn #bevy_props_path::Prop> {
fn prop_mut(&mut self, name: &str) -> Option<&mut dyn #bevy_property_path::Property> {
match name {
#(#field_names => Some(&mut self.#field_idents),)*
_ => None,
}
}
fn prop_with_index(&self, index: usize) -> Option<&dyn #bevy_props_path::Prop> {
fn prop_with_index(&self, index: usize) -> Option<&dyn #bevy_property_path::Property> {
match index {
#(#field_indices => Some(&self.#field_idents),)*
_ => None,
}
}
fn prop_with_index_mut(&mut self, index: usize) -> Option<&mut dyn #bevy_props_path::Prop> {
fn prop_with_index_mut(&mut self, index: usize) -> Option<&mut dyn #bevy_property_path::Property> {
match index {
#(#field_indices => Some(&mut self.#field_idents),)*
_ => None,
@ -122,8 +122,8 @@ pub fn derive_props(input: TokenStream) -> TokenStream {
#field_count
}
fn iter_props(&self) -> #bevy_props_path::PropIter {
#bevy_props_path::PropIter::new(self)
fn iter_props(&self) -> #bevy_property_path::PropertyIter {
#bevy_property_path::PropertyIter::new(self)
}
}
})

View file

@ -11,7 +11,7 @@ pub struct ModuleAttributeArgs {
#[darling(default)]
pub bevy_core: Option<String>,
#[darling(default)]
pub bevy_props: Option<String>,
pub bevy_property: Option<String>,
#[darling(default)]
pub bevy_app: Option<String>,
#[darling(default)]
@ -27,7 +27,7 @@ pub struct Modules {
pub bevy_render: String,
pub bevy_asset: String,
pub bevy_core: String,
pub bevy_props: String,
pub bevy_property: String,
pub bevy_app: String,
pub legion: String,
}
@ -38,7 +38,7 @@ impl Modules {
bevy_asset: "bevy::asset".to_string(),
bevy_render: "bevy::render".to_string(),
bevy_core: "bevy::core".to_string(),
bevy_props: "bevy::props".to_string(),
bevy_property: "bevy::property".to_string(),
bevy_app: "bevy::app".to_string(),
legion: "bevy".to_string(),
}
@ -49,7 +49,7 @@ impl Modules {
bevy_asset: "bevy_asset".to_string(),
bevy_render: "bevy_render".to_string(),
bevy_core: "bevy_core".to_string(),
bevy_props: "bevy_props".to_string(),
bevy_property: "bevy_property".to_string(),
bevy_app: "bevy_app".to_string(),
legion: "legion".to_string(),
}
@ -62,7 +62,7 @@ impl Default for ModuleAttributeArgs {
bevy_asset: None,
bevy_render: None,
bevy_core: None,
bevy_props: None,
bevy_property: None,
bevy_app: None,
legion: None,
meta: true,
@ -96,6 +96,10 @@ pub fn get_modules(ast: &DeriveInput) -> Modules {
modules.bevy_render = path;
}
if let Some(path) = module_attribute_args.bevy_property {
modules.bevy_property = path;
}
if let Some(path) = module_attribute_args.bevy_core {
modules.bevy_core = path;
}

View file

@ -1,5 +1,5 @@
[package]
name = "bevy_props"
name = "bevy_property"
version = "0.1.0"
authors = ["Carter Anderson <mcanders1@gmail.com>"]
edition = "2018"

View file

@ -1,29 +1,29 @@
use std::{collections::HashMap, borrow::Cow};
use crate::{Props, Prop, PropIter};
use crate::{Properties, Property, PropertyIter};
use serde::{Deserialize, Serialize, ser::SerializeMap};
#[derive(Default)]
pub struct DynamicProperties {
pub type_name: &'static str,
pub props: Vec<(Cow<'static, str>, Box<dyn Prop>)>,
pub props: Vec<(Cow<'static, str>, Box<dyn Property>)>,
pub prop_indices: HashMap<Cow<'static, str>, usize>,
}
impl DynamicProperties {
fn push(&mut self, name: &str, prop: Box<dyn Prop>) {
fn push(&mut self, name: &str, prop: Box<dyn Property>) {
let name: Cow<'static, str> = Cow::Owned(name.to_string());
self.props.push((name.clone(), prop));
self.prop_indices.insert(name, self.props.len());
}
pub fn set<T: Prop>(&mut self, name: &str, prop: T) {
pub fn set<T: Property>(&mut self, name: &str, prop: T) {
if let Some(index) = self.prop_indices.get(name) {
self.props[*index].1 = Box::new(prop);
} else {
self.push(name, Box::new(prop));
}
}
pub fn set_box(&mut self, name: &str, prop: Box<dyn Prop>) {
pub fn set_box(&mut self, name: &str, prop: Box<dyn Property>) {
if let Some(index) = self.prop_indices.get(name) {
self.props[*index].1 = prop;
} else {
@ -33,13 +33,13 @@ impl DynamicProperties {
}
impl Props for DynamicProperties {
impl Properties for DynamicProperties {
#[inline]
fn type_name(&self) -> &str {
self.type_name
}
#[inline]
fn prop(&self, name: &str) -> Option<&dyn Prop> {
fn prop(&self, name: &str) -> Option<&dyn Property> {
if let Some(index) = self.prop_indices.get(name) {
Some(&*self.props[*index].1)
} else {
@ -48,7 +48,7 @@ impl Props for DynamicProperties {
}
#[inline]
fn prop_mut(&mut self, name: &str) -> Option<&mut dyn Prop> {
fn prop_mut(&mut self, name: &str) -> Option<&mut dyn Property> {
if let Some(index) = self.prop_indices.get(name) {
Some(&mut *self.props[*index].1)
} else {
@ -57,12 +57,12 @@ impl Props for DynamicProperties {
}
#[inline]
fn prop_with_index(&self, index: usize) -> Option<&dyn Prop> {
fn prop_with_index(&self, index: usize) -> Option<&dyn Property> {
self.props.get(index).map(|(_i, prop)| &**prop)
}
#[inline]
fn prop_with_index_mut(&mut self, index: usize) -> Option<&mut dyn Prop> {
fn prop_with_index_mut(&mut self, index: usize) -> Option<&mut dyn Property> {
self.props.get_mut(index).map(|(_i, prop)| &mut **prop)
}
@ -76,8 +76,8 @@ impl Props for DynamicProperties {
self.props.len()
}
fn iter_props(&self) -> PropIter {
PropIter {
fn iter_props(&self) -> PropertyIter {
PropertyIter {
props: self,
index: 0,
}

View file

@ -0,0 +1,9 @@
#![feature(min_specialization)]
mod property;
mod properties;
mod dynamic_properties;
pub use property::*;
pub use properties::*;
pub use dynamic_properties::*;

View file

@ -1,23 +1,23 @@
use crate::{DynamicProperties, Prop, PropVal};
use crate::{DynamicProperties, Property, PropertyVal};
use serde::{ser::SerializeMap, Serialize};
pub trait Props {
pub trait Properties {
fn type_name(&self) -> &str;
fn prop(&self, name: &str) -> Option<&dyn Prop>;
fn prop_mut(&mut self, name: &str) -> Option<&mut dyn Prop>;
fn prop_with_index(&self, index: usize) -> Option<&dyn Prop>;
fn prop_with_index_mut(&mut self, index: usize) -> Option<&mut dyn Prop>;
fn prop(&self, name: &str) -> Option<&dyn Property>;
fn prop_mut(&mut self, name: &str) -> Option<&mut dyn Property>;
fn prop_with_index(&self, index: usize) -> Option<&dyn Property>;
fn prop_with_index_mut(&mut self, index: usize) -> Option<&mut dyn Property>;
fn prop_name(&self, index: usize) -> Option<&str>;
fn prop_len(&self) -> usize;
fn iter_props(&self) -> PropIter;
fn set_prop(&mut self, name: &str, value: &dyn Prop) {
fn iter_props(&self) -> PropertyIter;
fn set_prop(&mut self, name: &str, value: &dyn Property) {
if let Some(prop) = self.prop_mut(name) {
prop.set(value);
} else {
panic!("prop does not exist: {}", name);
}
}
fn apply(&mut self, props: &dyn Props) {
fn apply(&mut self, props: &dyn Properties) {
for (name, prop) in props.iter_props() {
self.set_prop(name, prop);
}
@ -36,19 +36,19 @@ pub trait Props {
}
}
pub struct PropIter<'a> {
pub(crate) props: &'a dyn Props,
pub struct PropertyIter<'a> {
pub(crate) props: &'a dyn Properties,
pub(crate) index: usize,
}
impl<'a> PropIter<'a> {
pub fn new(props: &'a dyn Props) -> Self {
PropIter { props, index: 0 }
impl<'a> PropertyIter<'a> {
pub fn new(props: &'a dyn Properties) -> Self {
PropertyIter { props, index: 0 }
}
}
impl<'a> Iterator for PropIter<'a> {
type Item = (&'a str, &'a dyn Prop);
impl<'a> Iterator for PropertyIter<'a> {
type Item = (&'a str, &'a dyn Property);
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.props.prop_len() {
let prop = self.props.prop_with_index(self.index).unwrap();
@ -61,14 +61,14 @@ impl<'a> Iterator for PropIter<'a> {
}
}
pub trait PropsVal {
pub trait PropertiesVal {
fn prop_val<T: 'static>(&self, name: &str) -> Option<&T>;
fn set_prop_val<T: 'static>(&mut self, name: &str, value: T);
}
impl<P> PropsVal for P
impl<P> PropertiesVal for P
where
P: Props,
P: Properties,
{
// #[inline]
fn prop_val<T: 'static>(&self, name: &str) -> Option<&T> {
@ -84,11 +84,11 @@ where
}
}
pub struct SerializableProps<'a> {
pub props: &'a dyn Props,
pub struct SerializableProperties<'a> {
pub props: &'a dyn Properties,
}
impl<'a> Serialize for SerializableProps<'a> {
impl<'a> Serialize for SerializableProperties<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,

View file

@ -1,21 +1,21 @@
use serde::Serialize;
use std::any::Any;
pub trait Prop: erased_serde::Serialize + Send + Sync + Any + '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 Prop>;
fn set(&mut self, value: &dyn Prop);
fn clone_prop(&self) -> Box<dyn Property>;
fn set(&mut self, value: &dyn Property);
}
erased_serde::serialize_trait_object!(Prop);
erased_serde::serialize_trait_object!(Property);
pub trait PropVal {
pub trait PropertyVal {
fn val<T: 'static>(&self) -> Option<&T>;
fn set_val<T: 'static>(&mut self, value: T);
}
impl PropVal for dyn Prop {
impl PropertyVal for dyn Property {
// #[inline]
default fn val<T: 'static>(&self) -> Option<&T> {
self.any().downcast_ref::<T>()
@ -31,7 +31,7 @@ impl PropVal for dyn Prop {
}
}
impl<T> Prop for T
impl<T> Property for T
where
T: Clone + Serialize + Send + Sync + Any + 'static,
{
@ -44,11 +44,11 @@ where
self
}
#[inline]
default fn clone_prop(&self) -> Box<dyn Prop> {
default fn clone_prop(&self) -> Box<dyn Property> {
Box::new(self.clone())
}
#[inline]
default fn set(&mut self, value: &dyn Prop) {
default fn set(&mut self, value: &dyn Property) {
if let Some(prop) = value.any().downcast_ref::<T>() {
*self = prop.clone();
} else {
@ -57,8 +57,8 @@ where
}
}
impl Prop for usize {
fn set(&mut self, value: &dyn Prop) {
impl Property for usize {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -86,8 +86,8 @@ impl Prop for usize {
}
}
impl Prop for u64 {
fn set(&mut self, value: &dyn Prop) {
impl Property for u64 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -115,8 +115,8 @@ impl Prop for u64 {
}
}
impl Prop for u32 {
fn set(&mut self, value: &dyn Prop) {
impl Property for u32 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -144,8 +144,8 @@ impl Prop for u32 {
}
}
impl Prop for u16 {
fn set(&mut self, value: &dyn Prop) {
impl Property for u16 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -173,8 +173,8 @@ impl Prop for u16 {
}
}
impl Prop for u8 {
fn set(&mut self, value: &dyn Prop) {
impl Property for u8 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -202,8 +202,8 @@ impl Prop for u8 {
}
}
impl Prop for isize {
fn set(&mut self, value: &dyn Prop) {
impl Property for isize {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -231,8 +231,8 @@ impl Prop for isize {
}
}
impl Prop for i64 {
fn set(&mut self, value: &dyn Prop) {
impl Property for i64 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -260,8 +260,8 @@ impl Prop for i64 {
}
}
impl Prop for i32 {
fn set(&mut self, value: &dyn Prop) {
impl Property for i32 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -289,8 +289,8 @@ impl Prop for i32 {
}
}
impl Prop for i16 {
fn set(&mut self, value: &dyn Prop) {
impl Property for i16 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -318,8 +318,8 @@ impl Prop for i16 {
}
}
impl Prop for i8 {
fn set(&mut self, value: &dyn Prop) {
impl Property for i8 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -348,8 +348,8 @@ impl Prop for i8 {
}
impl Prop for f32 {
fn set(&mut self, value: &dyn Prop) {
impl Property for f32 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
@ -361,8 +361,8 @@ impl Prop for f32 {
}
}
impl Prop for f64 {
fn set(&mut self, value: &dyn Prop) {
impl Property for f64 {
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;

View file

@ -1,9 +0,0 @@
#![feature(min_specialization)]
mod prop;
mod props;
mod dynamic_properties;
pub use prop::*;
pub use props::*;
pub use dynamic_properties::*;

View file

@ -7,7 +7,7 @@ edition = "2018"
[dependencies]
bevy_app = { path = "../bevy_app" }
bevy_asset = { path = "../bevy_asset" }
bevy_props = { path = "../bevy_props" }
bevy_property = { path = "../bevy_property" }
legion = { path = "../bevy_legion", features = ["serialize"] }
serde = { version = "1.0", features = ["derive"]}
erased-serde = "0.3"

View file

@ -2,7 +2,7 @@ use crate::{ComponentRegistry, ComponentRegistryContext, SceneDeserializer};
use anyhow::Result;
use bevy_app::FromResources;
use bevy_asset::AssetLoader;
use bevy_props::DynamicProperties;
use bevy_property::DynamicProperties;
use legion::prelude::{Resources, World};
use serde::de::DeserializeSeed;
use serde::{Serialize, Deserialize};

View file

@ -1,6 +1,8 @@
use bevy::prelude::*;
use bevy_props::SerializableProps;
use bevy_scene::{DynamicScene, SceneEntity};
use bevy::{
prelude::*,
property::SerializableProperties,
scene::{DynamicScene, SceneEntity},
};
use serde::ser::Serialize;
fn main() {
@ -10,7 +12,7 @@ fn main() {
.run();
}
#[derive(Properties)]
#[derive(Properties, Default)]
pub struct Test {
a: usize,
b: String,
@ -41,20 +43,23 @@ fn setup() {
assert_eq!(test.a, 3);
let ser = SerializableProps { props: &test };
let ser = SerializableProperties { props: &test };
let mut serializer = ron::ser::Serializer::new(Some(ron::ser::PrettyConfig::default()), false);
ser.serialize(&mut serializer).unwrap();
println!("{}", serializer.into_output_string());
let ron_string = serializer.into_output_string();
println!("{}", ron_string);
let dynamic_scene = DynamicScene {
entities: vec![SceneEntity {
entity: 12345,
components: vec![patch],
}],
};
// let dynamic_scene = DynamicScene {
// entities: vec![SceneEntity {
// entity: 12345,
// components: vec![patch],
// }],
// };
let mut serializer = ron::ser::Serializer::new(Some(ron::ser::PrettyConfig::default()), false);
dynamic_scene.entities.serialize(&mut serializer).unwrap();
println!("{}", serializer.into_output_string());
// let mut serializer = ron::ser::Serializer::new(Some(ron::ser::PrettyConfig::default()), false);
// dynamic_scene.entities.serialize(&mut serializer).unwrap();
// println!("{}", serializer.into_output_string());
let mut deserializer = ron::de::Deserializer::from_str(&ron_string).unwrap();
}

View file

@ -61,8 +61,8 @@ pub use bevy_gltf as gltf;
pub use bevy_input as input;
#[cfg(feature = "pbr")]
pub use bevy_pbr as pbr;
#[cfg(feature = "props")]
pub use bevy_props as props;
#[cfg(feature = "property")]
pub use bevy_property as property;
#[cfg(feature = "render")]
pub use bevy_render as render;
#[cfg(feature = "scene")]

View file

@ -11,8 +11,8 @@ pub use crate::derive::*;
pub use crate::diagnostic::DiagnosticsPlugin;
#[cfg(feature = "pbr")]
pub use crate::pbr::{entity::*, light::Light, material::StandardMaterial};
#[cfg(feature = "props")]
pub use crate::props::{Prop, Props, PropVal, PropsVal, DynamicProperties};
#[cfg(feature = "property")]
pub use crate::property::{Property, Properties, PropertyVal, PropertiesVal, DynamicProperties};
#[cfg(feature = "render")]
pub use crate::render::{
draw_target,