diff options
author | HampusM <hampus@hampusmat.com> | 2025-03-18 15:13:35 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-03-19 17:59:39 +0100 |
commit | 44cd47fd67102902b649c98b85c5abb9a0da39f8 (patch) | |
tree | 05f5cf37f9f41e0f08a0fba9b23b0f5ec065ce0b | |
parent | c1cf1b779e66e985774dad29867a57733947b0e8 (diff) |
refactor(ecs): add component::Into
-rw-r--r-- | ecs/src/component.rs | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index dc60995..9257c41 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -267,10 +267,16 @@ pub trait FromLockedOptional<'comp>: Sized macro_rules! inner { ($c: tt) => { seq!(I in 0..=$c { - impl<#(Comp~I: Component,)*> Sequence for (#(Comp~I,)*) + impl<#(IntoComp~I,)*> Sequence for (#(IntoComp~I,)*) where - #(for<'comp> Comp~I::RefMut<'comp>: FromOptionalMut<'comp>,)* - #(for<'comp> Comp~I::Ref<'comp>: FromOptional<'comp>,)* + #( + for<'comp> IntoComp~I: Into< + Component: Component< + RefMut<'comp>: FromOptionalMut<'comp>, + Ref<'comp>: FromOptional<'comp> + > + >, + )* { const COUNT: usize = $c + 1; @@ -278,7 +284,11 @@ macro_rules! inner { fn into_array(self) -> Self::Array { - [#((Comp~I::id(), Box::new(self.I) as Box<dyn Component>),)*] + [#({ + let (id, component) = self.I.into_component(); + + (id, Box::new(component)) + },)*] } fn metadata() -> impl Array<Metadata> @@ -286,8 +296,8 @@ macro_rules! inner { [ #( Metadata { - id: Comp~I::id(), - is_optional: Comp~I::is_optional(), + id: IntoComp~I::Component::id(), + is_optional: IntoComp~I::Component::is_optional(), }, )* ] @@ -296,14 +306,14 @@ macro_rules! inner { fn added_event_ids() -> Vec<Uid> { vec![ - #(ComponentAddedEvent::<Comp~I>::id(),)* + #(ComponentAddedEvent::<IntoComp~I::Component>::id(),)* ] } fn removed_event_ids() -> Vec<Uid> { vec![ - #(ComponentRemovedEvent::<Comp~I>::id(),)* + #(ComponentRemovedEvent::<IntoComp~I::Component>::id(),)* ] } } @@ -398,3 +408,22 @@ impl RefSequence for () { } } + +pub trait Into +{ + type Component; + + fn into_component(self) -> (Uid, Self::Component); +} + +impl<ComponentT> Into for ComponentT +where + ComponentT: Component, +{ + type Component = Self; + + fn into_component(self) -> (Uid, Self::Component) + { + (Self::id(), self) + } +} |