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/query/term.rs | 121 ++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 43 deletions(-) (limited to 'engine-ecs/src/query/term.rs') diff --git a/engine-ecs/src/query/term.rs b/engine-ecs/src/query/term.rs index 7c4503a..04bca1d 100644 --- a/engine-ecs/src/query/term.rs +++ b/engine-ecs/src/query/term.rs @@ -1,17 +1,19 @@ use std::any::type_name; use std::marker::PhantomData; +use crate::World; use crate::component::{ Component, Handle as ComponentHandle, HandleMut as ComponentHandleMut, }; +use crate::entity::Handle as EntityHandle; use crate::query::{ - TermWithField, - TermWithoutField, + Term, TermsBuilder, TermsBuilderInterface, }; +use crate::tuple::Tuple; use crate::uid::With as WithUid; pub struct With @@ -21,16 +23,27 @@ where _pd: PhantomData, } -impl TermWithoutField for With +impl<'world, WithUidT> Term<'world> for With where WithUidT: WithUid, { + type AddField = Fields; + fn apply_to_terms_builder( terms_builder: &mut TermsBuilder, ) { terms_builder.present([WithUidT::uid()]); } + + fn add_field( + _entity_handle: &EntityHandle<'world>, + _world: &'world World, + fields: Fields, + ) -> Self::AddField + { + fields + } } pub struct Without @@ -40,21 +53,33 @@ where _pd: PhantomData, } -impl TermWithoutField for Without +impl<'world, WithUidT> Term<'world> for Without where WithUidT: WithUid, { + type AddField = Fields; + fn apply_to_terms_builder( terms_builder: &mut TermsBuilder, ) { terms_builder.absent([WithUidT::uid()]); } + + fn add_field( + _entity_handle: &EntityHandle<'world>, + _world: &'world World, + fields: Fields, + ) -> Self::AddField + { + fields + } } -impl TermWithField for Option<&ComponentT> +impl<'world, ComponentT: Component> Term<'world> for Option<&ComponentT> { - type Field<'a> = Option>; + type AddField = + Fields::WithElementAtEnd>>; fn apply_to_terms_builder( _terms_builder: &mut TermsBuilder, @@ -62,30 +87,36 @@ impl TermWithField for Option<&ComponentT> { } - fn get_field<'world>( - entity_handle: &crate::entity::Handle<'world>, - _world: &'world crate::World, - ) -> Self::Field<'world> + fn add_field( + entity_handle: &EntityHandle<'world>, + _world: &'world World, + fields: Fields, + ) -> Self::AddField { - Some( - ComponentHandle::<'world, ComponentT>::from_entity_component_ref( - &entity_handle - .get_matching_components(ComponentT::id()) - .next()?, - ) - .unwrap_or_else(|err| { - panic!( - "Creating handle to component {} failed: {err}", - type_name::() - ); - }), - ) + let component = (|| { + let comp_ref = &entity_handle + .get_matching_components(ComponentT::id()) + .next()?; + + match ComponentHandle::::from_entity_component_ref(comp_ref) { + Ok(component) => Some(component), + Err(err) => { + panic!( + "Creating handle to component {} failed: {err}", + type_name::() + ); + } + } + })(); + + fields.with_elem(component) } } -impl TermWithField for Option<&mut ComponentT> +impl<'world, ComponentT: Component> Term<'world> for Option<&mut ComponentT> { - type Field<'a> = Option>; + type AddField = + Fields::WithElementAtEnd>>; fn apply_to_terms_builder( _terms_builder: &mut TermsBuilder, @@ -93,24 +124,28 @@ impl TermWithField for Option<&mut ComponentT> { } - fn get_field<'world>( - entity_handle: &crate::entity::Handle<'world>, - world: &'world crate::World, - ) -> Self::Field<'world> + fn add_field( + entity_handle: &EntityHandle<'world>, + world: &'world World, + fields: Fields, + ) -> Self::AddField { - Some( - ComponentHandleMut::<'world, ComponentT>::from_entity_component_ref( - &entity_handle - .get_matching_components(ComponentT::id()) - .next()?, - world, - ) - .unwrap_or_else(|err| { - panic!( - "Creating handle to component {} failed: {err}", - type_name::() - ); - }), - ) + let component = (|| { + let comp_ref = &entity_handle + .get_matching_components(ComponentT::id()) + .next()?; + + match ComponentHandleMut::::from_entity_component_ref(comp_ref, world) { + Ok(component) => Some(component), + Err(err) => { + panic!( + "Creating mut handle to component {} failed: {err}", + type_name::() + ); + } + } + })(); + + fields.with_elem(component) } } -- cgit v1.2.3-18-g5258