diff options
Diffstat (limited to 'ecs/src/uid.rs')
| -rw-r--r-- | ecs/src/uid.rs | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/ecs/src/uid.rs b/ecs/src/uid.rs index 6c97649..bb393a1 100644 --- a/ecs/src/uid.rs +++ b/ecs/src/uid.rs @@ -2,8 +2,10 @@ use std::fmt::{Debug, Display, Formatter}; use std::mem::transmute; use std::sync::atomic::{AtomicU32, Ordering}; +use seq_macro::seq; + use crate::component::Component; -use crate::util::{gen_mask_64, BitMask, NumberExt}; +use crate::util::{gen_mask_64, Array, BitMask, NumberExt}; static NEXT: AtomicU32 = AtomicU32::new(Uid::FIRST_UNIQUE_ID); @@ -54,8 +56,12 @@ impl Uid } } + /// Returns a new pair UID. + /// + /// # Panics + /// Will panic if either the given relation or target is a pair UID. #[must_use] - pub fn new_pair(params: PairParams) -> Self + pub fn new_pair(params: &PairParams) -> Self { assert_ne!( params.relation.kind(), @@ -102,6 +108,7 @@ impl Uid /// /// # Panics /// Will panic if this `Uid` is not a pair. + #[must_use] pub fn relation_component(&self) -> Self { assert_eq!(self.kind(), Kind::Pair, "Uid is not a pair"); @@ -112,6 +119,7 @@ impl Uid } } + #[must_use] pub fn has_same_relation_as(&self, other: Self) -> bool { self.relation() == other.relation() @@ -121,6 +129,7 @@ impl Uid /// /// # Panics /// Will panic if this `Uid` is not a pair. + #[must_use] pub fn relation_entity(&self) -> Self { assert_eq!(self.kind(), Kind::Pair, "Uid is not a pair"); @@ -135,6 +144,7 @@ impl Uid /// /// # Panics /// Will panic if this `Uid` is not a pair. + #[must_use] pub fn target_component(&self) -> Self { assert_eq!(self.kind(), Kind::Pair, "Uid is not a pair"); @@ -149,6 +159,7 @@ impl Uid /// /// # Panics /// Will panic if this `Uid` is not a pair. + #[must_use] pub fn target_entity(&self) -> Self { assert_eq!(self.kind(), Kind::Pair, "Uid is not a pair"); @@ -159,7 +170,7 @@ impl Uid } } - fn relation(&self) -> u32 + fn relation(self) -> u32 { let Ok(relation) = u32::try_from(self.inner.field_get(RELATION_BITS)) else { unreachable!("Uid relation does not fit in u32"); @@ -209,7 +220,7 @@ pub struct PairParams pub target: Uid, } -pub trait With: 'static +pub trait With { fn uid() -> Uid; } @@ -222,13 +233,29 @@ impl<ComponentT: Component> With for ComponentT } } -#[derive(Debug)] -pub enum Wildcard {} - -impl With for Wildcard +pub trait WithUidTuple { - fn uid() -> Uid - { - Uid::wildcard() - } + type UidsArray: Array<Uid>; + + fn uids() -> Self::UidsArray; +} + +macro_rules! impl_with_uid_tuple { + ($c: tt) => { + seq!(I in 0..=$c { + impl<#(WithUid~I: With,)*> WithUidTuple for (#(WithUid~I,)*) + { + type UidsArray = [Uid; $c + 1]; + + fn uids() -> Self::UidsArray + { + [#(WithUid~I::uid(),)*] + } + } + }); + }; } + +seq!(C in 0..=16 { + impl_with_uid_tuple!(C); +}); |
