diff options
Diffstat (limited to 'ecs/src/query')
-rw-r--r-- | ecs/src/query/flexible.rs | 23 | ||||
-rw-r--r-- | ecs/src/query/options.rs | 10 |
2 files changed, 20 insertions, 13 deletions
diff --git a/ecs/src/query/flexible.rs b/ecs/src/query/flexible.rs index 3bb8fd6..c42ec25 100644 --- a/ecs/src/query/flexible.rs +++ b/ecs/src/query/flexible.rs @@ -1,5 +1,5 @@ //! Low-level querying. -use std::iter::{repeat_n, Filter, Flatten, Map, RepeatN, Zip}; +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}; @@ -30,19 +30,18 @@ where { /// Iterates over the entities matching this query. #[must_use] - pub fn iter<'query, OptionsT: Options>(&'query self) -> Iter<'query> + pub fn iter<OptionsT: Options>(&self) -> Iter<'_> { Iter { iter: self .component_storage - .search_archetypes(&self.comp_metadata.as_ref()) - .map( + .search_archetypes(self.comp_metadata.as_ref()) + .flat_map( (|archetype| { repeat_n(archetype, archetype.entity_cnt()) .zip(archetype.entities()) }) as ComponentIterMapFn, ) - .flatten() .filter(|(_, entity)| OptionsT::entity_filter(&entity.components)), } } @@ -110,31 +109,39 @@ impl<'query> EntityHandle<'query> { /// Returns the [`Uid`] of this entity. #[inline] + #[must_use] pub fn uid(&self) -> Uid { self.entity.uid } #[inline] + #[must_use] pub fn components(&self) -> &'query [EntityComponent] { &self.entity.components } #[inline] + #[must_use] pub fn get_component_index(&self, component_uid: Uid) -> Option<usize> { self.archetype.get_index_for_component(component_uid) } } -type ComponentIterMapFn = - for<'a> fn(&'a Archetype) -> Zip<RepeatN<&'a Archetype>, EntityIter<'a>>; +type ComponentIterMapFnOutput<'a> = Zip<RepeatN<&'a Archetype>, EntityIter<'a>>; + +type ComponentIterMapFn = for<'a> fn(&'a Archetype) -> ComponentIterMapFnOutput<'a>; type ComponentIterFilterFn = for<'a, 'b> fn(&'a (&'b Archetype, &'b ArchetypeEntity)) -> bool; type QueryEntityIter<'query> = Filter< - Flatten<Map<ArchetypeRefIter<'query>, ComponentIterMapFn>>, + FlatMap< + ArchetypeRefIter<'query>, + ComponentIterMapFnOutput<'query>, + ComponentIterMapFn, + >, ComponentIterFilterFn, >; diff --git a/ecs/src/query/options.rs b/ecs/src/query/options.rs index 772d091..6318359 100644 --- a/ecs/src/query/options.rs +++ b/ecs/src/query/options.rs @@ -8,12 +8,12 @@ use crate::EntityComponent; /// Query options. pub trait Options { - fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool; + fn entity_filter(components: &[EntityComponent]) -> bool; } impl Options for () { - fn entity_filter<'component>(_components: &'component [EntityComponent]) -> bool + fn entity_filter(_components: &[EntityComponent]) -> bool { true } @@ -30,10 +30,10 @@ impl<ComponentT> Options for With<ComponentT> where ComponentT: Component, { - fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool + fn entity_filter(components: &[EntityComponent]) -> bool { let ids_set = components - .into_iter() + .iter() .map(|component| component.id) .collect::<HashSet<_>>(); @@ -52,7 +52,7 @@ impl<OptionsT> Options for Not<OptionsT> where OptionsT: Options, { - fn entity_filter<'component>(components: &'component [EntityComponent]) -> bool + fn entity_filter(components: &[EntityComponent]) -> bool { !OptionsT::entity_filter(components) } |