From e5018c8258ca2cc94c4c914a25f76a5b50adcf52 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 30 Mar 2024 22:42:41 -0400 Subject: [PATCH] reflect: fix type registry from changes with ecs resources --- .envrc | 1 + lyra-reflect/src/lib.rs | 28 ++++++++++++++-------------- lyra-reflect/src/registry.rs | 14 +++++++------- lyra-reflect/src/resource.rs | 20 ++++++++++++-------- 4 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..17d6464 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use_nix \ No newline at end of file diff --git a/lyra-reflect/src/lib.rs b/lyra-reflect/src/lib.rs index 8823aa7..d1bf4f7 100644 --- a/lyra-reflect/src/lib.rs +++ b/lyra-reflect/src/lib.rs @@ -2,7 +2,7 @@ use std::{any::TypeId, any::Any, cell::{Ref, RefMut}}; -use lyra_ecs::World; +use lyra_ecs::{AtomicRef, AtomicRefMut, World}; extern crate self as lyra_reflect; @@ -250,46 +250,46 @@ pub trait FromType { pub trait ReflectWorldExt { /// Retrieves the type registry from the world. - fn get_type_registry(&self) -> Ref; + fn get_type_registry(&self) -> AtomicRef; /// Retrieves the type registry mutably from the world. - fn get_type_registry_mut(&self) -> RefMut; + fn get_type_registry_mut(&self) -> AtomicRefMut; /// Get a registered type from the type registry. Returns `None` if the type is not registered - fn get_type(&self, type_id: TypeId) -> Option>; + fn get_type(&self, type_id: TypeId) -> Option>; /// Get a mutable registered type from the type registry in the world. returns `None` if the type is not registered. - fn get_type_mut(&self, type_id: TypeId) -> Option>; + fn get_type_mut(&self, type_id: TypeId) -> Option>; /// Get a registered type, or register a new type and return it. - fn get_type_or_default(&self, type_id: TypeId) -> RefMut; + fn get_type_or_default(&self, type_id: TypeId) -> AtomicRefMut; } impl ReflectWorldExt for World { - fn get_type_registry(&self) -> Ref { + fn get_type_registry(&self) -> AtomicRef { self.get_resource::() } - fn get_type_registry_mut(&self) -> RefMut { + fn get_type_registry_mut(&self) -> AtomicRefMut { self.get_resource_mut::() } - fn get_type(&self, type_id: TypeId) -> Option> { + fn get_type(&self, type_id: TypeId) -> Option> { let r = self.get_resource::(); if r.has_type(type_id) { - Some(Ref::map(r, |tr| tr.get_type(type_id).unwrap())) + Some(AtomicRef::map(r, |tr| tr.get_type(type_id).unwrap())) } else { None } } - fn get_type_mut(&self, type_id: TypeId) -> Option> { + fn get_type_mut(&self, type_id: TypeId) -> Option> { let r = self.get_resource_mut::(); if r.has_type(type_id) { - Some(RefMut::map(r, |tr| tr.get_type_mut(type_id).unwrap())) + Some(AtomicRefMut::map(r, |tr| tr.get_type_mut(type_id).unwrap())) } else { None } } - fn get_type_or_default(&self, type_id: TypeId) -> RefMut { + fn get_type_or_default(&self, type_id: TypeId) -> AtomicRefMut { let r = self.get_resource_mut::(); - RefMut::map(r, |tr| tr.get_type_or_default(type_id)) + AtomicRefMut::map(r, |tr| tr.get_type_or_default(type_id)) } } \ No newline at end of file diff --git a/lyra-reflect/src/registry.rs b/lyra-reflect/src/registry.rs index 46651f4..135dd79 100644 --- a/lyra-reflect/src/registry.rs +++ b/lyra-reflect/src/registry.rs @@ -1,4 +1,4 @@ -use std::{any::{TypeId, type_name}, collections::HashMap, ops::Deref}; +use std::{any::{type_name, Any, TypeId}, collections::HashMap, ops::Deref}; /// Storage for #[derive(Default, Clone)] @@ -44,19 +44,19 @@ impl TypeRegistry { } } -pub trait TypeData: std::any::Any { - fn as_any(&self) -> &dyn std::any::Any; - fn as_any_mut(&mut self) -> &mut dyn std::any::Any; +pub trait TypeData: Send + Sync + Any { + fn as_any(&self) -> &dyn Any; + fn as_any_mut(&mut self) -> &mut dyn Any; fn boxed_clone(&self) -> Box; } -impl TypeData for T { - fn as_any(&self) -> &dyn std::any::Any { +impl TypeData for T { + fn as_any(&self) -> &dyn Any { self } - fn as_any_mut(&mut self) -> &mut dyn std::any::Any { + fn as_any_mut(&mut self) -> &mut dyn Any { self } diff --git a/lyra-reflect/src/resource.rs b/lyra-reflect/src/resource.rs index ab7445c..f7caca1 100644 --- a/lyra-reflect/src/resource.rs +++ b/lyra-reflect/src/resource.rs @@ -1,6 +1,6 @@ -use std::{any::{Any, TypeId}, cell::{Ref, RefMut}, ptr::NonNull}; +use std::{any::{Any, TypeId}, mem, ptr::NonNull}; -use lyra_ecs::{World, ResourceObject}; +use lyra_ecs::{AtomicRef, AtomicRefMut, ResourceObject, World}; use crate::{Reflect, FromType}; @@ -8,20 +8,20 @@ use crate::{Reflect, FromType}; pub struct ReflectedResource { pub type_id: TypeId, - fn_reflect: for<'a> fn (world: &'a World) -> Option>, - fn_reflect_mut: for<'a> fn (world: &'a mut World) -> Option>, + fn_reflect: for<'a> fn (world: &'a World) -> Option>, + fn_reflect_mut: for<'a> fn (world: &'a mut World) -> Option>, fn_reflect_ptr: fn (world: &mut World) -> Option>, fn_refl_insert: fn (world: &mut World, this: Box), } impl ReflectedResource { /// Retrieves the reflected resource from the world. - pub fn reflect<'a>(&self, world: &'a World) -> Option> { + pub fn reflect<'a>(&self, world: &'a World) -> Option> { (self.fn_reflect)(world) } /// Retrieves a mutable reflected resource from the world. - pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option> { + pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option> { (self.fn_reflect_mut)(world) } @@ -41,11 +41,15 @@ impl FromType for ReflectedResource { type_id: TypeId::of::(), fn_reflect: |world: &World| { world.try_get_resource::() - .map(|r| r as Ref) + .map(|r| { + AtomicRef::map(r, |r| r as &dyn Reflect) + }) }, fn_reflect_mut: |world: &mut World| { world.try_get_resource_mut::() - .map(|r| r as RefMut) + .map(|r| { + AtomicRefMut::map(r, |r| r as &mut dyn Reflect) + }) }, fn_reflect_ptr: |world: &mut World| unsafe { world.try_get_resource_ptr::()