From 61dfcf1ba2049bf0375821652e49b0e4c4147623 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 29 Mar 2024 14:20:21 +0100 Subject: feat(ecs): make World unwind safe --- ecs/src/component.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'ecs/src/component.rs') 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 +{ + 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; - fn from_components(components: &[RefCell>]) -> Self::Refs<'_>; + fn from_components<'component>( + components: impl Iterator>>, + ) -> Self::Refs<'component>; } macro_rules! inner { @@ -83,18 +94,18 @@ macro_rules! inner { ] } - fn from_components( - components: &[RefCell>] - ) -> Self::Refs<'_> + fn from_components<'component>( + components: impl Iterator>>, + ) -> Self::Refs<'component> { #( - let mut comp_~I: Option>> = None; + let mut comp_~I: Option>> = 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::() { @@ -106,10 +117,7 @@ macro_rules! inner { (#( ComponentRefMut::new( - RefMut::filter_map( - comp_~I.unwrap(), - |component| component.downcast_mut::() - ).expect("Failed to downcast component") + comp_~I.unwrap(), ), )*) } -- cgit v1.2.3-18-g5258