diff options
author | HampusM <hampus@hampusmat.com> | 2024-11-03 21:12:57 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-11-03 21:12:57 +0100 |
commit | caef866b4e3f87fd6ae2dd5b979a1fe1a1f3e5f2 (patch) | |
tree | fa390998ff41b11bb0e60298a81f45619fbe4d69 /ecs/src/system.rs | |
parent | 373a0d53f31b838b3f650a37df39176a31a6c1c4 (diff) |
feat(ecs): add read-only query iterating
Diffstat (limited to 'ecs/src/system.rs')
-rw-r--r-- | ecs/src/system.rs | 75 |
1 files changed, 69 insertions, 6 deletions
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<WriteGuard<'component, Box<dyn Component>>>, _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<ComponentRefMut<'comp, ComponentT>> where ComponentT: Component, { - fn from_optional_component( + fn from_optional_mut_component( optional_component: Option<WriteGuard<'comp, Box<dyn Component>>>, _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<dyn Component>>, + _ph: PhantomData<ComponentT>, +} + +impl<'a, ComponentT: Component> ComponentRef<'a, ComponentT> +{ + pub(crate) fn new(inner: ReadGuard<'a, Box<dyn Component>>) -> Self + { + Self { inner, _ph: PhantomData } + } +} + +impl<'component, ComponentT: Component> FromOptionalComponent<'component> + for ComponentRef<'component, ComponentT> +{ + fn from_optional_component( + inner: Option<ReadGuard<'component, Box<dyn Component>>>, + _world: &'component World, + ) -> Self + { + Self { + inner: inner.unwrap_or_else(|| { + panic!( + "Component {} was not found in entity", + type_name::<ComponentT>() + ); + }), + _ph: PhantomData, + } + } +} + +impl<'comp, ComponentT> FromOptionalComponent<'comp> + for Option<ComponentRef<'comp, ComponentT>> +where + ComponentT: Component, +{ + fn from_optional_component( + optional_component: Option<ReadGuard<'comp, Box<dyn Component>>>, + _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() + } +} |