From ec883f3fd7bec95ff2ab6b482cc3b7ce6e40a293 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 2 Apr 2025 13:22:18 +0200 Subject: refactor(ecs): remove unnecessary component::FromOptional* traits --- ecs/src/component.rs | 33 +++----------- ecs/src/relationship.rs | 116 ++++++++++-------------------------------------- ecs/src/system.rs | 89 +++++++------------------------------ 3 files changed, 44 insertions(+), 194 deletions(-) (limited to 'ecs/src') diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 265eb9e..63be828 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -8,7 +8,7 @@ use crate::event::component::{ Kind as ComponentEventKind, Removed as ComponentRemovedEvent, }; -use crate::lock::{Error as LockError, Lock, ReadGuard, WriteGuard}; +use crate::lock::{Error as LockError, Lock}; use crate::system::Input as SystemInput; use crate::type_name::TypeName; use crate::uid::Uid; @@ -26,11 +26,11 @@ pub trait Component: SystemInput + Any + TypeName where Self: Sized; - type RefMut<'component>: FromOptionalMut<'component> + FromLockedOptional<'component> + type RefMut<'component>: FromLockedOptional<'component> where Self: Sized; - type Ref<'component>: FromOptional<'component> + FromLockedOptional<'component> + type Ref<'component>: FromLockedOptional<'component> where Self: Sized; @@ -101,8 +101,8 @@ impl TypeName for Box impl Component for Option where ComponentT: Component, - for<'a> Option>: FromOptional<'a> + FromLockedOptional<'a>, - for<'a> Option>: FromOptionalMut<'a> + FromLockedOptional<'a>, + for<'a> Option>: FromLockedOptional<'a>, + for<'a> Option>: FromLockedOptional<'a>, { type Component = ComponentT; type Ref<'component> = Option>; @@ -220,22 +220,6 @@ impl Metadata } } -pub trait FromOptionalMut<'comp> -{ - fn from_optional_mut_component( - optional_component: Option>>, - world: &'comp World, - ) -> Self; -} - -pub trait FromOptional<'comp> -{ - fn from_optional_component( - optional_component: Option>>, - world: &'comp World, - ) -> Self; -} - pub trait FromLockedOptional<'comp>: Sized { /// Converts a reference to a optional locked boxed component to a instance of `Self`. @@ -254,12 +238,7 @@ macro_rules! inner { impl<#(IntoComp~I,)*> Sequence for (#(IntoComp~I,)*) where #( - for<'comp> IntoComp~I: Into< - Component: Component< - RefMut<'comp>: FromOptionalMut<'comp>, - Ref<'comp>: FromOptional<'comp> - > - >, + for<'comp> IntoComp~I: Into, )* { const COUNT: usize = $c + 1; 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>, } -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>, - >, + fn from_locked_optional_component( + optional_component: Option<&'rel_comp crate::lock::Lock>>, world: &'rel_comp World, - ) -> Self + ) -> Result { + let relationship_comp_ref_from_locked_opt_comp = ComponentRefMut::< + Relationship, + >::from_locked_optional_component; + let relationship_comp = - ComponentRefMut::>::from_optional_mut_component( - optional_component, - world, - ); + relationship_comp_ref_from_locked_opt_comp(optional_component, world)?; let component_storage_lock = world .data @@ -95,48 +89,15 @@ 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> -where - ComponentT: Component, -{ - fn from_optional_mut_component( - optional_component: Option>>, - 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>>, - world: &'rel_comp World, - ) -> Result - { - Ok(Self::from_optional_mut_component( - optional_component.map(Lock::write_nonblock).transpose()?, - world, - )) - } -} - impl<'rel_comp, Kind, ComponentT> FromLockedOptionalComponent<'rel_comp> for Option> where @@ -329,21 +290,22 @@ where relationship_comp: ComponentRef<'rel_comp, Relationship>, } -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>>, + fn from_locked_optional_component( + optional_component: Option<&'rel_comp Lock>>, world: &'rel_comp World, - ) -> Self + ) -> Result { + let relationship_comp_ref_from_locked_opt_comp = ComponentRef::< + Relationship, + >::from_locked_optional_component; + let relationship_comp = - ComponentRef::>::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> -where - ComponentT: Component, -{ - fn from_optional_component( - optional_component: Option>>, - 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>>, - world: &'rel_comp World, - ) -> Result - { - Ok(Self::from_optional_component( - optional_component.map(Lock::read_nonblock).transpose()?, - world, - )) + }) } } diff --git a/ecs/src/system.rs b/ecs/src/system.rs index 40eba8d..fdf87f8 100644 --- a/ecs/src/system.rs +++ b/ecs/src/system.rs @@ -7,12 +7,7 @@ use std::ops::{Deref, DerefMut}; use ecs_macros::Component; use seq_macro::seq; -use crate::component::{ - Component, - FromLockedOptional as FromLockedOptionalComponent, - FromOptional as FromOptionalComponent, - FromOptionalMut as FromOptionalMutComponent, -}; +use crate::component::{Component, FromLockedOptional as FromLockedOptionalComponent}; use crate::lock::{ Error as LockError, Lock, @@ -221,49 +216,22 @@ impl<'a, ComponentT: Component> ComponentRefMut<'a, ComponentT> } } -impl<'component, ComponentT: Component> FromOptionalMutComponent<'component> +impl<'component, ComponentT: Component> FromLockedOptionalComponent<'component> for ComponentRefMut<'component, ComponentT> { - fn from_optional_mut_component( - inner: Option>>, + fn from_locked_optional_component( + optional_component: Option<&'component crate::lock::Lock>>, _world: &'component World, - ) -> Self + ) -> Result { - Self::new(inner.unwrap_or_else(|| { + let component = optional_component.unwrap_or_else(|| { panic!( "Component {} was not found in entity", type_name::() ); - })) - } -} - -impl<'component, ComponentT: Component> FromLockedOptionalComponent<'component> - for ComponentRefMut<'component, ComponentT> -{ - fn from_locked_optional_component( - optional_component: Option<&'component crate::lock::Lock>>, - world: &'component World, - ) -> Result - { - Ok(Self::from_optional_mut_component( - optional_component.map(Lock::write_nonblock).transpose()?, - world, - )) - } -} + }); -impl<'comp, ComponentT> FromOptionalMutComponent<'comp> - for Option> -where - ComponentT: Component, -{ - fn from_optional_mut_component( - optional_component: Option>>, - _world: &'comp World, - ) -> Self - { - optional_component.map(|component| ComponentRefMut::new(component)) + Ok(Self::new(component.write_nonblock()?)) } } @@ -327,49 +295,22 @@ impl<'a, ComponentT: Component> ComponentRef<'a, ComponentT> } } -impl<'component, ComponentT: Component> FromOptionalComponent<'component> +impl<'component, ComponentT: Component> FromLockedOptionalComponent<'component> for ComponentRef<'component, ComponentT> { - fn from_optional_component( - inner: Option>>, + fn from_locked_optional_component( + optional_component: Option<&'component crate::lock::Lock>>, _world: &'component World, - ) -> Self + ) -> Result { - Self::new(inner.unwrap_or_else(|| { + let component = optional_component.unwrap_or_else(|| { panic!( "Component {} was not found in entity", type_name::() ); - })) - } -} - -impl<'component, ComponentT: Component> FromLockedOptionalComponent<'component> - for ComponentRef<'component, ComponentT> -{ - fn from_locked_optional_component( - optional_component: Option<&'component crate::lock::Lock>>, - world: &'component World, - ) -> Result - { - Ok(Self::from_optional_component( - optional_component.map(Lock::read_nonblock).transpose()?, - world, - )) - } -} + }); -impl<'comp, ComponentT> FromOptionalComponent<'comp> - for Option> -where - ComponentT: Component, -{ - fn from_optional_component( - optional_component: Option>>, - _world: &'comp World, - ) -> Self - { - optional_component.map(|component| ComponentRef::new(component)) + Ok(Self::new(component.read_nonblock()?)) } } -- cgit v1.2.3-18-g5258