From 624e6dd450944e67d84ad7afda54ae2abd277aba Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 23 Jul 2026 17:38:51 +0200 Subject: refactor(engine-ecs): remove distinction between query terms with & without a field --- engine-ecs/src/pair.rs | 103 +++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 41 deletions(-) (limited to 'engine-ecs/src/pair.rs') diff --git a/engine-ecs/src/pair.rs b/engine-ecs/src/pair.rs index 3668f1b..1168f67 100644 --- a/engine-ecs/src/pair.rs +++ b/engine-ecs/src/pair.rs @@ -14,10 +14,11 @@ use crate::entity::{ MatchingComponentIter as EntityMatchingComponentIter, }; use crate::query::{ - TermWithField as QueryTermWithField, + Term as QueryTerm, TermsBuilder as QueryTermsBuilder, TermsBuilderInterface, }; +use crate::tuple::Tuple; use crate::uid::{PairParams as UidPairParams, Uid, With as WithUid}; use crate::util::impl_multiple; use crate::{Component, EntityComponentRef, World}; @@ -191,12 +192,13 @@ where } } -impl QueryTermWithField for Pair +impl<'world, Relation, Target> QueryTerm<'world> for Pair where Relation: Component, Target: Component, { - type Field<'a> = ComponentHandle<'a, Target>; + type AddField = + Fields::WithElementAtEnd>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, @@ -205,31 +207,38 @@ where terms_builder.present([Pair::::uid()]); } - fn get_field<'world>( + fn add_field( entity_handle: &EntityHandle<'world>, _world: &'world World, - ) -> Self::Field<'world> + fields: Fields, + ) -> Self::AddField { let target_component = entity_handle .get_matching_components(Pair::::uid()) .next() .expect("Not possible"); - Self::Field::from_entity_component_ref(&target_component).unwrap_or_else(|err| { - panic!( - "Creating handle to target component {} failed: {err}", - type_name::() - ); - }) + let target_component = match ComponentHandle::::from_entity_component_ref(&target_component) { + Ok(target_component) => target_component, + Err(err) => { + panic!( + "Creating handle to target component {} failed: {err}", + type_name::() + ); + } + }; + + fields.with_elem(target_component) } } -impl QueryTermWithField for Pair +impl<'world, Relation, Target> QueryTerm<'world> for Pair where Relation: Component, Target: Component, { - type Field<'a> = ComponentHandleMut<'a, Target>; + type AddField = + Fields::WithElementAtEnd>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, @@ -238,35 +247,40 @@ where terms_builder.present([Pair::::uid()]); } - fn get_field<'world>( + fn add_field( entity_handle: &EntityHandle<'world>, world: &'world World, - ) -> Self::Field<'world> + fields: Fields, + ) -> Self::AddField { let target_component = entity_handle .get_matching_components(Pair::::uid()) .next() .expect("Not possible"); - Self::Field::from_entity_component_ref(&target_component, world).unwrap_or_else( - |err| { + let target_component = match ComponentHandleMut::::from_entity_component_ref(&target_component, world) { + Ok(target_component) => target_component, + Err(err) => { panic!( - "Creating handle to target component {} failed: {err}", + "Creating mut handle to target component {} failed: {err}", type_name::() ); - }, - ) + } + }; + + fields.with_elem(target_component) } } -// TODO: implement QueryTermWithField for Pair<&Relation, Target> (or equivalent) -// TODO: implement QueryTermWithField for Pair<&mut Relation, Target> (or equivalent) +// TODO: implement QueryTerm for Pair<&Relation, Target> (or equivalent) +// TODO: implement QueryTerm for Pair<&mut Relation, Target> (or equivalent) -impl QueryTermWithField for Pair +impl<'world, Relation> QueryTerm<'world> for Pair where Relation: Component, { - type Field<'a> = WithWildcard<'a, Relation, Wildcard>; + type AddField = + Fields::WithElementAtEnd>; fn apply_to_terms_builder( terms_builder: &mut QueryTermsBuilder, @@ -275,21 +289,24 @@ where terms_builder.present([Self::uid()]); } - fn get_field<'world>( + fn add_field( entity_handle: &EntityHandle<'world>, world: &'world World, - ) -> Self::Field<'world> + fields: Fields, + ) -> Self::AddField { let first_matching_comp = entity_handle .get_matching_components(Self::uid()) .next() .expect("Not possible"); - WithWildcard { - world, - component_ref: first_matching_comp, - _pd: PhantomData, - } + fields.with_elem( + WithWildcard { + world, + component_ref: first_matching_comp, + _pd: PhantomData, + } + ) } } @@ -320,11 +337,12 @@ where } } -impl QueryTermWithField for &'_ [Pair] +impl<'world, Relation> QueryTerm<'world> for &[Pair] where Relation: Component, { - type Field<'a> = MultipleWithWildcard<'a, Relation, Wildcard>; + type AddField = + Fields::WithElementAtEnd>; fn apply_to_terms_builder( _terms_builder: &mut QueryTermsBuilder, @@ -332,16 +350,19 @@ where { } - fn get_field<'world>( + fn add_field( entity_handle: &EntityHandle<'world>, world: &'world World, - ) -> Self::Field<'world> - { - MultipleWithWildcard { - entity_handle: entity_handle.clone(), - world, - _pd: PhantomData, - } + fields: Fields, + ) -> Self::AddField + { + fields.with_elem( + MultipleWithWildcard { + entity_handle: entity_handle.clone(), + world, + _pd: PhantomData, + } + ) } } -- cgit v1.2.3-18-g5258