Skip to content

Commit

Permalink
props: remove specialization, ignore fields, impl for Handle, fix wor…
Browse files Browse the repository at this point in the history
…ld round tripping
  • Loading branch information
cart committed May 25, 2020
1 parent 1cd3b4c commit 0826d74
Show file tree
Hide file tree
Showing 16 changed files with 745 additions and 307 deletions.
24 changes: 14 additions & 10 deletions assets/scene/load_scene_example.scn
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
[
(
entity: 1279729879,

Entity(
id: 1401014920,
components: [

{
"type": "load_scene::Test",
"x": 3.0,
"y": 4.0,
},
],
}, ],
),
(
entity: 1639302665,
Entity(
id: 99763910,
components: [

{
"type": "load_scene::Test",
"x": 1.0,
"y": 2.0,
},
{
"type": "bevy_asset::handle::Handle<bevy_render::mesh::Mesh>",
"id": HandleId("edcdd23f-5be5-4dbf-9a6e-9f46b2185570"),
},
{
"type": "load_scene::Foo",
"value": "hello",
},
],
),
]
}, ],
),]
2 changes: 2 additions & 0 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ filesystem_watcher = ["notify"]
[dependencies]
bevy_app = { path = "../bevy_app" }
bevy_core = { path = "../bevy_core" }
bevy_property = { path = "../bevy_property" }
legion = { path = "../bevy_legion" }

uuid = { version = "0.8", features = ["v4", "serde"] }
serde = { version = "1", features = ["derive"] }
crossbeam-channel = "0.4.2"
anyhow = "1.0"
thiserror = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use std::{
path::{Path, PathBuf},
};

pub enum AssetEvent<T> {
pub enum AssetEvent<T: 'static> {
Created { handle: Handle<T> },
Modified { handle: Handle<T> },
Removed { handle: Handle<T> },
}

pub struct Assets<T> {
pub struct Assets<T: 'static> {
assets: HashMap<Handle<T>, T>,
paths: HashMap<PathBuf, Handle<T>>,
events: Events<AssetEvent<T>>,
Expand Down
42 changes: 40 additions & 2 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use std::{
};

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)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct HandleId(pub Uuid);
pub const DEFAULT_HANDLE_ID: HandleId =
HandleId(Uuid::from_u128(240940089166493627844978703213080810552));
Expand All @@ -17,8 +19,40 @@ impl HandleId {
}
}

pub struct Handle<T> {
impl Property for HandleId {
fn any(&self) -> &dyn std::any::Any {
self
}
fn any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn clone_prop(&self) -> Box<dyn Property> {
Box::new(self.clone())
}
fn set(&mut self, value: &dyn Property) {
let value = value.any();
if let Some(prop) = value.downcast_ref::<Self>() {
*self = *prop;
} else {
panic!("prop value is not {}", std::any::type_name::<Self>());
}
}
fn apply(&mut self, value: &dyn Property) {
self.set(value);
}
}

impl AsProperties for HandleId {
fn as_properties(&self) -> Option<&dyn Properties> {
None
}

}

#[derive(Properties)]
pub struct Handle<T> where T: 'static {
pub id: HandleId,
#[prop(ignore)]
marker: PhantomData<T>,
}

Expand Down Expand Up @@ -137,6 +171,10 @@ impl<T> Clone for Handle<T> {
}
impl<T> Copy for Handle<T> {}

// SAFE: T is phantom data and Handle::id is an integer
unsafe impl<T> Send for Handle<T> {}
unsafe impl<T> Sync for Handle<T> {}

#[derive(Hash, Copy, Clone, Eq, PartialEq, Debug)]
pub struct HandleUntyped {
pub id: HandleId,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_asset/src/load_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub trait AssetLoadRequestHandler: Send + Sync + 'static {
pub struct ChannelAssetHandler<TLoader, TAsset>
where
TLoader: AssetLoader<TAsset>,
TAsset: 'static,
{
sender: Sender<AssetResult<TAsset>>,
loader: TLoader,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ pub trait AssetLoader<T>: Send + Sync + 'static {
}
}

pub struct AssetResult<T> {
pub struct AssetResult<T: 'static> {
pub result: Result<T, AssetLoadError>,
pub handle: Handle<T>,
pub path: PathBuf,
}

pub struct AssetChannel<T> {
pub struct AssetChannel<T: 'static> {
pub sender: Sender<AssetResult<T>>,
pub receiver: Receiver<AssetResult<T>>,
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_legion/legion_core/src/guid_entity_allocator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::entity::Entity;
use parking_lot::RwLock;
use std::{collections::HashSet, num::Wrapping, sync::Arc};
use std::{collections::{VecDeque, HashSet}, num::Wrapping, sync::Arc};

#[derive(Default, Debug, Clone)]
pub struct GuidEntityAllocator {
entities: Arc<RwLock<HashSet<Entity>>>,
next_ids: Arc<RwLock<Vec<Entity>>>,
next_ids: Arc<RwLock<VecDeque<Entity>>>,
}

impl GuidEntityAllocator {
Expand All @@ -18,7 +18,7 @@ impl GuidEntityAllocator {
/// Allocates a new unused `Entity` ID.
pub fn create_entity(&self) -> Entity {
let entity = if !self.next_ids.read().is_empty() {
self.next_ids.write().pop().unwrap()
self.next_ids.write().pop_front().unwrap()
} else {
Entity::new(rand::random::<u32>(), Wrapping(1))
};
Expand Down
45 changes: 41 additions & 4 deletions crates/bevy_property/bevy_property_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ extern crate proc_macro;

mod modules;

use darling::FromMeta;
use modules::{get_modules, get_path};
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields};
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Field, Fields};

#[derive(FromMeta, Debug, Default)]
struct PropAttributeArgs {
#[darling(default)]
pub ignore: Option<bool>,
}

static PROP_ATTRIBUTE_NAME: &str = "prop";

#[proc_macro_derive(Properties, attributes(prop, module))]
pub fn derive_properties(input: TokenStream) -> TokenStream {
Expand All @@ -17,19 +26,47 @@ pub fn derive_properties(input: TokenStream) -> TokenStream {
}) => &fields.named,
_ => panic!("expected a struct with named fields"),
};
let fields_and_args = fields
.iter()
.map(|f| {
(
f,
f.attrs
.iter()
.find(|a| {
a.path.get_ident().as_ref().unwrap().to_string() == PROP_ATTRIBUTE_NAME
})
.map(|a| {
PropAttributeArgs::from_meta(&a.parse_meta().unwrap())
.unwrap_or_else(|_err| PropAttributeArgs::default())
}),
)
})
.collect::<Vec<(&Field, Option<PropAttributeArgs>)>>();
let active_fields = fields_and_args
.iter()
.filter(|(_field, attrs)| {
attrs.is_none()
|| match attrs.as_ref().unwrap().ignore {
Some(ignore) => !ignore,
None => true,
}
})
.map(|(f, _attr)| *f)
.collect::<Vec<&Field>>();

let modules = get_modules(&ast);
let bevy_property_path = get_path(&modules.bevy_property);

let field_names = fields
let field_names = active_fields
.iter()
.map(|field| field.ident.as_ref().unwrap().to_string())
.collect::<Vec<String>>();
let field_idents = fields
let field_idents = active_fields
.iter()
.map(|field| field.ident.as_ref().unwrap())
.collect::<Vec<_>>();
let field_count = fields.len();
let field_count = active_fields.len();
let field_indices = (0..field_count).collect::<Vec<usize>>();

let generics = ast.generics;
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_property/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(min_specialization)]

mod property;
mod properties;
mod dynamic_properties;
Expand Down
Loading

0 comments on commit 0826d74

Please sign in to comment.