diff options
author | HampusM <hampus@hampusmat.com> | 2024-05-19 21:12:07 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-05-19 21:14:55 +0200 |
commit | 9863b431950c681225f8774af244a56adbd18937 (patch) | |
tree | c487a12b9a7b4d362687d9fa065748af1f12cca8 /ecs/src/system.rs | |
parent | 355a19e630de61397bf70b69b7ab2356318be2b8 (diff) |
feat(ecs): add support for optional query components
Diffstat (limited to 'ecs/src/system.rs')
-rw-r--r-- | ecs/src/system.rs | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/ecs/src/system.rs b/ecs/src/system.rs index 3440a57..3c44148 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -1,4 +1,4 @@ -use std::any::Any; +use std::any::{type_name, Any}; use std::convert::Infallible; use std::fmt::Debug; use std::marker::PhantomData; @@ -8,7 +8,7 @@ use std::ptr::addr_of; use seq_macro::seq; -use crate::component::Component; +use crate::component::{Component, FromOptionalComponent}; use crate::lock::WriteGuard; use crate::system::util::check_params_are_compatible; use crate::tuple::{FilterElement as TupleFilterElement, With as TupleWith}; @@ -208,6 +208,38 @@ impl<'a, ComponentT: Component> ComponentRefMut<'a, ComponentT> } } +impl<'component, ComponentT: Component> FromOptionalComponent<'component> + for ComponentRefMut<'component, ComponentT> +{ + fn from_optional_component( + inner: Option<WriteGuard<'component, Box<dyn Component>>>, + ) -> 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<ComponentRefMut<'comp, ComponentT>> +where + ComponentT: Component, +{ + fn from_optional_component( + optional_component: Option<WriteGuard<'comp, Box<dyn Component>>>, + ) -> Self + { + optional_component.map(|component| ComponentRefMut::new(component)) + } +} + impl<'a, ComponentT: Component> Deref for ComponentRefMut<'a, ComponentT> { type Target = ComponentT; |