From 58dd1d5d79070c3066f50975c918de291bbdf162 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 7 Apr 2025 22:20:46 +0200 Subject: refactor(ecs): make FromLockedOptional not take Lock --- ecs/src/relationship.rs | 72 +++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 32 deletions(-) (limited to 'ecs/src/relationship.rs') diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index 45fa265..c5399e4 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -6,13 +6,14 @@ use ecs_macros::Component; use crate::component::storage::Storage as ComponentStorage; use crate::component::{ Component, - FromLockedOptional as FromLockedOptionalComponent, Handle as ComponentHandle, + HandleError as ComponentHandleError, + HandleFromEntityComponentRef, HandleMut as ComponentHandleMut, }; -use crate::lock::{Error as LockError, Lock, ReadGuard}; +use crate::lock::ReadGuard; use crate::uid::{Kind as UidKind, Uid}; -use crate::World; +use crate::{EntityComponentRef, World}; /// A relationship to one or more targets. #[derive(Debug, Component)] @@ -70,22 +71,24 @@ where relationship_comp: ComponentHandleMut<'rel_comp, Relationship>, } -impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> +impl<'rel_comp, Kind, ComponentT> HandleFromEntityComponentRef<'rel_comp> for RelationMut<'rel_comp, Kind, ComponentT> where ComponentT: Component, { - fn from_locked_optional_component( - optional_component: Option<&'rel_comp crate::lock::Lock>>, + type Error = ComponentHandleError; + + fn from_entity_component_ref( + entity_component_ref: Option>, world: &'rel_comp World, - ) -> Result + ) -> Result { - let relationship_comp_handle_from_locked_opt_comp = ComponentHandleMut::< + let relationship_comp_handle_from_ent_comp_ref = ComponentHandleMut::< Relationship, - >::from_locked_optional_component; + >::from_entity_component_ref; let relationship_comp = - relationship_comp_handle_from_locked_opt_comp(optional_component, world)?; + relationship_comp_handle_from_ent_comp_ref(entity_component_ref, world)?; let component_storage_lock = world .data @@ -100,19 +103,21 @@ where } } -impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> +impl<'rel_comp, Kind, ComponentT> HandleFromEntityComponentRef<'rel_comp> for Option> where ComponentT: Component, { - fn from_locked_optional_component( - optional_component: Option<&'rel_comp Lock>>, + type Error = ComponentHandleError; + + fn from_entity_component_ref( + entity_component_ref: Option>, world: &'rel_comp World, - ) -> Result + ) -> Result { - optional_component - .map(|component| { - RelationMut::from_locked_optional_component(Some(component), world) + entity_component_ref + .map(|entity_comp| { + RelationMut::from_entity_component_ref(Some(entity_comp), world) }) .transpose() } @@ -292,22 +297,23 @@ where relationship_comp: ComponentHandle<'rel_comp, Relationship>, } -impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> +impl<'rel_comp, Kind, ComponentT> HandleFromEntityComponentRef<'rel_comp> for Relation<'rel_comp, Kind, ComponentT> where ComponentT: Component, { - fn from_locked_optional_component( - optional_component: Option<&'rel_comp Lock>>, + type Error = ComponentHandleError; + + fn from_entity_component_ref( + entity_component_ref: Option>, world: &'rel_comp World, - ) -> Result + ) -> Result { - let relationship_comp_handle_from_locked_opt_comp = ComponentHandle::< - Relationship, - >::from_locked_optional_component; + let relationship_comp_handle_from_ent_comp_ref = + ComponentHandle::>::from_entity_component_ref; let relationship_comp = - relationship_comp_handle_from_locked_opt_comp(optional_component, world)?; + relationship_comp_handle_from_ent_comp_ref(entity_component_ref, world)?; let component_storage_lock = world .data @@ -322,19 +328,21 @@ where } } -impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> +impl<'rel_comp, Kind, ComponentT> HandleFromEntityComponentRef<'rel_comp> for Option> where ComponentT: Component, { - fn from_locked_optional_component( - optional_component: Option<&'rel_comp Lock>>, + type Error = ComponentHandleError; + + fn from_entity_component_ref( + entity_component_ref: Option>, world: &'rel_comp World, - ) -> Result + ) -> Result { - optional_component - .map(|component| { - Relation::from_locked_optional_component(Some(component), world) + entity_component_ref + .map(|entity_comp| { + Relation::from_entity_component_ref(Some(entity_comp), world) }) .transpose() } -- cgit v1.2.3-18-g5258