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/system/stateful.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'ecs/src/system/stateful.rs') diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs index 7ce87fa..8d56a13 100644 --- a/ecs/src/system/stateful.rs +++ b/ecs/src/system/stateful.rs @@ -1,10 +1,11 @@ use std::any::{type_name, Any, TypeId}; -use std::cell::{RefCell, RefMut}; use std::collections::HashMap; +use std::panic::{RefUnwindSafe, UnwindSafe}; use seq_macro::seq; use crate::component::Component; +use crate::lock::Lock; use crate::system::util::check_params_are_compatible; use crate::system::{ComponentRefMut, Into as IntoSystem, Param, System, TypeErased}; use crate::tuple::{ @@ -20,7 +21,7 @@ use crate::WorldData; pub struct Stateful { func: Func, - local_components: HashMap>>, + local_components: HashMap>>, } macro_rules! impl_system { @@ -29,7 +30,7 @@ macro_rules! impl_system { impl<'world, Func, #(TParam~I,)*> System<'world, fn(&'world (), #(TParam~I,)*)> for Stateful where - Func: Fn(#(TParam~I,)*) + Copy + 'static, + Func: Fn(#(TParam~I,)*) + Copy + RefUnwindSafe + UnwindSafe + 'static, #(TParam~I: Param<'world>,)* #(TParam~I::Input: 'static,)* (#(TParam~I::Input,)*): TupleFilter, @@ -119,12 +120,10 @@ macro_rules! impl_system { { let local_component = self.local_components .get(&TypeId::of::())? - .borrow_mut(); + .write_nonblock() + .expect("Failed to aquire read-write local component lock"); - Some(ComponentRefMut::new(RefMut::filter_map( - local_component, - |local_component| local_component.downcast_mut() - ).ok()?)) + Some(ComponentRefMut::new(local_component)) } fn set_local_component( @@ -135,7 +134,7 @@ macro_rules! impl_system { self.local_components .insert( TypeId::of::(), - RefCell::new(Box::new(local_component)) + Lock::new(Box::new(local_component)) ); } } -- cgit v1.2.3-18-g5258