diff options
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r-- | ecs/src/component.rs | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 07701c8..120ba30 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -1,19 +1,20 @@ use std::any::{Any, TypeId}; -use std::cell::{RefCell, RefMut}; use std::fmt::Debug; use std::ops::{Deref, DerefMut}; use seq_macro::seq; +use crate::lock::{Lock, WriteGuard}; use crate::system::{ ComponentRefMut, Input as SystemInput, Param as SystemParam, System, }; +use crate::type_name::TypeName; use crate::WorldData; -pub trait Component: SystemInput + Any +pub trait Component: SystemInput + Any + TypeName { #[doc(hidden)] fn as_any_mut(&mut self) -> &mut dyn Any; @@ -48,6 +49,14 @@ impl Debug for dyn Component } } +impl TypeName for Box<dyn Component> +{ + fn type_name(&self) -> &'static str + { + self.as_ref().type_name() + } +} + /// A sequence of components. pub trait Sequence { @@ -59,7 +68,9 @@ pub trait Sequence fn type_ids() -> Vec<TypeId>; - fn from_components(components: &[RefCell<Box<dyn Component>>]) -> Self::Refs<'_>; + fn from_components<'component>( + components: impl Iterator<Item = &'component Lock<Box<dyn Component>>>, + ) -> Self::Refs<'component>; } macro_rules! inner { @@ -83,18 +94,18 @@ macro_rules! inner { ] } - fn from_components( - components: &[RefCell<Box<dyn Component>>] - ) -> Self::Refs<'_> + fn from_components<'component>( + components: impl Iterator<Item = &'component Lock<Box<dyn Component>>>, + ) -> Self::Refs<'component> { #( - let mut comp_~I: Option<RefMut<Box<dyn Component>>> = None; + let mut comp_~I: Option<WriteGuard<Box<dyn Component>>> = None; )* for comp in components { - let Ok(comp_ref) = comp.try_borrow_mut() else { - continue; - }; + let comp_ref = comp + .write_nonblock() + .expect("Failed to acquire read-write component lock"); #( if comp_ref.is::<Comp~I>() { @@ -106,10 +117,7 @@ macro_rules! inner { (#( ComponentRefMut::new( - RefMut::filter_map( - comp_~I.unwrap(), - |component| component.downcast_mut::<Comp~I>() - ).expect("Failed to downcast component") + comp_~I.unwrap(), ), )*) } |