summaryrefslogtreecommitdiff
path: root/engine-ecs/src
diff options
context:
space:
mode:
Diffstat (limited to 'engine-ecs/src')
-rw-r--r--engine-ecs/src/component.rs17
-rw-r--r--engine-ecs/src/component/storage.rs8
-rw-r--r--engine-ecs/src/component/storage/archetype.rs13
-rw-r--r--engine-ecs/src/lib.rs36
-rw-r--r--engine-ecs/src/lock.rs132
-rw-r--r--engine-ecs/src/pair.rs19
-rw-r--r--engine-ecs/src/sole.rs3
7 files changed, 53 insertions, 175 deletions
diff --git a/engine-ecs/src/component.rs b/engine-ecs/src/component.rs
index d424e16..3e93930 100644
--- a/engine-ecs/src/component.rs
+++ b/engine-ecs/src/component.rs
@@ -1,4 +1,5 @@
use std::any::{Any, TypeId};
+use std::borrow::Cow;
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
@@ -32,7 +33,7 @@ pub trait Component: WithAssociatedId + SystemInput + Any + IntoParts
Self: Sized;
/// Returns the name of this component.
- fn name(&self) -> &'static str;
+ fn name() -> Cow<'static, str>;
}
pub trait WithAssociatedId
@@ -262,7 +263,7 @@ pub trait IntoParts
pub struct Parts
{
pub id: Uid,
- pub name: &'static str,
+ pub name: Cow<'static, str>,
pub is_sole: bool,
pub type_reflection: Option<&'static TypeReflection>,
pub data: Box<dyn Any>,
@@ -286,7 +287,7 @@ impl Parts
Self::builder()
.type_reflection(component_info.type_reflection)
- .name(component_info.name)
+ .name(component_info.name.clone())
.is_sole(component_info.is_sole)
.build_with_any_data(id, data)
}
@@ -309,7 +310,7 @@ impl IntoParts for Parts
#[derive(Debug)]
pub struct PartsBuilder
{
- name: &'static str,
+ name: Cow<'static, str>,
is_sole: bool,
type_reflection: Option<&'static TypeReflection>,
pair_relation_metadata: Option<PairMemberMetadata>,
@@ -319,9 +320,9 @@ pub struct PartsBuilder
impl PartsBuilder
{
#[must_use]
- pub fn name(mut self, name: &'static str) -> Self
+ pub fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self
{
- self.name = name;
+ self.name = name.into();
self
}
@@ -391,7 +392,7 @@ impl Default for PartsBuilder
fn default() -> Self
{
Self {
- name: "(unspecified)",
+ name: "<unspecified>".into(),
is_sole: false,
type_reflection: None,
pair_relation_metadata: None,
@@ -406,7 +407,7 @@ pub struct Info
{
pub type_reflection: Option<&'static TypeReflection>,
pub ty_id: TypeId,
- pub name: &'static str,
+ pub name: Cow<'static, str>,
pub is_sole: bool,
}
diff --git a/engine-ecs/src/component/storage.rs b/engine-ecs/src/component/storage.rs
index 98f8d46..7a49110 100644
--- a/engine-ecs/src/component/storage.rs
+++ b/engine-ecs/src/component/storage.rs
@@ -261,7 +261,7 @@ impl Storage
pub fn add_entity_component(
&mut self,
entity_uid: Uid,
- (component_id, component_name, component): (Uid, &'static str, Box<dyn Any>),
+ (component_id, component): (Uid, Box<dyn Any>),
) -> Result<(), Error>
{
let Some(archetype_id) = self.entity_archetype_lookup.get(&entity_uid) else {
@@ -334,7 +334,7 @@ impl Storage
entity.insert_component(
component_id,
- ArchetypeEntityComponent::new(component, component_name),
+ ArchetypeEntityComponent::new(component),
add_edge_archetype,
);
@@ -403,8 +403,7 @@ impl Storage
.remove_entity(entity_uid)
.expect("Entity should exist in archetype");
- let removed_component =
- entity.remove_component(component_id, archetype_node.archetype());
+ entity.remove_component(component_id, archetype_node.archetype());
self.graph
.get_node_by_id_mut(remove_edge_id)
@@ -418,7 +417,6 @@ impl Storage
tracing::debug!(
entity_id = %entity_uid,
component_id = %component_id,
- component_name = removed_component.name(),
"Removed component from entity"
);
diff --git a/engine-ecs/src/component/storage/archetype.rs b/engine-ecs/src/component/storage/archetype.rs
index 480a1da..64ab587 100644
--- a/engine-ecs/src/component/storage/archetype.rs
+++ b/engine-ecs/src/component/storage/archetype.rs
@@ -328,28 +328,19 @@ impl Entity
pub struct EntityComponent
{
component: Lock<Box<dyn Any>>,
- name: &'static str,
}
impl EntityComponent
{
- pub fn new(component: Box<dyn Any>, component_name: &'static str) -> Self
+ pub fn new(component: Box<dyn Any>) -> Self
{
- Self {
- component: Lock::new(component, component_name),
- name: component_name,
- }
+ Self { component: Lock::new(component) }
}
pub fn component(&self) -> &Lock<Box<dyn Any>>
{
&self.component
}
-
- pub fn name(&self) -> &str
- {
- self.name
- }
}
/// Archetype ID.
diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs
index 7965bcd..397605c 100644
--- a/engine-ecs/src/lib.rs
+++ b/engine-ecs/src/lib.rs
@@ -246,13 +246,11 @@ impl World
return Err(SoleAlreadyExistsError);
}
- let name = sole.name();
-
self.create_ent(
SoleT::id(),
[
sole.into_parts(),
- EntityName { name: name.into() }.into_parts(),
+ EntityName { name: SoleT::name() }.into_parts(),
crate::sole::Is.into_parts(),
],
&ComponentAddingFlags { allow_soles: true },
@@ -626,13 +624,13 @@ impl World
self.data.component_storage.create_entity(sole_parts.id)
{
tracing::error!(
- name = sole_parts.name,
+ name = %sole_parts.name,
"Can not create sole that already exists"
);
continue;
}
- let name = sole_parts.name;
+ let name = sole_parts.name.clone();
Self::add_entity_components(
sole_parts.id,
@@ -674,10 +672,9 @@ impl World
let comp_name = component_parts.name;
let comp_type_id = (*component_parts.data).type_id();
- if let Err(err) = component_storage.add_entity_component(
- entity_uid,
- (comp_id, comp_name, component_parts.data),
- ) {
+ if let Err(err) = component_storage
+ .add_entity_component(entity_uid, (comp_id, component_parts.data))
+ {
let ent_name = Self::get_component_of_entity::<EntityName>(
component_storage,
entity_uid,
@@ -687,7 +684,7 @@ impl World
entity = %entity_uid,
entity_name = ent_name.as_ref().map(|ent_name| ent_name.name.as_ref()),
component = %comp_id,
- component_name = comp_name,
+ component_name = %comp_name,
"Failed to add component to entity: {err}"
);
@@ -696,7 +693,7 @@ impl World
if comp_id.is_pair() {
if let Some(pair_relation_metadata) =
- &component_parts.pair_relation_metadata
+ component_parts.pair_relation_metadata
{
Self::create_component_info_entity_if_missing(
component_storage,
@@ -710,8 +707,7 @@ impl World
);
}
- if let Some(pair_target_metadata) = &component_parts.pair_target_metadata
- {
+ if let Some(pair_target_metadata) = component_parts.pair_target_metadata {
Self::create_component_info_entity_if_missing(
component_storage,
comp_id.target(),
@@ -767,14 +763,9 @@ impl World
let comp_info_parts = comp_info.into_parts();
- match component_storage.add_entity_component(
- comp_id,
- (
- comp_info_parts.id,
- comp_info_parts.name,
- comp_info_parts.data,
- ),
- ) {
+ match component_storage
+ .add_entity_component(comp_id, (comp_info_parts.id, comp_info_parts.data))
+ {
Ok(()) => {}
Err(ComponentStorageError::EntityDoesNotExist(_)) => {
// Entity is created if it does not exist
@@ -819,7 +810,8 @@ impl World
component = %component_id,
component_name = component_info
.as_ref()
- .map(|component_info| component_info.name),
+ .map(|comp_info| comp_info.name.as_ref())
+ .map(tracing::field::display),
"Failed to remove component from entity: {err}"
);
}
diff --git a/engine-ecs/src/lock.rs b/engine-ecs/src/lock.rs
index 685eb8f..275ad12 100644
--- a/engine-ecs/src/lock.rs
+++ b/engine-ecs/src/lock.rs
@@ -1,5 +1,3 @@
-use std::any::type_name;
-use std::mem::forget;
use std::ops::{Deref, DerefMut};
use parking_lot::{
@@ -14,17 +12,13 @@ use parking_lot::{
pub struct Lock<Value>
{
inner: RwLock<Value>,
- value_type_name: &'static str,
}
impl<Value> Lock<Value>
{
- pub fn new(value: Value, value_type_name: &'static str) -> Self
+ pub fn new(value: Value) -> Self
{
- Self {
- inner: RwLock::new(value),
- value_type_name,
- }
+ Self { inner: RwLock::new(value) }
}
/// Tries to a acquire a handle to the resource with read access.
@@ -35,12 +29,7 @@ impl<Value> Lock<Value>
{
let guard = self.inner.try_read().ok_or(Error::ReadUnavailable)?;
- tracing::trace!("Acquired lock to value of type {}", self.value_type_name);
-
- Ok(ReadGuard {
- inner: guard,
- value_type_name: self.value_type_name,
- })
+ Ok(ReadGuard { inner: guard })
}
/// Tries to a acquire a handle to the resource with mutable access.
@@ -51,15 +40,7 @@ impl<Value> Lock<Value>
{
let guard = self.inner.try_write().ok_or(Error::WriteUnavailable)?;
- tracing::trace!(
- "Acquired mutable lock to value of type {}",
- self.value_type_name
- );
-
- Ok(WriteGuard {
- inner: guard,
- value_type_name: self.value_type_name,
- })
+ Ok(WriteGuard { inner: guard })
}
}
@@ -67,7 +48,7 @@ impl<Value: Default + 'static> Default for Lock<Value>
{
fn default() -> Self
{
- Self::new(Value::default(), type_name::<Value>())
+ Self::new(Value::default())
}
}
@@ -85,7 +66,6 @@ pub enum Error
pub struct ReadGuard<'guard, Value: ?Sized>
{
inner: RwLockReadGuard<'guard, Value>,
- value_type_name: &'static str,
}
impl<'guard, Value: ?Sized> ReadGuard<'guard, Value>
@@ -95,17 +75,8 @@ impl<'guard, Value: ?Sized> ReadGuard<'guard, Value>
func: impl FnOnce(&Value) -> &NewValue,
) -> MappedReadGuard<'guard, NewValue>
{
- let value_type_name = this.value_type_name;
-
- // The 'inner' field cannot be moved out of ReadGuard in a normal way since
- // ReadGuard implements Drop
- let inner = unsafe { std::ptr::read(&raw const this.inner) };
-
- forget(this);
-
MappedReadGuard {
- inner: RwLockReadGuard::map(inner, func),
- value_type_name,
+ inner: RwLockReadGuard::map(this.inner, func),
}
}
@@ -114,21 +85,9 @@ impl<'guard, Value: ?Sized> ReadGuard<'guard, Value>
func: impl FnOnce(&Value) -> Option<&NewValue>,
) -> Result<MappedReadGuard<'guard, NewValue>, Self>
{
- let value_type_name = this.value_type_name;
-
- // The 'inner' field cannot be moved out of ReadGuard in a normal way since
- // ReadGuard implements Drop
- let inner = unsafe { std::ptr::read(&raw const this.inner) };
- forget(this);
-
- match RwLockReadGuard::try_map(inner, func) {
- Ok(mapped_guard) => {
- Ok(MappedReadGuard { inner: mapped_guard, value_type_name })
- }
- Err(unmapped_guard) => Err(Self {
- inner: unmapped_guard,
- value_type_name,
- }),
+ match RwLockReadGuard::try_map(this.inner, func) {
+ Ok(mapped_guard) => Ok(MappedReadGuard { inner: mapped_guard }),
+ Err(unmapped_guard) => Err(Self { inner: unmapped_guard }),
}
}
}
@@ -143,19 +102,10 @@ impl<Value: ?Sized> Deref for ReadGuard<'_, Value>
}
}
-impl<Value: ?Sized> Drop for ReadGuard<'_, Value>
-{
- fn drop(&mut self)
- {
- tracing::trace!("Dropped lock to value of type {}", self.value_type_name);
- }
-}
-
#[derive(Debug)]
pub struct MappedReadGuard<'guard, Value: ?Sized>
{
inner: MappedRwLockReadGuard<'guard, Value>,
- value_type_name: &'static str,
}
impl<Value: ?Sized> Deref for MappedReadGuard<'_, Value>
@@ -168,22 +118,10 @@ impl<Value: ?Sized> Deref for MappedReadGuard<'_, Value>
}
}
-impl<Value: ?Sized> Drop for MappedReadGuard<'_, Value>
-{
- fn drop(&mut self)
- {
- tracing::trace!(
- "Dropped mapped lock to value of type {}",
- self.value_type_name
- );
- }
-}
-
#[derive(Debug)]
pub struct WriteGuard<'guard, Value: ?Sized>
{
inner: RwLockWriteGuard<'guard, Value>,
- value_type_name: &'static str,
}
impl<'guard, Value: ?Sized> WriteGuard<'guard, Value>
@@ -193,17 +131,8 @@ impl<'guard, Value: ?Sized> WriteGuard<'guard, Value>
func: impl FnOnce(&mut Value) -> &mut NewValue,
) -> MappedWriteGuard<'guard, NewValue>
{
- let value_type_name = this.value_type_name;
-
- // The 'inner' field cannot be moved out of ReadGuard in a normal way since
- // ReadGuard implements Drop
- let inner = unsafe { std::ptr::read(&raw const this.inner) };
-
- forget(this);
-
MappedWriteGuard {
- inner: RwLockWriteGuard::map(inner, func),
- value_type_name,
+ inner: RwLockWriteGuard::map(this.inner, func),
}
}
@@ -212,21 +141,9 @@ impl<'guard, Value: ?Sized> WriteGuard<'guard, Value>
func: impl FnOnce(&mut Value) -> Option<&mut NewValue>,
) -> Result<MappedWriteGuard<'guard, NewValue>, Self>
{
- let value_type_name = this.value_type_name;
-
- // The 'inner' field cannot be moved out of ReadGuard in a normal way since
- // ReadGuard implements Drop
- let inner = unsafe { std::ptr::read(&raw const this.inner) };
- forget(this);
-
- match RwLockWriteGuard::try_map(inner, func) {
- Ok(mapped_guard) => {
- Ok(MappedWriteGuard { inner: mapped_guard, value_type_name })
- }
- Err(unmapped_guard) => Err(Self {
- inner: unmapped_guard,
- value_type_name,
- }),
+ match RwLockWriteGuard::try_map(this.inner, func) {
+ Ok(mapped_guard) => Ok(MappedWriteGuard { inner: mapped_guard }),
+ Err(unmapped_guard) => Err(Self { inner: unmapped_guard }),
}
}
}
@@ -249,22 +166,10 @@ impl<Value: ?Sized> DerefMut for WriteGuard<'_, Value>
}
}
-impl<Value: ?Sized> Drop for WriteGuard<'_, Value>
-{
- fn drop(&mut self)
- {
- tracing::trace!(
- "Dropped mutable lock to value of type {}",
- self.value_type_name
- );
- }
-}
-
#[derive(Debug)]
pub struct MappedWriteGuard<'guard, Value: ?Sized>
{
inner: MappedRwLockWriteGuard<'guard, Value>,
- value_type_name: &'static str,
}
impl<Value: ?Sized> Deref for MappedWriteGuard<'_, Value>
@@ -284,14 +189,3 @@ impl<Value: ?Sized> DerefMut for MappedWriteGuard<'_, Value>
&mut self.inner
}
}
-
-impl<Value: ?Sized> Drop for MappedWriteGuard<'_, Value>
-{
- fn drop(&mut self)
- {
- tracing::trace!(
- "Dropped mapped mutable lock to value of type {}",
- self.value_type_name
- );
- }
-}
diff --git a/engine-ecs/src/pair.rs b/engine-ecs/src/pair.rs
index efeead9..296e9cc 100644
--- a/engine-ecs/src/pair.rs
+++ b/engine-ecs/src/pair.rs
@@ -1,4 +1,5 @@
use std::any::{type_name, TypeId};
+use std::borrow::Cow;
use std::convert::Infallible;
use std::marker::PhantomData;
@@ -47,7 +48,7 @@ impl<Relation, Target> Builder<Relation, Target>
relation_metadata: Some(MemberMetadata {
ty: NewRelation::type_reflection(),
ty_id: TypeId::of::<NewRelation>(),
- name: type_name::<NewRelation>(),
+ name: NewRelation::name(),
is_sole: false,
}),
target_metadata: self.target_metadata,
@@ -73,7 +74,7 @@ impl<Relation, Target> Builder<Relation, Target>
target_metadata: Some(MemberMetadata {
ty: NewTarget::type_reflection(),
ty_id: TypeId::of::<NewTarget>(),
- name: type_name::<NewTarget>(),
+ name: NewTarget::name(),
is_sole: false,
}),
}
@@ -106,7 +107,7 @@ impl_multiple!(
target_metadata: Some(MemberMetadata {
ty: NewTarget::type_reflection(),
ty_id: TypeId::of::<NewTarget>(),
- name: type_name::<NewTarget>(),
+ name: NewTarget::name(),
is_sole: false,
}),
}
@@ -129,7 +130,7 @@ impl_multiple!(
relation_metadata: Some(MemberMetadata {
ty: NewRelation::type_reflection(),
ty_id: TypeId::of::<NewRelation>(),
- name: type_name::<NewRelation>(),
+ name: NewRelation::name(),
is_sole: false,
}),
target_metadata: self.target_metadata,
@@ -289,7 +290,7 @@ where
Err(err) => {
panic!(
"Creating handle to target component {} failed: {err}",
- type_name::<Target>()
+ Target::name()
);
}
};
@@ -334,7 +335,7 @@ where
Err(err) => {
panic!(
"Creating mut handle to target component {} failed: {err}",
- type_name::<Target>()
+ Target::name()
);
}
};
@@ -597,7 +598,7 @@ where
err @ ComponentHandleError::AcquireLockFailed(_) => {
panic!(
"Creating handle to pair data as component {} failed: {err}",
- type_name::<Relation>()
+ Relation::name()
);
}
},
@@ -623,7 +624,7 @@ where
err @ ComponentHandleError::AcquireLockFailed(_) => {
panic!(
"Creating handle to pair data as component {} failed: {err}",
- type_name::<Relation>()
+ Relation::name()
);
}
},
@@ -784,7 +785,7 @@ pub struct MemberMetadata
{
pub ty: Option<&'static TypeReflection>,
pub ty_id: TypeId,
- pub name: &'static str,
+ pub name: Cow<'static, str>,
pub is_sole: bool,
}
diff --git a/engine-ecs/src/sole.rs b/engine-ecs/src/sole.rs
index cf5283b..ee1da7a 100644
--- a/engine-ecs/src/sole.rs
+++ b/engine-ecs/src/sole.rs
@@ -1,4 +1,5 @@
use std::any::{type_name, Any};
+use std::borrow::Cow;
use std::fmt::Debug;
use crate::component::{
@@ -17,7 +18,7 @@ pub trait Sole: Any + IntoComponentParts + ComponentWithAssociatedId
Self: Sized;
/// Returns the name of this component.
- fn name(&self) -> &'static str;
+ fn name() -> Cow<'static, str>;
}
/// Marker component for the entities of soles.