From a44e663eb6d4aaf567dd35f2676014ba5aaa9e00 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 5 Jan 2025 22:03:34 +0100 Subject: feat(ecs): allow control over component mutability in query --- ecs/src/query.rs | 114 ++++++------------------------------------------------- 1 file changed, 12 insertions(+), 102 deletions(-) (limited to 'ecs/src/query.rs') diff --git a/ecs/src/query.rs b/ecs/src/query.rs index 68964eb..53d4b74 100644 --- a/ecs/src/query.rs +++ b/ecs/src/query.rs @@ -9,22 +9,21 @@ use crate::component::storage::{ Storage as ComponentStorage, }; use crate::component::{ - Component, Metadata as ComponentMetadata, - Sequence as ComponentSequence, + RefSequence as ComponentRefSequence, }; -use crate::lock::{ReadGuard, WriteGuard}; +use crate::lock::ReadGuard; use crate::query::options::Options; use crate::system::{Param as SystemParam, System}; use crate::uid::Uid; -use crate::{EntityComponent, World}; +use crate::World; pub mod options; #[derive(Debug)] pub struct Query<'world, Comps, OptionsT = ()> where - Comps: ComponentSequence, + Comps: ComponentRefSequence, { world: &'world World, component_storage: ReadGuard<'world, ComponentStorage>, @@ -33,36 +32,9 @@ where impl<'world, Comps, OptionsT> Query<'world, Comps, OptionsT> where - Comps: ComponentSequence, + Comps: ComponentRefSequence, OptionsT: Options, { - /// Iterates over the entities matching this query. - #[must_use] - pub fn iter_mut<'query>( - &'query self, - ) -> ComponentIterMut<'query, 'world, Comps, QueryEntityIter<'query>> - { - #[cfg(feature = "debug")] - tracing::debug!("Searching for {}", std::any::type_name::()); - - #[allow(clippy::map_flatten)] - ComponentIterMut { - world: self.world, - entities: self - .component_storage - .iter_archetypes_with_comps(Comps::metadata()) - .map( - (|archetype| { - repeat_n(archetype, archetype.entity_cnt()) - .zip(archetype.entities()) - }) as ComponentIterMapFn, - ) - .flatten() - .filter(|(_, entity)| OptionsT::entity_filter(entity.components())), - comps_pd: PhantomData, - } - } - /// Iterates over the entities matching this query. #[must_use] pub fn iter<'query>( @@ -158,21 +130,21 @@ where impl<'query, 'world, Comps, OptionsT> IntoIterator for &'query Query<'world, Comps, OptionsT> where - Comps: ComponentSequence + 'world, + Comps: ComponentRefSequence + 'world, OptionsT: Options, { - type IntoIter = ComponentIterMut<'query, 'world, Comps, QueryEntityIter<'query>>; - type Item = Comps::MutRefs<'query>; + type IntoIter = ComponentIter<'query, 'world, Comps, QueryEntityIter<'query>>; + type Item = Comps::Handles<'query>; fn into_iter(self) -> Self::IntoIter { - self.iter_mut() + self.iter() } } impl<'world, Comps, OptionsT> SystemParam<'world> for Query<'world, Comps, OptionsT> where - Comps: ComponentSequence, + Comps: ComponentRefSequence, OptionsT: Options, { type Input = (); @@ -204,52 +176,6 @@ type QueryEntityIter<'query> = Filter< ComponentIterFilterFn, >; -pub struct ComponentIterMut<'query, 'world, Comps, EntityIter> -where - EntityIter: Iterator, -{ - world: &'world World, - entities: EntityIter, - comps_pd: PhantomData, -} - -impl<'query, 'world, Comps, EntityIter> Iterator - for ComponentIterMut<'query, 'world, Comps, EntityIter> -where - Comps: ComponentSequence + 'world, - EntityIter: Iterator, - 'world: 'query, -{ - type Item = Comps::MutRefs<'query>; - - fn next(&mut self) -> Option - { - let (archetype, entity) = self.entities.next()?; - - Some(Comps::from_components_mut( - entity.components(), - |component_uid| archetype.get_index_for_component(component_uid), - self.world, - lock_component_rw, - )) - } -} - -fn lock_component_rw( - entity_component: &EntityComponent, -) -> WriteGuard<'_, Box> -{ - entity_component - .component - .write_nonblock() - .unwrap_or_else(|_| { - panic!( - "Failed to acquire read-write lock to component {}", - entity_component.name - ); - }) -} - pub struct ComponentIter<'query, 'world, Comps, EntityIter> where EntityIter: Iterator, @@ -262,11 +188,11 @@ where impl<'query, 'world, Comps, EntityIter> Iterator for ComponentIter<'query, 'world, Comps, EntityIter> where - Comps: ComponentSequence + 'world, + Comps: ComponentRefSequence + 'world, EntityIter: Iterator, 'world: 'query, { - type Item = Comps::Refs<'query>; + type Item = Comps::Handles<'query>; fn next(&mut self) -> Option { @@ -276,22 +202,6 @@ where entity.components(), |component_uid| archetype.get_index_for_component(component_uid), self.world, - lock_component_ro, )) } } - -fn lock_component_ro( - entity_component: &EntityComponent, -) -> ReadGuard<'_, Box> -{ - entity_component - .component - .read_nonblock() - .unwrap_or_else(|_| { - panic!( - "Failed to acquire read-write lock to component {}", - entity_component.name - ); - }) -} -- cgit v1.2.3-18-g5258