diff options
author | HampusM <hampus@hampusmat.com> | 2025-09-11 18:42:53 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-09-11 18:42:53 +0200 |
commit | 3a2cbf0208635866e6da56091ce8e046c0bebb35 (patch) | |
tree | 0becee8b3d95c29d751d91a0fc3531cdb02b5a97 /ecs/src | |
parent | b920863c2853a1a610cdc5274c913358cfc0f56e (diff) |
feat(ecs): add get_sole fn to World
Diffstat (limited to 'ecs/src')
-rw-r--r-- | ecs/src/lib.rs | 21 | ||||
-rw-r--r-- | ecs/src/sole.rs | 2 |
2 files changed, 10 insertions, 13 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index 6f49a91..e9494a7 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -33,7 +33,7 @@ use crate::query::{ TermsBuilderInterface, MAX_TERM_CNT as QUERY_MAX_TERM_CNT, }; -use crate::sole::Sole; +use crate::sole::{Single, Sole}; use crate::stats::Stats; use crate::system::observer::{Observer, WrapperComponent as ObserverWrapperComponent}; use crate::system::{Callbacks, Metadata as SystemMetadata, System, SystemComponent}; @@ -212,6 +212,11 @@ impl World Some(EntityHandle::new(archetype, entity, self)) } + pub fn get_sole<SoleT: Sole>(&self) -> Option<Single<'_, SoleT>> + { + Some(Single::new(self.data.sole_storage.get::<SoleT>()?)) + } + pub fn event_submitter(&self) -> EventSubmitter<'_> { EventSubmitter::new(self) @@ -246,17 +251,9 @@ impl World return StepResult::Stop; } - let mut stats_lock = self - .data - .sole_storage - .get::<Stats>() - .expect("No stats sole found") - .write_nonblock() - .expect("Failed to aquire read-write stats sole lock"); - - let stats = stats_lock - .downcast_mut::<Stats>() - .expect("Casting stats sole to Stats type failed"); + let Some(mut stats) = self.get_sole::<Stats>() else { + unreachable!(); // Reason: is added in World::new + }; stats.current_tick += 1; diff --git a/ecs/src/sole.rs b/ecs/src/sole.rs index f148224..7cfcc24 100644 --- a/ecs/src/sole.rs +++ b/ecs/src/sole.rs @@ -64,7 +64,7 @@ where } } - fn new(sole: &'world Arc<Lock<Box<dyn Sole>>>) -> Self + pub(crate) fn new(sole: &'world Arc<Lock<Box<dyn Sole>>>) -> Self { Self { sole: sole.write_nonblock().unwrap_or_else(|_| { |