use std::any::Any; use std::collections::HashSet; use std::iter::{Filter, Flatten, Map}; use std::marker::PhantomData; use std::sync::{Arc, Weak}; use crate::component::storage::{ Archetype, ArchetypeRefIter, Storage as ComponentStorage, }; use crate::component::{ Id as ComponentId, IsOptional as ComponentIsOptional, Sequence as ComponentSequence, }; use crate::lock::{Lock, ReadGuard}; use crate::query::options::Options; use crate::system::{ NoInitParamFlag as NoInitSystemParamFlag, Param as SystemParam, System, }; use crate::{EntityComponent, WorldData}; pub mod options; #[derive(Debug)] pub struct Query<'world, Comps, OptionsT = ()> where Comps: ComponentSequence, { component_storage: ReadGuard<'world, ComponentStorage>, component_storage_lock: Weak>, _pd: PhantomData<(Comps, OptionsT)>, } impl<'world, Comps, OptionsT> Query<'world, Comps, OptionsT> where Comps: ComponentSequence, OptionsT: Options, { /// Iterates over the entities matching this query. #[must_use] pub fn iter<'this>(&'this self) -> ComponentIter<'world, Comps> where 'this: 'world, { #[cfg(feature = "debug")] tracing::debug!("Searching for {}", std::any::type_name::()); #[allow(clippy::map_flatten)] ComponentIter { entities: self .component_storage .find_entities(&Comps::ids()) .map((|archetype| archetype.components.as_slice()) as ComponentIterMapFn) .flatten() .filter(|components| OptionsT::entity_filter(*components)), comps_pd: PhantomData, } } /// Returns a weak reference query to the same components. #[must_use] pub fn to_weak_ref(&self) -> WeakRef { WeakRef { component_storage: self.component_storage_lock.clone(), comps_pd: PhantomData, } } pub(crate) fn new(component_storage: &'world Arc>) -> Self { Self { component_storage: component_storage .read_nonblock() .expect("Failed to acquire read-only component storage lock"), component_storage_lock: Arc::downgrade(component_storage), _pd: PhantomData, } } } impl<'world, Comps, OptionsT> IntoIterator for &'world Query<'world, Comps, OptionsT> where Comps: ComponentSequence, OptionsT: Options, { type IntoIter = ComponentIter<'world, Comps>; type Item = Comps::Refs<'world>; fn into_iter(self) -> Self::IntoIter { self.iter() } } unsafe impl<'world, Comps, OptionsT> SystemParam<'world> for Query<'world, Comps, OptionsT> where Comps: ComponentSequence, OptionsT: Options, { type Flags = NoInitSystemParamFlag; type Input = (); fn initialize( _system: &mut impl System<'world, SystemImpl>, _input: Self::Input, ) { } fn new( _system: &'world impl System<'world, SystemImpl>, world_data: &'world WorldData, ) -> Self { Self::new(&world_data.component_storage) } fn is_compatible>() -> bool { let other_comparable = Other::get_comparable(); let Some(other_query_component_ids) = other_comparable.downcast_ref::() else { return true; }; !other_query_component_ids.contains_component_in::() } fn get_comparable() -> Box { Box::new(QueryComponentIds { component_ids: Comps::ids() }) } fn handle_pre_run(world_data: &WorldData) { let mut component_storage_lock = world_data .component_storage .write_nonblock() .expect("Failed to acquire read-write component storage lock"); #[cfg(feature = "debug")] tracing::debug!( "Adding archetypes lookup entry for components: ({})", std::any::type_name::() ); component_storage_lock.add_archetype_lookup_entry( &Comps::ids() .into_iter() .filter_map(|(component_id, is_optional)| { if is_optional == ComponentIsOptional::Yes { return None; } Some(component_id) }) .collect::>(), ); } } /// A entity query containing a weak reference to the world. #[derive(Debug)] pub struct WeakRef where Comps: ComponentSequence, { component_storage: Weak>, comps_pd: PhantomData<(Comps, OptionsT)>, } impl WeakRef where Comps: ComponentSequence, { /// Returns a struct which can be used to retrieve a [`Query`]. /// /// Returns [`None`] if the [`World`] has been dropped. #[must_use] pub fn access(&self) -> Option> { Some(Ref { component_storage: self.component_storage.upgrade()?, _pd: PhantomData, }) } } impl Clone for WeakRef where Comps: ComponentSequence, { fn clone(&self) -> Self { Self { component_storage: self.component_storage.clone(), comps_pd: PhantomData, } } } /// Intermediate between [`Query`] and [`WeakRefQuery`]. Contains a strong reference to /// the world which is not allowed direct access to. #[derive(Debug, Clone)] pub struct Ref<'weak_ref, Comps, OptionsT> where Comps: ComponentSequence, { component_storage: Arc>, _pd: PhantomData<(&'weak_ref Comps, OptionsT)>, } impl<'weak_ref, Comps, OptionsT> Ref<'weak_ref, Comps, OptionsT> where Comps: ComponentSequence, OptionsT: Options, { #[must_use] pub fn to_query(&self) -> Query<'_, Comps, OptionsT> { Query::new(&self.component_storage) } } type ComponentIterMapFn = for<'a> fn(&'a Archetype) -> &'a [Vec]; type ComponentIterFilterFn = for<'a, 'b> fn(&'a &'b Vec) -> bool; pub struct ComponentIter<'world, Comps> { entities: Filter< Flatten, ComponentIterMapFn>>, ComponentIterFilterFn, >, comps_pd: PhantomData, } impl<'world, Comps> Iterator for ComponentIter<'world, Comps> where Comps: ComponentSequence + 'world, { type Item = Comps::Refs<'world>; fn next(&mut self) -> Option { Some(Comps::from_components(self.entities.next()?.iter())) } } #[derive(Debug)] struct QueryComponentIds { component_ids: Vec<(ComponentId, ComponentIsOptional)>, } impl QueryComponentIds { fn contains_component_in(&self) -> bool where OtherComps: ComponentSequence, { let other_ids = OtherComps::ids() .into_iter() .map(|(id, _)| id) .collect::>(); self.component_ids .iter() .all(|(id, _)| other_ids.contains(id)) } }