summaryrefslogtreecommitdiff
path: root/ecs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-01-02 17:02:25 +0100
committerHampusM <hampus@hampusmat.com>2025-01-02 17:02:25 +0100
commit85ed89df486858984f0936086205efc23fd32d24 (patch)
treef0480c745b3980e0767ed79177d87188f8129514 /ecs
parent1f6685aa570eadc70342bd1c62f5f9ff451afcb2 (diff)
refactor(ecs): make component::Sequence return metadata as array
Diffstat (limited to 'ecs')
-rw-r--r--ecs/src/actions.rs6
-rw-r--r--ecs/src/component.rs18
-rw-r--r--ecs/src/lib.rs2
-rw-r--r--ecs/src/util.rs18
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<Box<dyn Component>>;
- fn metadata() -> Vec<Metadata>;
+ fn metadata() -> impl Array<Metadata>;
fn added_event_ids() -> Vec<Uid>;
@@ -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<Box<dyn Component>>
{
Vec::from_iter([#(Box::new(self.I) as Box<dyn Component>,)*])
}
- fn metadata() -> Vec<Metadata>
+ fn metadata() -> impl Array<Metadata>
{
- 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<Box<dyn Component>>
{
Vec::new()
}
- fn metadata() -> Vec<Metadata>
+ fn metadata() -> impl Array<Metadata>
{
- Vec::new()
+ []
}
fn added_event_ids() -> Vec<Uid>
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<Item>:
+ AsRef<[Item]>
+ + AsMut<[Item]>
+ + IntoIterator<Item = Item>
+ + Sortable<Item = Item>
+ + sealed::Sealed
+{
+}
+
+impl<Item, const CNT: usize> Array<Item> for [Item; CNT] {}
+
+impl<Item, const CNT: usize> 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
{