From 85ed89df486858984f0936086205efc23fd32d24 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Jan 2025 17:02:25 +0100 Subject: refactor(ecs): make component::Sequence return metadata as array --- ecs/src/actions.rs | 6 +++--- ecs/src/component.rs | 18 +++++++++++++----- ecs/src/lib.rs | 2 +- ecs/src/util.rs | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index c062472..3988a77 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -44,7 +44,7 @@ impl<'world> Actions<'world> { debug_assert_eq!(entity_uid.kind(), UidKind::Entity); - if Comps::metadata().len() == 0 { + if Comps::COUNT == 0 { return; } @@ -62,13 +62,13 @@ impl<'world> Actions<'world> { debug_assert_eq!(entity_uid.kind(), UidKind::Entity); - if Comps::metadata().len() == 0 { + if Comps::COUNT == 0 { return; } self.action_queue.push(Action::RemoveComponents( entity_uid, - Comps::metadata(), + Comps::metadata().into_iter().collect(), EventIds { ids: Comps::removed_event_ids() }, )); } diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 1fde5b4..bf19c9e 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -12,6 +12,7 @@ use crate::lock::{ReadGuard, WriteGuard}; use crate::system::{ComponentRef, ComponentRefMut, Input as SystemInput}; use crate::type_name::TypeName; use crate::uid::Uid; +use crate::util::Array; use crate::{EntityComponent, World}; pub mod local; @@ -170,9 +171,12 @@ pub trait Sequence where Self: 'component; + /// The number of components in this component sequence. + const COUNT: usize; + fn into_vec(self) -> Vec>; - fn metadata() -> Vec; + fn metadata() -> impl Array; fn added_event_ids() -> Vec; @@ -271,14 +275,16 @@ macro_rules! inner { type Refs<'component> = (#(Comp~I::Ref<'component>,)*) where Self: 'component; + const COUNT: usize = $c + 1; + fn into_vec(self) -> Vec> { Vec::from_iter([#(Box::new(self.I) as Box,)*]) } - fn metadata() -> Vec + fn metadata() -> impl Array { - vec![ + [ #( Metadata { id: Comp~I::id(), @@ -357,14 +363,16 @@ impl Sequence for () type MutRefs<'component> = (); type Refs<'component> = (); + const COUNT: usize = 0; + fn into_vec(self) -> Vec> { Vec::new() } - fn metadata() -> Vec + fn metadata() -> impl Array { - Vec::new() + [] } fn added_event_ids() -> Vec diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 43a00f1..ae5fd19 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -45,12 +45,12 @@ pub mod system; pub mod tuple; pub mod type_name; pub mod uid; +pub mod util; #[doc(hidden)] pub mod private; mod archetype; -mod util; pub use ecs_macros::{Component, Sole}; diff --git a/ecs/src/util.rs b/ecs/src/util.rs index 0344e89..4ba8597 100644 --- a/ecs/src/util.rs +++ b/ecs/src/util.rs @@ -1,5 +1,18 @@ use std::ops::BitAnd; +pub trait Array: + AsRef<[Item]> + + AsMut<[Item]> + + IntoIterator + + Sortable + + sealed::Sealed +{ +} + +impl Array for [Item; CNT] {} + +impl sealed::Sealed for [Item; CNT] {} + pub trait Sortable { type Item; @@ -115,6 +128,11 @@ macro_rules! gen_mask_64 { pub(crate) use gen_mask_64; +mod sealed +{ + pub trait Sealed {} +} + #[cfg(test)] mod tests { -- cgit v1.2.3-18-g5258