summaryrefslogtreecommitdiff
path: root/ecs/src/component.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-03-29 14:20:21 +0100
committerHampusM <hampus@hampusmat.com>2024-03-29 14:20:21 +0100
commit61dfcf1ba2049bf0375821652e49b0e4c4147623 (patch)
tree3eaa2624523c893cb12b08595c4dd9e5250728c4 /ecs/src/component.rs
parent96ba84e42c77147d297b358c944a48fee3785ae1 (diff)
feat(ecs): make World unwind safe
Diffstat (limited to 'ecs/src/component.rs')
-rw-r--r--ecs/src/component.rs36
1 files changed, 22 insertions, 14 deletions
diff --git a/ecs/src/component.rs b/ecs/src/component.rs
index 07701c8..120ba30 100644
--- a/ecs/src/component.rs
+++ b/ecs/src/component.rs
@@ -1,19 +1,20 @@
use std::any::{Any, TypeId};
-use std::cell::{RefCell, RefMut};
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use seq_macro::seq;
+use crate::lock::{Lock, WriteGuard};
use crate::system::{
ComponentRefMut,
Input as SystemInput,
Param as SystemParam,
System,
};
+use crate::type_name::TypeName;
use crate::WorldData;
-pub trait Component: SystemInput + Any
+pub trait Component: SystemInput + Any + TypeName
{
#[doc(hidden)]
fn as_any_mut(&mut self) -> &mut dyn Any;
@@ -48,6 +49,14 @@ impl Debug for dyn Component
}
}
+impl TypeName for Box<dyn Component>
+{
+ fn type_name(&self) -> &'static str
+ {
+ self.as_ref().type_name()
+ }
+}
+
/// A sequence of components.
pub trait Sequence
{
@@ -59,7 +68,9 @@ pub trait Sequence
fn type_ids() -> Vec<TypeId>;
- fn from_components(components: &[RefCell<Box<dyn Component>>]) -> Self::Refs<'_>;
+ fn from_components<'component>(
+ components: impl Iterator<Item = &'component Lock<Box<dyn Component>>>,
+ ) -> Self::Refs<'component>;
}
macro_rules! inner {
@@ -83,18 +94,18 @@ macro_rules! inner {
]
}
- fn from_components(
- components: &[RefCell<Box<dyn Component>>]
- ) -> Self::Refs<'_>
+ fn from_components<'component>(
+ components: impl Iterator<Item = &'component Lock<Box<dyn Component>>>,
+ ) -> Self::Refs<'component>
{
#(
- let mut comp_~I: Option<RefMut<Box<dyn Component>>> = None;
+ let mut comp_~I: Option<WriteGuard<Box<dyn Component>>> = None;
)*
for comp in components {
- let Ok(comp_ref) = comp.try_borrow_mut() else {
- continue;
- };
+ let comp_ref = comp
+ .write_nonblock()
+ .expect("Failed to acquire read-write component lock");
#(
if comp_ref.is::<Comp~I>() {
@@ -106,10 +117,7 @@ macro_rules! inner {
(#(
ComponentRefMut::new(
- RefMut::filter_map(
- comp_~I.unwrap(),
- |component| component.downcast_mut::<Comp~I>()
- ).expect("Failed to downcast component")
+ comp_~I.unwrap(),
),
)*)
}