diff options
author | HampusM <hampus@hampusmat.com> | 2025-01-04 15:07:14 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-01-04 15:07:37 +0100 |
commit | cd385ddedc767c953f24109ec3ffe0a07d247ff5 (patch) | |
tree | 4294f908aadbd85b91f9230568b62ed8af691b46 /ecs/src | |
parent | 9cb1cc6735b4275d3af9e3257075fbb9f81adf1f (diff) |
refactor(ecs): add query component iter lifetime params
Diffstat (limited to 'ecs/src')
-rw-r--r-- | ecs/src/query.rs | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/ecs/src/query.rs b/ecs/src/query.rs index 3905fb8..68964eb 100644 --- a/ecs/src/query.rs +++ b/ecs/src/query.rs @@ -38,9 +38,9 @@ where { /// Iterates over the entities matching this query. #[must_use] - pub fn iter_mut( - &'world self, - ) -> ComponentIterMut<'world, Comps, QueryEntityIter<'world>> + pub fn iter_mut<'query>( + &'query self, + ) -> ComponentIterMut<'query, 'world, Comps, QueryEntityIter<'query>> { #[cfg(feature = "debug")] tracing::debug!("Searching for {}", std::any::type_name::<Comps>()); @@ -65,7 +65,9 @@ where /// Iterates over the entities matching this query. #[must_use] - pub fn iter(&'world self) -> ComponentIter<'world, Comps, QueryEntityIter<'world>> + pub fn iter<'query>( + &'query self, + ) -> ComponentIter<'query, 'world, Comps, QueryEntityIter<'query>> { #[cfg(feature = "debug")] tracing::debug!("Searching for {}", std::any::type_name::<Comps>()); @@ -91,10 +93,10 @@ where /// Iterates over the entities matching this query and has the provided extra /// component. #[must_use] - pub fn iter_with_extra_comps( - &'world self, + pub fn iter_with_extra_comps<'query>( + &'query self, extra_components: impl IntoIterator<Item = ComponentMetadata>, - ) -> ComponentIter<'world, Comps, QueryEntityIter<'world>> + ) -> ComponentIter<'query, 'world, Comps, QueryEntityIter<'query>> { #[cfg(feature = "debug")] tracing::debug!( @@ -153,13 +155,14 @@ where } } -impl<'world, Comps, OptionsT> IntoIterator for &'world Query<'world, Comps, OptionsT> +impl<'query, 'world, Comps, OptionsT> IntoIterator + for &'query Query<'world, Comps, OptionsT> where - Comps: ComponentSequence, + Comps: ComponentSequence + 'world, OptionsT: Options, { - type IntoIter = ComponentIterMut<'world, Comps, QueryEntityIter<'world>>; - type Item = Comps::MutRefs<'world>; + type IntoIter = ComponentIterMut<'query, 'world, Comps, QueryEntityIter<'query>>; + type Item = Comps::MutRefs<'query>; fn into_iter(self) -> Self::IntoIter { @@ -196,26 +199,28 @@ type ComponentIterMapFn = type ComponentIterFilterFn = for<'a, 'b> fn(&'a (&'b Archetype, &'b ArchetypeEntity)) -> bool; -type QueryEntityIter<'world> = Filter< - Flatten<Map<ArchetypeRefIter<'world>, ComponentIterMapFn>>, +type QueryEntityIter<'query> = Filter< + Flatten<Map<ArchetypeRefIter<'query>, ComponentIterMapFn>>, ComponentIterFilterFn, >; -pub struct ComponentIterMut<'world, Comps, EntityIter> +pub struct ComponentIterMut<'query, 'world, Comps, EntityIter> where - EntityIter: Iterator<Item = (&'world Archetype, &'world ArchetypeEntity)>, + EntityIter: Iterator<Item = (&'query Archetype, &'query ArchetypeEntity)>, { world: &'world World, entities: EntityIter, comps_pd: PhantomData<Comps>, } -impl<'world, Comps, EntityIter> Iterator for ComponentIterMut<'world, Comps, EntityIter> +impl<'query, 'world, Comps, EntityIter> Iterator + for ComponentIterMut<'query, 'world, Comps, EntityIter> where Comps: ComponentSequence + 'world, - EntityIter: Iterator<Item = (&'world Archetype, &'world ArchetypeEntity)>, + EntityIter: Iterator<Item = (&'query Archetype, &'query ArchetypeEntity)>, + 'world: 'query, { - type Item = Comps::MutRefs<'world>; + type Item = Comps::MutRefs<'query>; fn next(&mut self) -> Option<Self::Item> { @@ -245,21 +250,23 @@ fn lock_component_rw( }) } -pub struct ComponentIter<'world, Comps, EntityIter> +pub struct ComponentIter<'query, 'world, Comps, EntityIter> where - EntityIter: Iterator<Item = (&'world Archetype, &'world ArchetypeEntity)>, + EntityIter: Iterator<Item = (&'query Archetype, &'query ArchetypeEntity)>, { world: &'world World, entities: EntityIter, comps_pd: PhantomData<Comps>, } -impl<'world, Comps, EntityIter> Iterator for ComponentIter<'world, Comps, EntityIter> +impl<'query, 'world, Comps, EntityIter> Iterator + for ComponentIter<'query, 'world, Comps, EntityIter> where Comps: ComponentSequence + 'world, - EntityIter: Iterator<Item = (&'world Archetype, &'world ArchetypeEntity)>, + EntityIter: Iterator<Item = (&'query Archetype, &'query ArchetypeEntity)>, + 'world: 'query, { - type Item = Comps::Refs<'world>; + type Item = Comps::Refs<'query>; fn next(&mut self) -> Option<Self::Item> { |