summaryrefslogtreecommitdiff
path: root/ecs/src/component.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-04-07 19:19:31 +0200
committerHampusM <hampus@hampusmat.com>2025-04-07 19:19:31 +0200
commit9faa8b8f530f3640e1a604a4888cc3fa7beafd5f (patch)
tree004e75803ac71696db7546676b166875899c58b0 /ecs/src/component.rs
parentf67ccdd0e10cfde6851610d5d96008c8a0a50ab6 (diff)
refactor(ecs): remove TypeName trait
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r--ecs/src/component.rs36
1 files changed, 11 insertions, 25 deletions
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<dyn Component>
-{
- fn type_name(&self) -> &'static str
- {
- self.as_ref().type_name()
- }
-}
-
impl<ComponentT> Component for Option<ComponentT>
where
ComponentT: Component,
@@ -115,6 +109,11 @@ where
ComponentT::id()
}
+ fn name(&self) -> &'static str
+ {
+ type_name::<Self>()
+ }
+
fn get_event_uid(&self, event_kind: ComponentEventKind) -> Uid
{
match event_kind {
@@ -133,16 +132,6 @@ where
}
}
-impl<ComponentT> TypeName for Option<ComponentT>
-where
- ComponentT: Component,
-{
- fn type_name(&self) -> &'static str
- {
- type_name::<Self>()
- }
-}
-
impl<ComponentT> SystemInput for Option<ComponentT> 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::<ComponentT>().unwrap_or_else(|| {
panic!(
- "Cannot downcast component {} to type {}",
- component.type_name(),
+ "Failed to downcast component to type {}",
type_name::<ComponentT>()
);
})
@@ -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::<ComponentT>().unwrap_or_else(|| {
panic!(
- "Cannot downcast component {component_type_name} to type {}",
+ "Failed to downcast component to type {}",
type_name::<ComponentT>()
);
})