From 799e66c3c92269aef8ba791b9db5661c625dfb95 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 14 Apr 2025 17:39:05 +0200 Subject: refactor(ecs): remove component::Ref trait --- ecs/src/query.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) (limited to 'ecs/src/query.rs') diff --git a/ecs/src/query.rs b/ecs/src/query.rs index 1d27b7a..1947c18 100644 --- a/ecs/src/query.rs +++ b/ecs/src/query.rs @@ -3,7 +3,12 @@ use std::marker::PhantomData; use seq_macro::seq; -use crate::component::{Component, HandleFromEntityComponentRef, Ref as ComponentRef}; +use crate::component::{ + Component, + Handle as ComponentHandle, + HandleFromEntityComponentRef, + HandleMut as ComponentHandleMut, +}; use crate::entity::Handle as EntityHandle; use crate::query::flexible::{Iter as FlexibleQueryIter, Query as FlexibleQuery}; use crate::system::{Param as SystemParam, System}; @@ -321,15 +326,15 @@ pub trait TermWithField ) -> Self::Field<'world>; } -impl TermWithField for ComponentRefT +impl TermWithField for &ComponentT { - type Field<'a> = ComponentRefT::Handle<'a>; + type Field<'a> = ComponentHandle<'a, ComponentT>; fn apply_to_terms_builder( terms_builder: &mut TermsBuilder, ) { - terms_builder.with::(); + terms_builder.with::(); } fn get_field<'world>( @@ -337,18 +342,51 @@ impl TermWithField for ComponentRefT world: &'world World, ) -> Self::Field<'world> { - assert_eq!(ComponentRefT::Component::id().kind(), UidKind::Component); + assert_eq!(ComponentT::id().kind(), UidKind::Component); Self::Field::from_entity_component_ref( entity_handle - .get_matching_components(ComponentRefT::Component::id()) + .get_matching_components(ComponentT::id()) .next(), world, ) .unwrap_or_else(|err| { panic!( "Creating handle to component {} failed: {err}", - type_name::() + type_name::() + ); + }) + } +} + +impl TermWithField for &mut ComponentT +{ + type Field<'a> = ComponentHandleMut<'a, ComponentT>; + + fn apply_to_terms_builder( + terms_builder: &mut TermsBuilder, + ) + { + terms_builder.with::(); + } + + fn get_field<'world>( + entity_handle: &EntityHandle<'world>, + world: &'world World, + ) -> Self::Field<'world> + { + assert_eq!(ComponentT::id().kind(), UidKind::Component); + + Self::Field::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::() ); }) } -- cgit v1.2.3-18-g5258