From d5a744b0909c4b2bec397ae4dcd43b56aba355c6 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 19 May 2026 00:12:11 +0200 Subject: feat(ecs): add error handling --- ecs/src/system/stateful.rs | 75 +++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 28 deletions(-) (limited to 'ecs/src/system/stateful.rs') diff --git a/ecs/src/system/stateful.rs b/ecs/src/system/stateful.rs index e74ef31..3e0076a 100644 --- a/ecs/src/system/stateful.rs +++ b/ecs/src/system/stateful.rs @@ -1,10 +1,11 @@ +use std::any::type_name; use std::mem::transmute; -use std::panic::{RefUnwindSafe, UnwindSafe}; use seq_macro::seq; -use crate::component::local::SystemWithLocalComponents; +use crate::World; use crate::component::Parts as ComponentParts; +use crate::component::local::SystemWithLocalComponents; use crate::event::Emitted as EmittedEvent; use crate::system::initializable::{Initializable, MaybeInitializableParamTuple}; use crate::system::observer::{ @@ -13,8 +14,14 @@ use crate::system::observer::{ Observer, WrapperComponent as ObserverWrapperComponent, }; -use crate::system::{Into as IntoSystem, Metadata, Param, System, TypeErased}; -use crate::World; +use crate::system::{ + Into as IntoSystem, + Metadata, + Param, + ReturnValue, + System, + TypeErased, +}; /// A stateful system. pub struct Stateful @@ -26,10 +33,11 @@ pub struct Stateful macro_rules! impl_system { ($c: tt) => { seq!(I in 0..$c { - impl<'world, Func, #(TParam~I,)*> - System<'world, fn(&'world (), #(TParam~I,)*)> for Stateful + impl<'world, Func, Ret, #(TParam~I,)*> + System<'world, fn(#(TParam~I,)*) -> Ret> for Stateful where - Func: Fn(#(TParam~I,)*) + Copy + RefUnwindSafe + UnwindSafe + 'static, + Func: Fn(#(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world, Input: 'static>,)* { type Callbacks = Callbacks; @@ -49,7 +57,10 @@ macro_rules! impl_system { func(#({ TParam~I::new(&world, &metadata) },)*); + + Ok(()) }), + name: type_name::() }; @@ -57,10 +68,11 @@ macro_rules! impl_system { } } - impl<'world, Func, #(TParam~I,)*> - Initializable<'world, fn(&'world (), #(TParam~I,)*)> for Stateful + impl<'world, Func, Ret, #(TParam~I,)*> + Initializable<'world, fn(#(TParam~I,)*) -> Ret> for Stateful where - Func: Fn(#(TParam~I,)*) + Copy + RefUnwindSafe + UnwindSafe + 'static, + Func: Fn(#(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world, Input: 'static>,)* (#(TParam~I,)*): MaybeInitializableParamTuple<'world, Self> { @@ -76,11 +88,12 @@ macro_rules! impl_system { } } - impl<'world, Func, #(TParam~I,)*> IntoSystem<'world, fn(#(TParam~I,)*)> - for Func + impl<'world, Func, Ret, #(TParam~I,)*> + IntoSystem<'world, fn(#(TParam~I,)*) -> Ret> for Func where + Func: Fn(#(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world>,)* - Func: Fn(#(TParam~I,)*) + Copy + 'static, { type System = Stateful; @@ -136,13 +149,14 @@ fn init_initializable_params<'world, SystemT, Params>( macro_rules! impl_observer { ($c: tt) => { seq!(I in 0..$c { - impl<'world, ObservedT, Func, #(TParam~I,)*> System< + impl<'world, ObservedT, Func, Ret, #(TParam~I,)*> System< 'world, - fn(Observe<'world, ObservedT>, #(TParam~I,)*) + fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret > for Stateful where ObservedT: Observed, - Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static, + Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world>,)* { type Callbacks = Callbacks; @@ -153,13 +167,14 @@ macro_rules! impl_observer { } } - impl<'world, ObservedT, Func, #(TParam~I,)*> Initializable< + impl<'world, ObservedT, Func, Ret, #(TParam~I,)*> Initializable< 'world, - fn(Observe<'world, ObservedT>, #(TParam~I,)*) + fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret > for Stateful where ObservedT: Observed, - Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static, + Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world>,)* (#(TParam~I,)*): MaybeInitializableParamTuple<'world, Self> { @@ -175,13 +190,14 @@ macro_rules! impl_observer { } } - impl<'world, ObservedT, Func, #(TParam~I,)*> Observer< + impl<'world, ObservedT, Func, Ret, #(TParam~I,)*> Observer< 'world, - fn(Observe<'world, ObservedT>, #(TParam~I,)*) + fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret > for Stateful where ObservedT: Observed, - Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static, + Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world>,)* { type ObservedEvents = ObservedT::Events; @@ -193,6 +209,8 @@ macro_rules! impl_observer { fn finish_observer(self) -> (ObserverWrapperComponent, Callbacks) { + #![allow(unused)] + let Self { func, local_components } = self; let callbacks = Callbacks { local_components }; @@ -213,23 +231,24 @@ macro_rules! impl_observer { func(Observe::new(world, emitted_event), #({ TParam~I::new(world, &metadata) - },)*); + },)*).into_result() }, + type_name::() ); (wrapper_comp, callbacks) } } - impl<'world, Func, ObservedT, #(TParam~I,)*> IntoSystem< + impl<'world, Func, Ret, ObservedT, #(TParam~I,)*> IntoSystem< 'world, - fn(Observe<'world, ObservedT>, - #(TParam~I,)*) + fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret > for Func where ObservedT: Observed, + Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world>,)* - Func: Fn(Observe<'world, ObservedT>, #(TParam~I,)*) + Copy + 'static, { type System = Stateful; @@ -245,6 +264,6 @@ macro_rules! impl_observer { }; } -seq!(C in 1..16 { +seq!(C in 0..16 { impl_observer!(C); }); -- cgit v1.2.3-18-g5258