diff options
Diffstat (limited to 'ecs/src/relationship.rs')
-rw-r--r-- | ecs/src/relationship.rs | 158 |
1 files changed, 44 insertions, 114 deletions
diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index a0ccf4d..45fa265 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -7,19 +7,18 @@ use crate::component::storage::Storage as ComponentStorage; use crate::component::{ Component, FromLockedOptional as FromLockedOptionalComponent, - FromOptional as FromOptionalComponent, - FromOptionalMut as FromOptionalMutComponent, + Handle as ComponentHandle, + HandleMut as ComponentHandleMut, }; -use crate::lock::{Error as LockError, Lock, ReadGuard, WriteGuard}; -use crate::system::{ComponentRef, ComponentRefMut}; +use crate::lock::{Error as LockError, Lock, ReadGuard}; use crate::uid::{Kind as UidKind, Uid}; use crate::World; /// A relationship to one or more targets. #[derive(Debug, Component)] #[component( - ref_type = Relation<'component, Kind, ComponentT>, - ref_mut_type = RelationMut<'component, Kind, ComponentT>, + handle_type = Relation<'component, Kind, ComponentT>, + handle_mut_type = RelationMut<'component, Kind, ComponentT>, )] pub struct Relationship<Kind, ComponentT: Component> where @@ -67,27 +66,26 @@ where Kind: 'static, ComponentT: Component, { - component_storage_lock: ReadGuard<'static, ComponentStorage>, - relationship_comp: ComponentRefMut<'rel_comp, Relationship<Kind, ComponentT>>, + component_storage_lock: ReadGuard<'rel_comp, ComponentStorage>, + relationship_comp: ComponentHandleMut<'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_handle_from_locked_opt_comp = ComponentHandleMut::< + Relationship<Kind, ComponentT>, + >::from_locked_optional_component; + let relationship_comp = - ComponentRefMut::<Relationship<Kind, ComponentT>>::from_optional_mut_component( - optional_component, - world, - ); + relationship_comp_handle_from_locked_opt_comp(optional_component, world)?; let component_storage_lock = world .data @@ -95,49 +93,14 @@ 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) + component_storage_lock, }) } } 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, @@ -165,7 +128,7 @@ where /// Will panic if the entity does not exist in the archetype it belongs to. This /// should hopefully never happend. #[must_use] - pub fn get(&self, index: usize) -> Option<ComponentRefMut<'_, ComponentT>> + pub fn get(&self, index: usize) -> Option<ComponentHandleMut<'_, ComponentT>> { let target = self.get_target(index)?; @@ -177,11 +140,11 @@ where let component_index = archetype.get_index_for_component(ComponentT::id())?; - let component = ComponentRefMut::new( + let component = ComponentHandleMut::new( entity - .components + .components() .get(component_index)? - .component + .component() .write_nonblock() .unwrap_or_else(|_| { panic!( @@ -276,7 +239,7 @@ where ComponentT: Component, { type IntoIter = TargetComponentIterMut<'relationship, 'rel_comp, Kind, ComponentT>; - type Item = ComponentRefMut<'rel_comp, ComponentT>; + type Item = ComponentHandleMut<'rel_comp, ComponentT>; fn into_iter(self) -> Self::IntoIter { @@ -301,7 +264,7 @@ where Kind: 'static, ComponentT: Component, { - type Item = ComponentRefMut<'rel_comp, ComponentT>; + type Item = ComponentHandleMut<'rel_comp, ComponentT>; fn next(&mut self) -> Option<Self::Item> { @@ -325,25 +288,26 @@ where Kind: 'static, ComponentT: Component, { - component_storage_lock: ReadGuard<'static, ComponentStorage>, - relationship_comp: ComponentRef<'rel_comp, Relationship<Kind, ComponentT>>, + component_storage_lock: ReadGuard<'rel_comp, ComponentStorage>, + relationship_comp: ComponentHandle<'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_handle_from_locked_opt_comp = ComponentHandle::< + Relationship<Kind, ComponentT>, + >::from_locked_optional_component; + let relationship_comp = - ComponentRef::<Relationship<Kind, ComponentT>>::from_optional_component( - optional_component, - world, - ); + relationship_comp_handle_from_locked_opt_comp(optional_component, world)?; let component_storage_lock = world .data @@ -351,44 +315,10 @@ 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, - )) + component_storage_lock, + }) } } @@ -420,7 +350,7 @@ where /// Will panic if the entity does not exist in the archetype it belongs to. This /// should hopefully never happend. #[must_use] - pub fn get(&self, index: usize) -> Option<ComponentRef<'_, ComponentT>> + pub fn get(&self, index: usize) -> Option<ComponentHandle<'_, ComponentT>> { let target = self.get_target(index)?; @@ -432,11 +362,11 @@ where let component_index = archetype.get_index_for_component(ComponentT::id())?; - let component = ComponentRef::new( + let component = ComponentHandle::new( entity - .components + .components() .get(component_index)? - .component + .component() .read_nonblock() .unwrap_or_else(|_| { panic!( @@ -490,7 +420,7 @@ where ComponentT: Component, { type IntoIter = TargetComponentIter<'relationship, 'rel_comp, Kind, ComponentT>; - type Item = ComponentRef<'rel_comp, ComponentT>; + type Item = ComponentHandle<'rel_comp, ComponentT>; fn into_iter(self) -> Self::IntoIter { @@ -515,7 +445,7 @@ where Kind: 'static, ComponentT: Component, { - type Item = ComponentRef<'rel_comp, ComponentT>; + type Item = ComponentHandle<'rel_comp, ComponentT>; fn next(&mut self) -> Option<Self::Item> { |