From 7b3374ad9585f78c60e1b158126ab54384a83f32 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 20 Jun 2026 16:01:00 +0200 Subject: refactor(engine-ecs): store soles as components --- engine-ecs/src/lib.rs | 87 +++++---------------------------------------------- 1 file changed, 8 insertions(+), 79 deletions(-) (limited to 'engine-ecs/src/lib.rs') diff --git a/engine-ecs/src/lib.rs b/engine-ecs/src/lib.rs index 1c157e3..25866ca 100644 --- a/engine-ecs/src/lib.rs +++ b/engine-ecs/src/lib.rs @@ -1,14 +1,10 @@ #![deny(clippy::all, clippy::pedantic)] -use std::any::{type_name, Any, TypeId}; +use std::any::Any; use std::fmt::Debug; use std::hint::cold_path; -use std::mem::ManuallyDrop; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; - -use hashbrown::HashMap; use crate::actions::Action; use crate::component::storage::archetype::EntityComponent as ArchetypeEntityComponent; @@ -156,7 +152,7 @@ impl World entity_decl.create(self); } - /// Adds a globally shared singleton value. + /// Adds a singleton. /// /// # Errors /// Returns `Err` if this [`Sole`] has already been added. @@ -164,7 +160,9 @@ impl World where SoleT: Sole, { - self.data.sole_storage.insert(sole) + self.create_ent(SoleT::id(), [sole.into_parts()]); + + Ok(()) } pub fn register_observer<'this, SystemImpl, ObserverT>( @@ -253,7 +251,9 @@ impl World pub fn get_sole(&self) -> Option> { - Some(Single::new(self.data.sole_storage.get::()?)) + let ent = self.get_entity(SoleT::id())?; + + Some(Single::new(ent.get_with_id_mut(SoleT::id())?)) } pub fn event_submitter(&self) -> EventSubmitter<'_> @@ -701,7 +701,6 @@ pub enum StepResult struct WorldData { component_storage: ComponentStorage, - sole_storage: SoleStorage, action_queue: Rc, new_events: Lock, } @@ -765,73 +764,3 @@ impl ActionQueue #[error("Sole {0} already exists")] pub struct SoleAlreadyExistsError(pub &'static str); -#[derive(Debug)] -struct StoredSole -{ - sole: Arc>>, - drop_last: bool, -} - -#[derive(Debug, Default)] -struct SoleStorage -{ - storage: HashMap>, -} - -impl SoleStorage -{ - fn get(&self) -> Option<&Arc>>> - { - self.storage - .get(&TypeId::of::()) - .map(|sole| &sole.sole) - } - - fn insert(&mut self, sole: SoleT) -> Result<(), SoleAlreadyExistsError> - { - let sole_type_id = TypeId::of::(); - - if self.storage.contains_key(&sole_type_id) { - return Err(SoleAlreadyExistsError(type_name::())); - } - - let drop_last = sole.drop_last(); - - // TODO: Reconsider this maybe? - #[allow(clippy::arc_with_non_send_sync)] - self.storage.insert( - sole_type_id, - ManuallyDrop::new(StoredSole { - sole: Arc::new(Lock::new(Box::new(sole), type_name::())), - drop_last, - }), - ); - - Ok(()) - } -} - -impl Drop for SoleStorage -{ - fn drop(&mut self) - { - let mut soles_to_drop_last = Vec::new(); - - for sole in self.storage.values_mut() { - if sole.drop_last { - soles_to_drop_last.push(sole); - continue; - } - - unsafe { - ManuallyDrop::drop(sole); - } - } - - for sole in &mut soles_to_drop_last { - unsafe { - ManuallyDrop::drop(sole); - } - } - } -} -- cgit v1.2.3-18-g5258