From 76e7e612e7b516bf52b508ae5bb367b1ddc3babc Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 18 Mar 2025 16:59:53 +0100 Subject: refactor(ecs): replace component::RefSequence with query terms --- ecs/src/query/flexible.rs | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) (limited to 'ecs/src/query') diff --git a/ecs/src/query/flexible.rs b/ecs/src/query/flexible.rs index c42ec25..a3e8701 100644 --- a/ecs/src/query/flexible.rs +++ b/ecs/src/query/flexible.rs @@ -3,30 +3,21 @@ use std::iter::{repeat_n, Filter, FlatMap, RepeatN, Zip}; use crate::component::storage::archetype::{Archetype, ArchetypeEntity, EntityIter}; use crate::component::storage::{ArchetypeRefIter, Storage as ComponentStorage}; -use crate::component::{ - Metadata as ComponentMetadata, - RefSequence as ComponentRefSequence, -}; use crate::lock::ReadGuard; use crate::query::options::Options; -use crate::query::ComponentIter; +use crate::query::Terms; use crate::uid::Uid; -use crate::util::Sortable; use crate::{EntityComponent, World}; /// Low-level entity query structure. #[derive(Debug)] -pub struct Query<'world, CompMetadata> -where - CompMetadata: Sortable + AsRef<[ComponentMetadata]>, +pub struct Query<'world, 'terms> { component_storage: ReadGuard<'world, ComponentStorage>, - comp_metadata: CompMetadata, + terms: Terms<'terms>, } -impl<'world, CompMetadata> Query<'world, CompMetadata> -where - CompMetadata: Sortable + AsRef<[ComponentMetadata]>, +impl<'world, 'terms> Query<'world, 'terms> { /// Iterates over the entities matching this query. #[must_use] @@ -35,7 +26,7 @@ where Iter { iter: self .component_storage - .search_archetypes(self.comp_metadata.as_ref()) + .search_archetypes(self.terms.required_components.as_ref()) .flat_map( (|archetype| { repeat_n(archetype, archetype.entity_cnt()) @@ -46,17 +37,15 @@ where } } - pub(crate) fn new(world: &'world World, mut comp_metadata: CompMetadata) -> Self + pub(crate) fn new(world: &'world World, terms: Terms<'terms>) -> Self { - comp_metadata.sort_by_key_b(|metadata| metadata.id); - Self { component_storage: world .data .component_storage .read_nonblock() .expect("Failed to acquire read-only component storage lock"), - comp_metadata, + terms, } } } @@ -66,27 +55,6 @@ pub struct Iter<'query> iter: QueryEntityIter<'query>, } -impl<'query> Iter<'query> -{ - /// Converts this iterator into a [`ComponentIter`]. - /// - /// Note: The matching entities of this iterator should have all of the non-[`Option`] - /// components in `Comps`, otherwise iterating the [`ComponentIter`] will cause a - /// panic. - #[must_use] - #[inline] - pub fn into_component_iter<'world, Comps>( - self, - world: &'world World, - ) -> ComponentIter<'query, 'world, Comps, Self> - where - Comps: ComponentRefSequence + 'world, - 'world: 'query, - { - ComponentIter::new(world, self) - } -} - impl<'query> Iterator for Iter<'query> { type Item = EntityHandle<'query>; -- cgit v1.2.3-18-g5258