diff --git a/Cargo.toml b/Cargo.toml index 291a7b7..6d4240c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = ["ecs", "update_macros"] [workspace.dependencies] anyhow = { version = "1.0.86", features = ["backtrace"] } -indexmap = { version = "2.2.6", features = ["rayon"] } +indexmap = { version = "2.9.0", features = ["rayon"] } serde = { version = "1.0.203", features = ["derive"] } paste = "1.0.15" ron = "0.9.0" diff --git a/ecs/src/entity.rs b/ecs/src/entity.rs index c007ca8..dbb6b1e 100644 --- a/ecs/src/entity.rs +++ b/ecs/src/entity.rs @@ -1,10 +1,8 @@ use core::fmt; use std::any::TypeId; -use std::collections::HashSet; use std::{num::ParseIntError, str::FromStr}; use anyhow::Result; -use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use crate::{ComponentDebug, ComponentNotFoundError, EntityComponent, TypeMap}; @@ -28,7 +26,7 @@ impl std::fmt::Display for EntityNotFoundError { impl std::error::Error for EntityNotFoundError {} -#[derive(Default)] +#[derive(Default, Debug)] pub struct ActivationState { activated: bool, } @@ -67,6 +65,22 @@ pub struct EntityObject { component_names: Vec, } +impl std::fmt::Debug for EntityObject { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut t = f.debug_struct("EntityObject"); + + t.field("debug_name", &self.debug_name) + .field("gltf_file", &self.gltf_file) + .field("activation_state", &self.activation_state) + .field("entity_id", &self.entity_id); + + #[cfg(debug_assertions)] + t.field("component_names", &self.component_names); + + t.finish() + } +} + unsafe impl Send for EntityObject {} unsafe impl Sync for EntityObject {} @@ -310,40 +324,3 @@ impl FromStr for Entity { Ok(Self { id: s.parse()? }) } } - -pub struct EntityMultiMut<'a> { - entities: &'a mut IndexMap, - buffer: HashSet, -} - -impl<'a> EntityMultiMut<'a> { - pub(crate) fn new(entities: &'a mut IndexMap) -> Self { - Self { - entities, - buffer: HashSet::new(), - } - } - - pub fn get( - &mut self, - entity: Entity, - ) -> std::result::Result<&'a mut EntityObject, EntityNotFoundError> { - match self.entities.get_mut(&entity) { - Some(v) => { - let ptr = v as *mut EntityObject; - - assert!( - self.buffer.get(&entity).is_none(), - "Entity ({}) already borrowed", - entity.id - ); - self.buffer.insert(entity); - - let e: Option<&'a mut EntityObject> = Some(unsafe { &mut *ptr }); - - e.ok_or_else(|| EntityNotFoundError::new(entity)) - } - None => Err(EntityNotFoundError::new(entity)), - } - } -} diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index ef34ea7..f916a02 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -8,9 +8,7 @@ mod unsafe_component_store; mod updates; mod world; -pub use crate::entity::{ - Entity, EntityComponentDisjointMut, EntityMultiMut, EntityNotFoundError, EntityObject, -}; +pub use crate::entity::{Entity, EntityComponentDisjointMut, EntityNotFoundError, EntityObject}; pub use crate::events::Events; pub use crate::resources::Resources; pub use crate::type_map::{ diff --git a/ecs/src/world.rs b/ecs/src/world.rs index e9d1b41..3936cb2 100644 --- a/ecs/src/world.rs +++ b/ecs/src/world.rs @@ -166,8 +166,19 @@ impl World { .map(|e| unsafe { remove_life_time_mut(e) }) } - pub fn entities_multi_mut(&mut self) -> EntityMultiMut<'_> { - EntityMultiMut::new(&mut self.entities) + pub fn entities_mut( + &mut self, + entities: [Entity; N], + ) -> std::result::Result<[&mut EntityObject; N], EntityNotFoundError> { + Ok(self + .entities + .get_disjoint_mut::(entities.iter().collect::>().try_into().unwrap()) + .into_iter() + .enumerate() + .map(|(i, entity_opt)| entity_opt.ok_or(EntityNotFoundError::new(entities[i]))) + .collect::, _>>()? + .try_into() + .unwrap()) } pub fn add_entity(&mut self, entity_object: EntityObject) -> Result {