diff options
Diffstat (limited to 'ecs/src/relationship.rs')
-rw-r--r-- | ecs/src/relationship.rs | 116 |
1 files changed, 23 insertions, 93 deletions
diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index e5442c2..89c64ed 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -4,13 +4,8 @@ use std::marker::PhantomData; use ecs_macros::Component; use crate::component::storage::Storage as ComponentStorage; -use crate::component::{ - Component, - FromLockedOptional as FromLockedOptionalComponent, - FromOptional as FromOptionalComponent, - FromOptionalMut as FromOptionalMutComponent, -}; -use crate::lock::{Error as LockError, Lock, ReadGuard, WriteGuard}; +use crate::component::{Component, FromLockedOptional as FromLockedOptionalComponent}; +use crate::lock::{Error as LockError, Lock, ReadGuard}; use crate::system::{ComponentRef, ComponentRefMut}; use crate::uid::{Kind as UidKind, Uid}; use crate::World; @@ -71,23 +66,22 @@ where relationship_comp: ComponentRefMut<'rel_comp, Relationship<Kind, ComponentT>>, } -impl<'rel_comp, Kind, ComponentT> FromOptionalMutComponent<'rel_comp> +impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> for RelationMut<'rel_comp, Kind, ComponentT> where ComponentT: Component, { - fn from_optional_mut_component( - optional_component: Option< - crate::lock::WriteGuard<'rel_comp, Box<dyn Component>>, - >, + fn from_locked_optional_component( + optional_component: Option<&'rel_comp crate::lock::Lock<Box<dyn Component>>>, world: &'rel_comp World, - ) -> Self + ) -> Result<Self, LockError> { + let relationship_comp_ref_from_locked_opt_comp = ComponentRefMut::< + Relationship<Kind, ComponentT>, + >::from_locked_optional_component; + let relationship_comp = - ComponentRefMut::<Relationship<Kind, ComponentT>>::from_optional_mut_component( - optional_component, - world, - ); + relationship_comp_ref_from_locked_opt_comp(optional_component, world)?; let component_storage_lock = world .data @@ -95,49 +89,16 @@ where .read_nonblock() .expect("Failed to aquire read-only component storage lock"); - Self { + Ok(Self { relationship_comp, // SAFETY: The component lock is not used for longer than the original // lifetime component_storage_lock: unsafe { component_storage_lock.upgrade_lifetime() }, - } - } -} - -impl<'rel_comp, Kind, ComponentT> FromOptionalMutComponent<'rel_comp> - for Option<RelationMut<'rel_comp, Kind, ComponentT>> -where - ComponentT: Component, -{ - fn from_optional_mut_component( - optional_component: Option<WriteGuard<'rel_comp, Box<dyn Component>>>, - world: &'rel_comp World, - ) -> Self - { - optional_component.map(|component| { - RelationMut::from_optional_mut_component(Some(component), world) }) } } impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> - for RelationMut<'rel_comp, Kind, ComponentT> -where - ComponentT: Component, -{ - fn from_locked_optional_component( - optional_component: Option<&'rel_comp crate::lock::Lock<Box<dyn Component>>>, - world: &'rel_comp World, - ) -> Result<Self, LockError> - { - Ok(Self::from_optional_mut_component( - optional_component.map(Lock::write_nonblock).transpose()?, - world, - )) - } -} - -impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> for Option<RelationMut<'rel_comp, Kind, ComponentT>> where ComponentT: Component, @@ -329,21 +290,22 @@ where relationship_comp: ComponentRef<'rel_comp, Relationship<Kind, ComponentT>>, } -impl<'rel_comp, Kind, ComponentT> FromOptionalComponent<'rel_comp> +impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> for Relation<'rel_comp, Kind, ComponentT> where ComponentT: Component, { - fn from_optional_component( - optional_component: Option<ReadGuard<'rel_comp, Box<dyn Component>>>, + fn from_locked_optional_component( + optional_component: Option<&'rel_comp Lock<Box<dyn Component>>>, world: &'rel_comp World, - ) -> Self + ) -> Result<Self, LockError> { + let relationship_comp_ref_from_locked_opt_comp = ComponentRef::< + Relationship<Kind, ComponentT>, + >::from_locked_optional_component; + let relationship_comp = - ComponentRef::<Relationship<Kind, ComponentT>>::from_optional_component( - optional_component, - world, - ); + relationship_comp_ref_from_locked_opt_comp(optional_component, world)?; let component_storage_lock = world .data @@ -351,44 +313,12 @@ where .read_nonblock() .expect("Failed to aquire read-only component storage lock"); - Self { + Ok(Self { relationship_comp, // SAFETY: The component lock is not used for longer than the original // lifetime component_storage_lock: unsafe { component_storage_lock.upgrade_lifetime() }, - } - } -} - -impl<'rel_comp, Kind, ComponentT> FromOptionalComponent<'rel_comp> - for Option<Relation<'rel_comp, Kind, ComponentT>> -where - ComponentT: Component, -{ - fn from_optional_component( - optional_component: Option<ReadGuard<'rel_comp, Box<dyn Component>>>, - world: &'rel_comp World, - ) -> Self - { - optional_component - .map(|component| Relation::from_optional_component(Some(component), world)) - } -} - -impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> - for Relation<'rel_comp, Kind, ComponentT> -where - ComponentT: Component, -{ - fn from_locked_optional_component( - optional_component: Option<&'rel_comp Lock<Box<dyn Component>>>, - world: &'rel_comp World, - ) -> Result<Self, LockError> - { - Ok(Self::from_optional_component( - optional_component.map(Lock::read_nonblock).transpose()?, - world, - )) + }) } } |