summaryrefslogtreecommitdiff
path: root/ecs/src/query.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ecs/src/query.rs')
-rw-r--r--ecs/src/query.rs114
1 files changed, 12 insertions, 102 deletions
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,38 +32,11 @@ 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::<Comps>());
-
- #[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>(
&'query self,
) -> ComponentIter<'query, 'world, Comps, QueryEntityIter<'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<Item = (&'query Archetype, &'query ArchetypeEntity)>,
-{
- world: &'world World,
- entities: EntityIter,
- comps_pd: PhantomData<Comps>,
-}
-
-impl<'query, 'world, Comps, EntityIter> Iterator
- for ComponentIterMut<'query, 'world, Comps, EntityIter>
-where
- Comps: ComponentSequence + 'world,
- EntityIter: Iterator<Item = (&'query Archetype, &'query ArchetypeEntity)>,
- 'world: 'query,
-{
- type Item = Comps::MutRefs<'query>;
-
- fn next(&mut self) -> Option<Self::Item>
- {
- 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<dyn Component>>
-{
- 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<Item = (&'query Archetype, &'query ArchetypeEntity)>,
@@ -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<Item = (&'query Archetype, &'query ArchetypeEntity)>,
'world: 'query,
{
- type Item = Comps::Refs<'query>;
+ type Item = Comps::Handles<'query>;
fn next(&mut self) -> Option<Self::Item>
{
@@ -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<dyn Component>>
-{
- entity_component
- .component
- .read_nonblock()
- .unwrap_or_else(|_| {
- panic!(
- "Failed to acquire read-write lock to component {}",
- entity_component.name
- );
- })
-}