diff options
| author | HampusM <hampus@hampusmat.com> | 2026-11-29 21:16:11 +0100 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-11-29 21:16:11 +0100 |
| commit | 3c117bcd9455ffbcec5cd59dab37db0786e15e02 (patch) | |
| tree | 0355ba259b51b3d5c9eb7c74cf395461517231fa /engine-ecs/src/lock.rs | |
| parent | c416eca3cccac2c25ee395469005faca01fdb912 (diff) | |
feat(engine-ecs): change meaning of component name to type identifier
Diffstat (limited to 'engine-ecs/src/lock.rs')
| -rw-r--r-- | engine-ecs/src/lock.rs | 132 |
1 files changed, 13 insertions, 119 deletions
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 - ); - } -} |
