From 9faa8b8f530f3640e1a604a4888cc3fa7beafd5f Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 7 Apr 2025 19:19:31 +0200 Subject: refactor(ecs): remove TypeName trait --- ecs/src/component.rs | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) (limited to 'ecs/src/component.rs') diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 525bd98..9ec962d 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -18,7 +18,6 @@ use crate::lock::{ WriteGuard, }; use crate::system::Input as SystemInput; -use crate::type_name::TypeName; use crate::uid::Uid; use crate::util::Array; use crate::World; @@ -27,7 +26,7 @@ pub mod local; pub(crate) mod storage; -pub trait Component: SystemInput + Any + TypeName +pub trait Component: SystemInput + Any { /// The component type in question. Will usually be `Self` type Component: Component @@ -47,6 +46,9 @@ pub trait Component: SystemInput + Any + TypeName where Self: Sized; + /// Returns the name of this component. + fn name(&self) -> &'static str; + /// Returns the component UID of a component event for this component. fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid; @@ -92,14 +94,6 @@ impl Debug for dyn Component } } -impl TypeName for Box -{ - fn type_name(&self) -> &'static str - { - self.as_ref().type_name() - } -} - impl Component for Option where ComponentT: Component, @@ -115,6 +109,11 @@ where ComponentT::id() } + fn name(&self) -> &'static str + { + type_name::() + } + fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid { match event_kind { @@ -133,16 +132,6 @@ where } } -impl TypeName for Option -where - ComponentT: Component, -{ - fn type_name(&self) -> &'static str - { - type_name::() - } -} - impl SystemInput for Option where ComponentT: Component {} /// A sequence of components. @@ -238,8 +227,7 @@ impl<'a, ComponentT: Component> Handle<'a, ComponentT> inner: inner.map(|component| { component.downcast_ref::().unwrap_or_else(|| { panic!( - "Cannot downcast component {} to type {}", - component.type_name(), + "Failed to downcast component to type {}", type_name::() ); }) @@ -304,11 +292,9 @@ impl<'a, ComponentT: Component> HandleMut<'a, ComponentT> { Self { inner: inner.map(|component| { - let component_type_name = component.type_name(); - component.downcast_mut::().unwrap_or_else(|| { panic!( - "Cannot downcast component {component_type_name} to type {}", + "Failed to downcast component to type {}", type_name::() ); }) -- cgit v1.2.3-18-g5258