From 0a7549ebaa7683b0c58379c2b0b4320981124acf Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 23 Mar 2025 20:17:20 +0100 Subject: refactor(ecs): move & rename EntityHandle to entity::Handle --- ecs/src/entity.rs | 36 +++++++++++++++++++++++++++++++++++- ecs/src/query.rs | 7 ++----- ecs/src/query/flexible.rs | 34 ++++------------------------------ 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs index 3de9cd5..85e7461 100644 --- a/ecs/src/entity.rs +++ b/ecs/src/entity.rs @@ -1,6 +1,40 @@ use linkme::distributed_slice; -use crate::World; +use crate::component::storage::archetype::{Archetype, ArchetypeEntity}; +use crate::uid::Uid; +use crate::{EntityComponent, World}; + +/// A handle to a entity. +pub struct Handle<'a> +{ + archetype: &'a Archetype, + entity: &'a ArchetypeEntity, +} + +impl<'a> Handle<'a> +{ + /// Returns the [`Uid`] of this entity. + #[inline] + #[must_use] + pub fn uid(&self) -> Uid + { + self.entity.uid + } + + #[inline] + #[must_use] + pub fn get_component(&self, component_uid: Uid) -> Option<&'a EntityComponent> + { + let index = self.archetype.get_index_for_component(component_uid)?; + + Some(self.entity.components.get(index).unwrap()) + } + + pub(crate) fn new(archetype: &'a Archetype, entity: &'a ArchetypeEntity) -> Self + { + Self { archetype, entity } + } +} #[allow(clippy::module_name_repetitions)] #[macro_export] diff --git a/ecs/src/query.rs b/ecs/src/query.rs index 1889e00..668c573 100644 --- a/ecs/src/query.rs +++ b/ecs/src/query.rs @@ -5,11 +5,8 @@ use std::marker::PhantomData; use seq_macro::seq; use crate::component::{Component, FromLockedOptional, Ref as ComponentRef}; -use crate::query::flexible::{ - EntityHandle, - Iter as FlexibleQueryIter, - Query as FlexibleQuery, -}; +use crate::entity::Handle as EntityHandle; +use crate::query::flexible::{Iter as FlexibleQueryIter, Query as FlexibleQuery}; use crate::system::{Param as SystemParam, System}; use crate::uid::Uid; use crate::util::VecExt; diff --git a/ecs/src/query/flexible.rs b/ecs/src/query/flexible.rs index 7d24dc9..652b96f 100644 --- a/ecs/src/query/flexible.rs +++ b/ecs/src/query/flexible.rs @@ -1,16 +1,16 @@ //! Low-level querying. use std::iter::{repeat_n, FlatMap, RepeatN, Zip}; -use crate::component::storage::archetype::{Archetype, ArchetypeEntity, EntityIter}; +use crate::component::storage::archetype::{Archetype, EntityIter}; use crate::component::storage::{ ArchetypeRefIter, ArchetypeSearchTerms, Storage as ComponentStorage, }; +use crate::entity::Handle as EntityHandle; use crate::lock::ReadGuard; use crate::query::Terms; -use crate::uid::Uid; -use crate::{EntityComponent, World}; +use crate::World; /// Low-level entity query structure. #[derive(Debug)] @@ -68,33 +68,7 @@ impl<'query> Iterator for Iter<'query> { let (archetype, entity) = self.iter.next()?; - Some(EntityHandle { archetype, entity }) - } -} - -pub struct EntityHandle<'query> -{ - archetype: &'query Archetype, - entity: &'query ArchetypeEntity, -} - -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 get_component(&self, component_uid: Uid) -> Option<&'query EntityComponent> - { - let index = self.archetype.get_index_for_component(component_uid)?; - - Some(self.entity.components.get(index).unwrap()) + Some(EntityHandle::new(archetype, entity)) } } -- cgit v1.2.3-18-g5258