diff options
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r-- | ecs/src/component.rs | 110 |
1 files changed, 40 insertions, 70 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs index 4bf38e7..6fb1230 100644 --- a/ecs/src/component.rs +++ b/ecs/src/component.rs @@ -1,5 +1,4 @@ use std::any::{type_name, Any}; -use std::error::Error; use std::fmt::Debug; use std::ops::{Deref, DerefMut}; @@ -15,7 +14,7 @@ use crate::lock::{ use crate::system::Input as SystemInput; use crate::uid::Uid; use crate::util::Array; -use crate::{EntityComponentRef, World}; +use crate::EntityComponentRef; pub mod local; @@ -69,29 +68,31 @@ pub trait Sequence fn into_parts_array(self) -> Self::PartsArray; } -pub trait HandleFromEntityComponentRef<'comp>: Sized -{ - type Error: Error; - - /// Creates a new handle instance from a [`EntityComponentRef`]. - /// - /// # Errors - /// See the implementation's [`Self::Error`] type. - fn from_entity_component_ref( - entity_component_ref: Option<EntityComponentRef<'comp>>, - world: &'comp World, - ) -> Result<Self, Self::Error>; -} - #[derive(Debug)] pub struct Handle<'a, ComponentData: 'static> { inner: MappedReadGuard<'a, ComponentData>, } -impl<'a, ComponentData: 'static> Handle<'a, ComponentData> +impl<'comp, ComponentData: 'static> Handle<'comp, ComponentData> { - pub(crate) fn new(inner: ReadGuard<'a, Box<dyn Any>>) -> Self + /// Creates a new handle instance from a [`EntityComponentRef`]. + /// + /// # Errors + /// Will return `Err` if acquiring the component's lock fails. + pub fn from_entity_component_ref( + entity_component_ref: EntityComponentRef<'comp>, + ) -> Result<Self, HandleError> + { + Ok(Self::new( + entity_component_ref + .component() + .read_nonblock() + .map_err(AcquireLockError)?, + )) + } + + pub(crate) fn new(inner: ReadGuard<'comp, Box<dyn Any>>) -> Self { Self { inner: inner.map(|component| { @@ -108,28 +109,6 @@ impl<'a, ComponentData: 'static> Handle<'a, ComponentData> } } -impl<'comp, ComponentData: 'static> HandleFromEntityComponentRef<'comp> - for Handle<'comp, ComponentData> -{ - type Error = HandleError; - - fn from_entity_component_ref( - entity_component_ref: Option<EntityComponentRef<'comp>>, - _world: &'comp World, - ) -> Result<Self, Self::Error> - { - let entity_comp = - entity_component_ref.ok_or(HandleError::ComponentDoesNotExist)?; - - Ok(Self::new( - entity_comp - .component() - .read_nonblock() - .map_err(AcquireComponentLockFailed)?, - )) - } -} - impl<ComponentData: 'static> Deref for Handle<'_, ComponentData> { type Target = ComponentData; @@ -146,9 +125,25 @@ pub struct HandleMut<'a, ComponentData: 'static> inner: MappedWriteGuard<'a, ComponentData>, } -impl<'a, ComponentData: 'static> HandleMut<'a, ComponentData> +impl<'comp, ComponentData: 'static> HandleMut<'comp, ComponentData> { - pub(crate) fn new(inner: WriteGuard<'a, Box<dyn Any>>) -> Self + /// Creates a new handle instance from a [`EntityComponentRef`]. + /// + /// # Errors + /// Will return `Err` if acquiring the component's lock fails. + pub fn from_entity_component_ref( + entity_component_ref: EntityComponentRef<'comp>, + ) -> Result<Self, HandleError> + { + Ok(Self::new( + entity_component_ref + .component() + .write_nonblock() + .map_err(AcquireLockError)?, + )) + } + + pub(crate) fn new(inner: WriteGuard<'comp, Box<dyn Any>>) -> Self { Self { inner: inner.map(|component| { @@ -165,28 +160,6 @@ impl<'a, ComponentData: 'static> HandleMut<'a, ComponentData> } } -impl<'comp, ComponentData: 'static> HandleFromEntityComponentRef<'comp> - for HandleMut<'comp, ComponentData> -{ - type Error = HandleError; - - fn from_entity_component_ref( - entity_component_ref: Option<EntityComponentRef<'comp>>, - _world: &'comp World, - ) -> Result<Self, Self::Error> - { - let entity_comp = - entity_component_ref.ok_or(HandleError::ComponentDoesNotExist)?; - - Ok(Self::new( - entity_comp - .component() - .write_nonblock() - .map_err(AcquireComponentLockFailed)?, - )) - } -} - impl<ComponentData: 'static> Deref for HandleMut<'_, ComponentData> { type Target = ComponentData; @@ -209,15 +182,12 @@ impl<ComponentData: 'static> DerefMut for HandleMut<'_, ComponentData> pub enum HandleError { #[error(transparent)] - AcquireComponentLockFailed(#[from] AcquireComponentLockFailed), - - #[error("Component does not exist")] - ComponentDoesNotExist, + AcquireLockFailed(#[from] AcquireLockError), } #[derive(Debug, thiserror::Error)] -#[error(transparent)] -pub struct AcquireComponentLockFailed(LockError); +#[error("Failed to acquire component lock")] +pub struct AcquireLockError(#[source] LockError); macro_rules! inner { ($c: tt) => { |