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.rs | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'ecs/src/system.rs') diff --git a/ecs/src/system.rs b/ecs/src/system.rs index ff3437a..5d3e0bf 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -4,6 +4,7 @@ use ecs_macros::Component; use seq_macro::seq; use crate::World; +use crate::error::Error; use crate::uid::Uid; pub mod initializable; @@ -28,10 +29,11 @@ pub trait System<'world, Impl>: 'static macro_rules! impl_system { ($c: tt) => { seq!(I in 0..$c { - impl<'world, Func, #(TParam~I,)*> System<'world, fn(#(TParam~I,)*)> + impl<'world, Func, Ret, #(TParam~I,)*> System<'world, fn(#(TParam~I,)*) -> Ret> for Func where - Func: Fn(#(TParam~I,)*) + Copy + 'static, + Func: Fn(#(TParam~I,)*) -> Ret + 'static, + Ret: ReturnValue, #(TParam~I: Param<'world, Input = ()>,)* { type Callbacks = NoCallbacks; @@ -48,8 +50,9 @@ macro_rules! impl_system { self(#({ TParam~I::new(world, &metadata) - },)*); + },)*).into_result() }), + name: std::any::type_name::() }; (type_erased, NoCallbacks) @@ -73,6 +76,7 @@ pub trait Into<'world, Impl> pub struct TypeErased { run: Box, + name: &'static str, } impl TypeErased @@ -81,9 +85,14 @@ impl TypeErased /// /// # Safety /// `world_data` must live at least as long as the [`World`] the system belongs to. - pub unsafe fn run(&self, world: &World, metadata: Metadata) + pub unsafe fn run(&self, world: &World, metadata: Metadata) -> Result<(), Error> { - (self.run)(world, metadata); + (self.run)(world, metadata) + } + + pub fn name(&self) -> &'static str + { + self.name } } @@ -95,8 +104,26 @@ impl Debug for TypeErased } } -/// Function in [`TypeErased`] used to run the system. -type TypeErasedRunFn = dyn Fn(&World, Metadata); +pub trait ReturnValue +{ + fn into_result(self) -> Result<(), Error>; +} + +impl ReturnValue for () +{ + fn into_result(self) -> Result<(), Error> + { + Ok(()) + } +} + +impl ReturnValue for Result<(), Error> +{ + fn into_result(self) -> Result<(), Error> + { + self + } +} /// A parameter to a [`System`]. pub trait Param<'world> @@ -126,3 +153,6 @@ pub(crate) struct SystemComponent { pub(crate) system: TypeErased, } + +/// Function in [`TypeErased`] used to run the system. +type TypeErasedRunFn = dyn Fn(&World, Metadata) -> Result<(), Error>; -- cgit v1.2.3-18-g5258