diff options
-rw-r--r-- | ecs-macros/src/lib.rs | 24 | ||||
-rw-r--r-- | ecs/src/component.rs | 36 | ||||
-rw-r--r-- | ecs/src/component/storage.rs | 20 | ||||
-rw-r--r-- | ecs/src/component/storage/archetype.rs | 6 | ||||
-rw-r--r-- | ecs/src/lib.rs | 54 | ||||
-rw-r--r-- | ecs/src/lock.rs | 90 | ||||
-rw-r--r-- | ecs/src/sole.rs | 11 | ||||
-rw-r--r-- | ecs/src/system/stateful.rs | 7 | ||||
-rw-r--r-- | ecs/src/type_name.rs | 15 |
9 files changed, 97 insertions, 166 deletions
diff --git a/ecs-macros/src/lib.rs b/ecs-macros/src/lib.rs index 862b0b1..a5acc2b 100644 --- a/ecs-macros/src/lib.rs +++ b/ecs-macros/src/lib.rs @@ -120,7 +120,6 @@ pub fn component_derive(input: TokenStream) -> TokenStream }; use #ecs_path::uid::{Uid, Kind as UidKind}; use #ecs_path::system::Input as SystemInput; - use #ecs_path::type_name::TypeName; use super::*; @@ -139,6 +138,11 @@ pub fn component_derive(input: TokenStream) -> TokenStream #get_id } + fn name(&self) -> &'static str + { + std::any::type_name::<Self>() + } + fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid { match event_kind { @@ -156,15 +160,6 @@ pub fn component_derive(input: TokenStream) -> TokenStream #where_clause { } - - impl #impl_generics TypeName for #item_ident #type_generics - #where_clause - { - fn type_name(&self) -> &'static str - { - std::any::type_name::<Self>() - } - } } } .into() @@ -204,15 +199,6 @@ pub fn sole_derive(input: TokenStream) -> TokenStream self } } - - impl #impl_generics #ecs_path::type_name::TypeName for #item_ident #type_generics - #where_clause - { - fn type_name(&self) -> &'static str - { - std::any::type_name::<Self>() - } - } } .into() } diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 525bd98..9ec962d 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -18,7 +18,6 @@ use crate::lock::{ WriteGuard, }; use crate::system::Input as SystemInput; -use crate::type_name::TypeName; use crate::uid::Uid; use crate::util::Array; use crate::World; @@ -27,7 +26,7 @@ pub mod local; pub(crate) mod storage; -pub trait Component: SystemInput + Any + TypeName +pub trait Component: SystemInput + Any { /// The component type in question. Will usually be `Self` type Component: Component @@ -47,6 +46,9 @@ pub trait Component: SystemInput + Any + TypeName where Self: Sized; + /// Returns the name of this component. + fn name(&self) -> &'static str; + /// Returns the component UID of a component event for this component. fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid; @@ -92,14 +94,6 @@ impl Debug for dyn Component } } -impl TypeName for Box<dyn Component> -{ - fn type_name(&self) -> &'static str - { - self.as_ref().type_name() - } -} - impl<ComponentT> Component for Option<ComponentT> where ComponentT: Component, @@ -115,6 +109,11 @@ where ComponentT::id() } + fn name(&self) -> &'static str + { + type_name::<Self>() + } + fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid { match event_kind { @@ -133,16 +132,6 @@ where } } -impl<ComponentT> TypeName for Option<ComponentT> -where - ComponentT: Component, -{ - fn type_name(&self) -> &'static str - { - type_name::<Self>() - } -} - impl<ComponentT> SystemInput for Option<ComponentT> where ComponentT: Component {} /// A sequence of components. @@ -238,8 +227,7 @@ impl<'a, ComponentT: Component> Handle<'a, ComponentT> inner: inner.map(|component| { component.downcast_ref::<ComponentT>().unwrap_or_else(|| { panic!( - "Cannot downcast component {} to type {}", - component.type_name(), + "Failed to downcast component to type {}", type_name::<ComponentT>() ); }) @@ -304,11 +292,9 @@ impl<'a, ComponentT: Component> HandleMut<'a, ComponentT> { Self { inner: inner.map(|component| { - let component_type_name = component.type_name(); - component.downcast_mut::<ComponentT>().unwrap_or_else(|| { panic!( - "Cannot downcast component {component_type_name} to type {}", + "Failed to downcast component to type {}", type_name::<ComponentT>() ); }) diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 0a6382b..14aa191 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -1,4 +1,3 @@ -use std::any::type_name; use std::array::IntoIter as ArrayIter; use std::cell::RefCell; use std::vec::IntoIter as VecIntoIter; @@ -18,7 +17,6 @@ use crate::component::storage::graph::{ Graph, }; use crate::component::Component; -use crate::type_name::TypeName; use crate::uid::{Kind as UidKind, Uid}; use crate::util::{BorrowedOrOwned, Either, StreamingIterator, VecExt}; @@ -161,11 +159,13 @@ impl Storage pub fn add_entity_component( &mut self, entity_uid: Uid, - component: (Uid, Box<dyn Component>), + (component_id, component_name, component): ( + Uid, + &'static str, + Box<dyn Component>, + ), ) -> Result<(), Error> { - let (component_id, component) = component; - debug_assert!( !component.self_is_optional(), "Adding a optional component to a entity is not supported" @@ -244,7 +244,7 @@ impl Storage entity.insert_component( component_id, - ArchetypeEntityComponent::new(component), + ArchetypeEntityComponent::new(component, component_name), add_edge_archetype, ); @@ -393,14 +393,6 @@ impl Storage } } -impl TypeName for Storage -{ - fn type_name(&self) -> &'static str - { - type_name::<Self>() - } -} - #[cfg(feature = "vizoxide")] impl Storage { diff --git a/ecs/src/component/storage/archetype.rs b/ecs/src/component/storage/archetype.rs index 5306cf9..8d48e13 100644 --- a/ecs/src/component/storage/archetype.rs +++ b/ecs/src/component/storage/archetype.rs @@ -215,11 +215,11 @@ pub struct EntityComponent impl EntityComponent { - pub fn new(component: Box<dyn Component>) -> Self + pub fn new(component: Box<dyn Component>, component_name: &'static str) -> Self { Self { - name: component.type_name(), - component: Lock::new(component), + name: component_name, + component: Lock::new(component, component_name), } } diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 32d82bc..2c653ee 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -31,7 +31,6 @@ use crate::relationship::{ChildOf, DependsOn, Relationship}; use crate::sole::Sole; use crate::stats::Stats; use crate::system::{System, SystemComponent}; -use crate::type_name::TypeName; use crate::uid::{Kind as UidKind, Uid}; pub mod actions; @@ -47,7 +46,6 @@ pub mod sole; pub mod stats; pub mod system; pub mod tuple; -pub mod type_name; pub mod uid; pub mod util; @@ -511,9 +509,10 @@ impl World ) { for (component_id, component) in components { - if let Err(err) = component_storage - .add_entity_component(entity_uid, (component_id, component)) - { + if let Err(err) = component_storage.add_entity_component( + entity_uid, + (component_id, component.name(), component), + ) { tracing::error!("Failed to add component to entity: {err}"); } } @@ -588,7 +587,7 @@ pub enum StepResult Stop, } -#[derive(Debug, Default)] +#[derive(Debug)] pub struct WorldData { component_storage: Arc<Lock<ComponentStorage>>, @@ -596,6 +595,21 @@ pub struct WorldData action_queue: Arc<ActionQueue>, } +impl Default for WorldData +{ + fn default() -> Self + { + Self { + component_storage: Arc::new(Lock::new( + ComponentStorage::default(), + type_name::<ComponentStorage>(), + )), + sole_storage: SoleStorage::default(), + action_queue: Arc::new(ActionQueue::default()), + } + } +} + #[derive(Debug)] pub struct EntityComponentRef<'a> { @@ -623,7 +637,7 @@ enum ActiveActionQueue B, } -#[derive(Debug, Default)] +#[derive(Debug)] struct ActionQueue { queue_a: Lock<Vec<Action>>, @@ -650,11 +664,15 @@ impl ActionQueue } } -impl TypeName for ActionQueue +impl Default for ActionQueue { - fn type_name(&self) -> &'static str + fn default() -> Self { - type_name::<Self>() + Self { + queue_a: Lock::new(Vec::new(), type_name::<Vec<Action>>()), + queue_b: Lock::new(Vec::new(), type_name::<Vec<Action>>()), + active_queue: RefCell::new(ActiveActionQueue::default()), + } } } @@ -699,7 +717,7 @@ impl SoleStorage self.storage.insert( sole_type_id, ManuallyDrop::new(StoredSole { - sole: Arc::new(Lock::new(Box::new(sole))), + sole: Arc::new(Lock::new(Box::new(sole), type_name::<SoleT>())), drop_last, }), ); @@ -716,18 +734,9 @@ impl Drop for SoleStorage for sole in self.storage.values_mut() { if sole.drop_last { - tracing::trace!( - "Sole {} pushed to dropping last queue", - sole.sole.read_nonblock().unwrap().type_name() - ); - soles_to_drop_last.push(sole); continue; } - tracing::trace!( - "Dropping sole {}", - sole.sole.read_nonblock().unwrap().type_name() - ); unsafe { ManuallyDrop::drop(sole); @@ -735,11 +744,6 @@ impl Drop for SoleStorage } for sole in &mut soles_to_drop_last { - tracing::trace!( - "Dropping sole {} last", - sole.sole.read_nonblock().unwrap().type_name() - ); - unsafe { ManuallyDrop::drop(sole); } diff --git a/ecs/src/lock.rs b/ecs/src/lock.rs index d6ed40e..9293710 100644 --- a/ecs/src/lock.rs +++ b/ecs/src/lock.rs @@ -9,23 +9,21 @@ use parking_lot::{ RwLockWriteGuard, }; -use crate::type_name::TypeName; - -#[derive(Debug, Default)] +#[derive(Debug)] pub struct Lock<Value> -where - Value: TypeName, { inner: RwLock<Value>, + value_type_name: &'static str, } impl<Value> Lock<Value> -where - Value: TypeName, { - pub fn new(value: Value) -> Self + pub fn new(value: Value, value_type_name: &'static str) -> Self { - Self { inner: RwLock::new(value) } + Self { + inner: RwLock::new(value), + value_type_name, + } } /// Tries to a acquire a handle to the resource with read access. @@ -36,9 +34,12 @@ where { let guard = self.inner.try_read().ok_or(Error::ReadUnavailable)?; - tracing::trace!("Acquired lock to value of type {}", guard.type_name()); + tracing::trace!("Acquired lock to value of type {}", self.value_type_name); - Ok(ReadGuard { inner: guard }) + Ok(ReadGuard { + inner: guard, + value_type_name: self.value_type_name, + }) } /// Tries to a acquire a handle to the resource with mutable access. @@ -51,10 +52,13 @@ where tracing::trace!( "Acquired mutable lock to value of type {}", - guard.type_name() + self.value_type_name ); - Ok(WriteGuard { inner: guard }) + Ok(WriteGuard { + inner: guard, + value_type_name: self.value_type_name, + }) } pub fn into_inner(self) -> Value @@ -75,23 +79,20 @@ pub enum Error #[derive(Debug)] pub struct ReadGuard<'guard, Value> -where - Value: TypeName, { inner: RwLockReadGuard<'guard, Value>, + value_type_name: &'static str, } impl<'guard, Value> ReadGuard<'guard, Value> -where - Value: TypeName, { pub fn map<NewValue>( self, func: impl FnOnce(&Value) -> &NewValue, ) -> MappedReadGuard<'guard, NewValue> - where - NewValue: TypeName, { + let value_type_name = self.value_type_name; + // The 'inner' field cannot be moved out of ReadGuard in a normal way since // ReadGuard implements Drop let inner = unsafe { std::ptr::read(&self.inner) }; @@ -99,13 +100,12 @@ where MappedReadGuard { inner: RwLockReadGuard::map(inner, func), + value_type_name, } } } impl<Value> Deref for ReadGuard<'_, Value> -where - Value: TypeName, { type Target = Value; @@ -116,26 +116,21 @@ where } impl<Value> Drop for ReadGuard<'_, Value> -where - Value: TypeName, { fn drop(&mut self) { - tracing::trace!("Dropped lock to value of type {}", self.type_name()); + tracing::trace!("Dropped lock to value of type {}", self.value_type_name); } } #[derive(Debug)] pub struct MappedReadGuard<'guard, Value> -where - Value: TypeName, { inner: MappedRwLockReadGuard<'guard, Value>, + value_type_name: &'static str, } impl<Value> Deref for MappedReadGuard<'_, Value> -where - Value: TypeName, { type Target = Value; @@ -146,34 +141,32 @@ where } impl<Value> Drop for MappedReadGuard<'_, Value> -where - Value: TypeName, { fn drop(&mut self) { - tracing::trace!("Dropped mapped lock to value of type {}", self.type_name()); + tracing::trace!( + "Dropped mapped lock to value of type {}", + self.value_type_name + ); } } #[derive(Debug)] pub struct WriteGuard<'guard, Value> -where - Value: TypeName, { inner: RwLockWriteGuard<'guard, Value>, + value_type_name: &'static str, } impl<'guard, Value> WriteGuard<'guard, Value> -where - Value: TypeName, { pub fn map<NewValue>( self, func: impl FnOnce(&mut Value) -> &mut NewValue, ) -> MappedWriteGuard<'guard, NewValue> - where - NewValue: TypeName, { + let value_type_name = self.value_type_name; + // The 'inner' field cannot be moved out of ReadGuard in a normal way since // ReadGuard implements Drop let inner = unsafe { std::ptr::read(&self.inner) }; @@ -181,13 +174,12 @@ where MappedWriteGuard { inner: RwLockWriteGuard::map(inner, func), + value_type_name, } } } impl<Value> Deref for WriteGuard<'_, Value> -where - Value: TypeName, { type Target = Value; @@ -198,8 +190,6 @@ where } impl<Value> DerefMut for WriteGuard<'_, Value> -where - Value: TypeName, { fn deref_mut(&mut self) -> &mut Self::Target { @@ -208,26 +198,24 @@ where } impl<Value> Drop for WriteGuard<'_, Value> -where - Value: TypeName, { fn drop(&mut self) { - tracing::trace!("Dropped mutable lock to value of type {}", self.type_name()); + tracing::trace!( + "Dropped mutable lock to value of type {}", + self.value_type_name + ); } } #[derive(Debug)] pub struct MappedWriteGuard<'guard, Value> -where - Value: TypeName, { inner: MappedRwLockWriteGuard<'guard, Value>, + value_type_name: &'static str, } impl<Value> Deref for MappedWriteGuard<'_, Value> -where - Value: TypeName, { type Target = Value; @@ -238,8 +226,6 @@ where } impl<Value> DerefMut for MappedWriteGuard<'_, Value> -where - Value: TypeName, { fn deref_mut(&mut self) -> &mut Self::Target { @@ -248,14 +234,12 @@ where } impl<Value> Drop for MappedWriteGuard<'_, Value> -where - Value: TypeName, { fn drop(&mut self) { tracing::trace!( "Dropped mapped mutable lock to value of type {}", - self.type_name() + self.value_type_name ); } } diff --git a/ecs/src/sole.rs b/ecs/src/sole.rs index 5af5ce3..1cce419 100644 --- a/ecs/src/sole.rs +++ b/ecs/src/sole.rs @@ -6,11 +6,10 @@ use std::sync::{Arc, Weak}; use crate::lock::{Lock, WriteGuard}; use crate::system::{Param as SystemParam, System}; -use crate::type_name::TypeName; use crate::World; /// A type which has a single instance and is shared globally. -pub trait Sole: Any + TypeName +pub trait Sole: Any { fn drop_last(&self) -> bool; @@ -40,14 +39,6 @@ impl Debug for dyn Sole } } -impl TypeName for Box<dyn Sole> -{ - fn type_name(&self) -> &'static str - { - self.as_ref().type_name() - } -} - /// Holds a reference to a globally shared singleton value. #[derive(Debug)] pub struct Single<'world, SoleT: Sole> diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs index 9d911ee..5415cc7 100644 --- a/ecs/src/system/stateful.rs +++ b/ecs/src/system/stateful.rs @@ -1,4 +1,4 @@ -use std::any::{Any, TypeId}; +use std::any::{type_name, Any, TypeId}; use std::panic::{RefUnwindSafe, UnwindSafe}; use hashbrown::HashMap; @@ -127,7 +127,10 @@ macro_rules! impl_system { self.local_components .insert( LocalComponent::id(), - Lock::new(Box::new(local_component)) + Lock::new( + Box::new(local_component), + type_name::<LocalComponent>() + ) ); } } diff --git a/ecs/src/type_name.rs b/ecs/src/type_name.rs deleted file mode 100644 index 54179be..0000000 --- a/ecs/src/type_name.rs +++ /dev/null @@ -1,15 +0,0 @@ -use std::any::type_name; - -pub trait TypeName -{ - /// Returns the name of this type. - fn type_name(&self) -> &'static str; -} - -impl<Item> TypeName for Vec<Item> -{ - fn type_name(&self) -> &'static str - { - type_name::<Self>() - } -} |