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.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'ecs/src/system.rs') diff --git a/ecs/src/system.rs b/ecs/src/system.rs index 76db2b5..3440a57 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -1,13 +1,15 @@ use std::any::Any; -use std::cell::RefMut; use std::convert::Infallible; use std::fmt::Debug; +use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; +use std::panic::{RefUnwindSafe, UnwindSafe}; use std::ptr::addr_of; use seq_macro::seq; use crate::component::Component; +use crate::lock::WriteGuard; use crate::system::util::check_params_are_compatible; use crate::tuple::{FilterElement as TupleFilterElement, With as TupleWith}; use crate::WorldData; @@ -45,7 +47,7 @@ macro_rules! impl_system { impl<'world, Func, #(TParam~I,)*> System<'world, fn(#(TParam~I,)*)> for Func where - Func: Fn(#(TParam~I,)*) + Copy + 'static, + Func: Fn(#(TParam~I,)*) + Copy + RefUnwindSafe + UnwindSafe + 'static, #(TParam~I: Param<'world, Flags = NoInitParamFlag>,)* { type Input = Infallible; @@ -125,7 +127,7 @@ pub trait Into pub struct TypeErased { - data: Box, + data: Box, func: Box, } @@ -153,7 +155,7 @@ impl Debug for TypeErased } /// Function in [`TypeErased`] used to run the system. -type TypeErasedFunc = dyn Fn(&dyn Any, &WorldData); +type TypeErasedFunc = dyn Fn(&dyn Any, &WorldData) + RefUnwindSafe + UnwindSafe; /// A parameter to a [`System`]. /// @@ -194,14 +196,15 @@ where #[derive(Debug)] pub struct ComponentRefMut<'a, ComponentT: Component> { - inner: RefMut<'a, ComponentT>, + inner: WriteGuard<'a, Box>, + _ph: PhantomData, } impl<'a, ComponentT: Component> ComponentRefMut<'a, ComponentT> { - pub(crate) fn new(inner: RefMut<'a, ComponentT>) -> Self + pub(crate) fn new(inner: WriteGuard<'a, Box>) -> Self { - Self { inner } + Self { inner, _ph: PhantomData } } } @@ -211,7 +214,7 @@ impl<'a, ComponentT: Component> Deref for ComponentRefMut<'a, ComponentT> fn deref(&self) -> &Self::Target { - &self.inner + self.inner.downcast_ref().unwrap() } } @@ -219,6 +222,6 @@ impl<'a, ComponentT: Component> DerefMut for ComponentRefMut<'a, ComponentT> { fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner + self.inner.downcast_mut().unwrap() } } -- cgit v1.2.3-18-g5258