summaryrefslogtreecommitdiff
path: root/ecs/src/system.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-05-19 00:12:11 +0200
committerHampusM <hampus@hampusmat.com>2026-05-19 00:12:11 +0200
commitd5a744b0909c4b2bec397ae4dcd43b56aba355c6 (patch)
tree1dcf8b769775ab7a0f4ff87e280aa98a0d280d21 /ecs/src/system.rs
parentb13b3f6e13f9ac9fe7fee0b5a81b026f411f0301 (diff)
feat(ecs): add error handlingHEADmaster
Diffstat (limited to 'ecs/src/system.rs')
-rw-r--r--ecs/src/system.rs44
1 files changed, 37 insertions, 7 deletions
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::<Self>()
};
(type_erased, NoCallbacks)
@@ -73,6 +76,7 @@ pub trait Into<'world, Impl>
pub struct TypeErased
{
run: Box<TypeErasedRunFn>,
+ 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>;