diff options
author | HampusM <hampus@hampusmat.com> | 2025-04-14 17:39:05 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-04-14 17:39:05 +0200 |
commit | 799e66c3c92269aef8ba791b9db5661c625dfb95 (patch) | |
tree | 67bfe73947428ec84b01ecdc31edfd3af1ab2e42 | |
parent | 3d2d3e51b6559b80bf945caa9914e557401e51d0 (diff) |
refactor(ecs): remove component::Ref trait
-rw-r--r-- | ecs/src/component.rs | 23 | ||||
-rw-r--r-- | ecs/src/query.rs | 52 | ||||
-rw-r--r-- | ecs/src/query/term.rs | 53 |
3 files changed, 90 insertions, 38 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index cc4b460..de4384b 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -77,29 +77,6 @@ pub trait Sequence fn into_parts_array(self) -> Self::PartsArray; } -/// A mutable or immutable reference to a component. -pub trait Ref -{ - type Component: Component; - type Handle<'component>: HandleFromEntityComponentRef<'component>; -} - -impl<ComponentT> Ref for &ComponentT -where - ComponentT: Component, -{ - type Component = ComponentT; - type Handle<'component> = ComponentT::Handle<'component>; -} - -impl<ComponentT> Ref for &mut ComponentT -where - ComponentT: Component, -{ - type Component = ComponentT; - type Handle<'component> = ComponentT::HandleMut<'component>; -} - /// [`Component`] metadata. #[derive(Debug, Clone)] #[non_exhaustive] 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<ComponentRefT: ComponentRef> TermWithField for ComponentRefT +impl<ComponentT: Component> TermWithField for &ComponentT { - type Field<'a> = ComponentRefT::Handle<'a>; + type Field<'a> = ComponentHandle<'a, ComponentT>; fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, ) { - terms_builder.with::<ComponentRefT::Component>(); + terms_builder.with::<ComponentT>(); } fn get_field<'world>( @@ -337,18 +342,51 @@ impl<ComponentRefT: ComponentRef> 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::<ComponentRefT::Component>() + type_name::<ComponentT>() + ); + }) + } +} + +impl<ComponentT: Component> TermWithField for &mut ComponentT +{ + type Field<'a> = ComponentHandleMut<'a, ComponentT>; + + fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( + terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, + ) + { + terms_builder.with::<ComponentT>(); + } + + 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::<ComponentT>() ); }) } diff --git a/ecs/src/query/term.rs b/ecs/src/query/term.rs index 597dd1a..2e1ecca 100644 --- a/ecs/src/query/term.rs +++ b/ecs/src/query/term.rs @@ -1,7 +1,12 @@ use std::any::type_name; use std::marker::PhantomData; -use crate::component::{Component, HandleFromEntityComponentRef, Ref as ComponentRef}; +use crate::component::{ + Component, + Handle as ComponentHandle, + HandleFromEntityComponentRef, + HandleMut as ComponentHandleMut, +}; use crate::query::{ TermWithField, TermWithoutField, @@ -48,11 +53,43 @@ where } } -impl<ComponentRefT> TermWithField for Option<ComponentRefT> -where - ComponentRefT: ComponentRef, +impl<ComponentT: Component> TermWithField for Option<&ComponentT> +{ + type Field<'a> = Option<ComponentHandle<'a, ComponentT>>; + + fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( + _terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, + ) + { + } + + fn get_field<'world>( + entity_handle: &crate::entity::Handle<'world>, + world: &'world crate::World, + ) -> Self::Field<'world> + { + Some( + ComponentHandle::<'world, ComponentT>::from_entity_component_ref( + Some( + entity_handle + .get_matching_components(ComponentT::id()) + .next()?, + ), + world, + ) + .unwrap_or_else(|err| { + panic!( + "Creating handle to component {} failed: {err}", + type_name::<ComponentT>() + ); + }), + ) + } +} + +impl<ComponentT: Component> TermWithField for Option<&mut ComponentT> { - type Field<'a> = Option<ComponentRefT::Handle<'a>>; + type Field<'a> = Option<ComponentHandleMut<'a, ComponentT>>; fn apply_to_terms_builder<const MAX_TERM_CNT: usize>( _terms_builder: &mut TermsBuilder<MAX_TERM_CNT>, @@ -66,10 +103,10 @@ where ) -> Self::Field<'world> { Some( - ComponentRefT::Handle::<'world>::from_entity_component_ref( + ComponentHandleMut::<'world, ComponentT>::from_entity_component_ref( Some( entity_handle - .get_matching_components(ComponentRefT::Component::id()) + .get_matching_components(ComponentT::id()) .next()?, ), world, @@ -77,7 +114,7 @@ where .unwrap_or_else(|err| { panic!( "Creating handle to component {} failed: {err}", - type_name::<ComponentRefT::Component>() + type_name::<ComponentT>() ); }), ) |