From caef866b4e3f87fd6ae2dd5b979a1fe1a1f3e5f2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 3 Nov 2024 21:12:57 +0100 Subject: feat(ecs): add read-only query iterating --- ecs/src/system.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 6 deletions(-) (limited to 'ecs/src/system.rs') diff --git a/ecs/src/system.rs b/ecs/src/system.rs index 7c0e454..afb5aac 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -7,8 +7,12 @@ use std::panic::{RefUnwindSafe, UnwindSafe}; use seq_macro::seq; -use crate::component::{Component, FromOptional as FromOptionalComponent}; -use crate::lock::WriteGuard; +use crate::component::{ + Component, + FromOptional as FromOptionalComponent, + FromOptionalMut as FromOptionalMutComponent, +}; +use crate::lock::{ReadGuard, WriteGuard}; use crate::system::util::check_params_are_compatible; use crate::tuple::{ReduceElement as TupleReduceElement, With as TupleWith}; use crate::World; @@ -214,10 +218,10 @@ impl<'a, ComponentT: Component> ComponentRefMut<'a, ComponentT> } } -impl<'component, ComponentT: Component> FromOptionalComponent<'component> +impl<'component, ComponentT: Component> FromOptionalMutComponent<'component> for ComponentRefMut<'component, ComponentT> { - fn from_optional_component( + fn from_optional_mut_component( inner: Option>>, _world: &'component World, ) -> Self @@ -234,12 +238,12 @@ impl<'component, ComponentT: Component> FromOptionalComponent<'component> } } -impl<'comp, ComponentT> FromOptionalComponent<'comp> +impl<'comp, ComponentT> FromOptionalMutComponent<'comp> for Option> where ComponentT: Component, { - fn from_optional_component( + fn from_optional_mut_component( optional_component: Option>>, _world: &'comp World, ) -> Self @@ -265,3 +269,62 @@ impl<'a, ComponentT: Component> DerefMut for ComponentRefMut<'a, ComponentT> self.inner.downcast_mut().unwrap() } } + +#[derive(Debug)] +pub struct ComponentRef<'a, ComponentT: Component> +{ + inner: ReadGuard<'a, Box>, + _ph: PhantomData, +} + +impl<'a, ComponentT: Component> ComponentRef<'a, ComponentT> +{ + pub(crate) fn new(inner: ReadGuard<'a, Box>) -> Self + { + Self { inner, _ph: PhantomData } + } +} + +impl<'component, ComponentT: Component> FromOptionalComponent<'component> + for ComponentRef<'component, ComponentT> +{ + fn from_optional_component( + inner: Option>>, + _world: &'component World, + ) -> Self + { + Self { + inner: inner.unwrap_or_else(|| { + panic!( + "Component {} was not found in entity", + type_name::() + ); + }), + _ph: PhantomData, + } + } +} + +impl<'comp, ComponentT> FromOptionalComponent<'comp> + for Option> +where + ComponentT: Component, +{ + fn from_optional_component( + optional_component: Option>>, + _world: &'comp World, + ) -> Self + { + optional_component.map(|component| ComponentRef::new(component)) + } +} + +impl<'a, ComponentT: Component> Deref for ComponentRef<'a, ComponentT> +{ + type Target = ComponentT; + + fn deref(&self) -> &Self::Target + { + self.inner.downcast_ref().unwrap() + } +} -- cgit v1.2.3-18-g5258