From f8e9fc157a205451607ddff6975027b3573c3934 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 21 Jul 2026 16:17:12 +0200 Subject: feat(engine-ecs): automatically include entity name component in declared entities --- engine-ecs/src/entity.rs | 9 ++++++++- engine-ecs/src/tuple.rs | 24 +++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) (limited to 'engine-ecs/src') diff --git a/engine-ecs/src/entity.rs b/engine-ecs/src/entity.rs index 4e0ca52..ba85956 100644 --- a/engine-ecs/src/entity.rs +++ b/engine-ecs/src/entity.rs @@ -322,7 +322,14 @@ macro_rules! declare_entity { $(#[$attr])* $visibility static $ident: $crate::entity::Declaration = $crate::entity::Declaration::new(|world| { - world.spawn_with_uid(*$ident, $components); + use $crate::tuple::Tuple; + + world.spawn_with_uid( + *$ident, + ($components).with_elem($crate::entity::Name { + name: concat!(module_path!(), stringify!($ident)).into() + }) + ); }); )+ } diff --git a/engine-ecs/src/tuple.rs b/engine-ecs/src/tuple.rs index def25a0..15e1719 100644 --- a/engine-ecs/src/tuple.rs +++ b/engine-ecs/src/tuple.rs @@ -1,4 +1,4 @@ -use std::any::TypeId; +use std::{any::TypeId, convert::Infallible}; use paste::paste; use seq_macro::seq; @@ -11,9 +11,9 @@ pub trait Tuple: sealed::Sealed /// `(String, i32, u8)::WithElementAtEnd = (String, i32, u8, Path)` /// /// # Important note - /// If `Self` has 16 elements, this will be `()`. The reason for this is that the - /// `Tuple` trait is only implemented for tuples with up to and including 16 elements. - type WithElementAtEnd: Tuple; + /// If `Self` has 16 elements, this will be [`Infallible`]. The reason for this is + /// that the `Tuple` trait is only implemented for tuples with 0-16 elements. + type WithElementAtEnd; /// `Self` without the last element. /// @@ -26,6 +26,8 @@ pub trait Tuple: sealed::Sealed /// Self with all elements wrapped in [`Option`]. type InOptions: Tuple; + fn with_elem(self, new_elem: NewElem) -> Self::WithElementAtEnd; + /// Pops the last element from this tuple, returning the new tuple and the popped /// element. fn pop_last(self) -> (Self::WithoutLastElement, Self::LastElement); @@ -126,6 +128,11 @@ macro_rules! impl_tuple_traits { type InOptions = (#(Option,)*); + fn with_elem(self, new_elem: NewElem) -> Self::WithElementAtEnd + { + (#(self.I,)* new_elem,) + } + fn pop_last(self) -> (Self::WithoutLastElement, Self::LastElement) { ( @@ -186,7 +193,7 @@ seq!(N in 0..16 { seq!(I in 0..16 { impl<#(Elem~I,)*> Tuple for (#(Elem~I,)*) { - type WithElementAtEnd = (); + type WithElementAtEnd = Infallible; type WithoutLastElement = all_except_last!(start #(Elem~I)*); @@ -194,6 +201,13 @@ seq!(I in 0..16 { type InOptions = (#(Option,)*); + fn with_elem(self, _: NewElem) -> Self::WithElementAtEnd + { + const { + panic!("Cannot add element to tuple with 16 elements"); + }; + } + fn pop_last(self) -> (Self::WithoutLastElement, Self::LastElement) { ( -- cgit v1.2.3-18-g5258