From d0cac9159e6ddfe5dbb9b32036e7258f6e51d47e Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 18 Jan 2026 17:46:02 +0100 Subject: refactor(ecs): remove Actions & Sole weak ref structs --- ecs/src/actions.rs | 68 ------------------------------------------------------ ecs/src/sole.rs | 68 +++--------------------------------------------------- 2 files changed, 3 insertions(+), 133 deletions(-) (limited to 'ecs') diff --git a/ecs/src/actions.rs b/ecs/src/actions.rs index 549e341..3d8afe6 100644 --- a/ecs/src/actions.rs +++ b/ecs/src/actions.rs @@ -1,7 +1,3 @@ -use std::any::type_name; -use std::marker::PhantomData; -use std::rc::{Rc, Weak}; - use crate::component::{Parts as ComponentParts, Sequence as ComponentSequence}; use crate::event::component::Removed; use crate::pair::Pair; @@ -151,28 +147,6 @@ impl Actions<'_> { self.action_queue.push(Action::Stop); } - - /// Returns a struct which holds a weak reference to the [`World`] that `Actions` - /// references and that can be used to aquire a new `Actions` instance if the - /// referenced [`World`] is still alive. - /// - /// # Panics - /// This function will panic if `self` was retrieved from a [`WeakRef`]. - #[must_use] - pub fn to_weak_ref(&self) -> WeakRef - { - let world = self.world.unwrap_or_else(|| { - panic!( - "This function cannot be called if the {} was retrieved from a {}", - type_name::(), - type_name::() - ) - }); - - WeakRef { - action_queue: Rc::downgrade(&world.data.action_queue), - } - } } impl<'world> SystemParam<'world> for Actions<'world> @@ -188,48 +162,6 @@ impl<'world> SystemParam<'world> for Actions<'world> } } -#[derive(Debug, Clone)] -pub struct WeakRef -{ - action_queue: Weak, -} - -impl WeakRef -{ - /// Returns a struct which can be used to retrieve a [`Actions`]. - /// - /// Returns [`None`] if the referenced [`World`] has been dropped. - #[must_use] - pub fn access(&self) -> Option> - { - Some(Ref { - action_queue: self.action_queue.upgrade()?, - _pd: PhantomData, - }) - } -} - -/// Intermediate between [`Actions`] and [`WeakRef`]. Contains a strong reference to -/// a world which is not allowed direct access to. -#[derive(Debug, Clone)] -pub struct Ref<'weak_ref> -{ - action_queue: Rc, - _pd: PhantomData<&'weak_ref ()>, -} - -impl Ref<'_> -{ - #[must_use] - pub fn to_actions(&self) -> Actions<'_> - { - Actions { - action_queue: &self.action_queue, - world: None, - } - } -} - /// A action for a [`System`] to perform. #[derive(Debug)] pub(crate) enum Action diff --git a/ecs/src/sole.rs b/ecs/src/sole.rs index 7cfcc24..82e5e0f 100644 --- a/ecs/src/sole.rs +++ b/ecs/src/sole.rs @@ -1,12 +1,12 @@ -use std::any::{type_name, Any}; +use std::any::{Any, type_name}; use std::fmt::Debug; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; -use std::sync::{Arc, Weak}; +use std::sync::Arc; +use crate::World; use crate::lock::{Lock, WriteGuard}; use crate::system::{Metadata as SystemMetadata, Param as SystemParam}; -use crate::World; /// A type which has a single instance and is shared globally. pub trait Sole: Any @@ -44,7 +44,6 @@ impl Debug for dyn Sole pub struct Single<'world, SoleT: Sole> { sole: WriteGuard<'world, Box>, - sole_weak: Weak>>, _ph: PhantomData, } @@ -52,18 +51,6 @@ impl<'world, SoleT> Single<'world, SoleT> where SoleT: Sole, { - /// Returns a struct which holds a weak reference to the [`World`] that `Single` - /// references and that can be used to aquire a new `Single` if the referenced - /// [`World`] is still alive. - #[must_use] - pub fn to_weak_ref(&self) -> SingleWeakRef - { - SingleWeakRef { - sole: self.sole_weak.clone(), - _ph: PhantomData, - } - } - pub(crate) fn new(sole: &'world Arc>>) -> Self { Self { @@ -73,7 +60,6 @@ where type_name::() ) }), - sole_weak: Arc::downgrade(sole), _ph: PhantomData, } } @@ -116,51 +102,3 @@ where self.sole.downcast_mut().unwrap() } } - -#[derive(Debug, Clone)] -pub struct SingleWeakRef -where - SoleT: Sole, -{ - sole: Weak>>, - _ph: PhantomData, -} - -impl SingleWeakRef -where - SoleT: Sole, -{ - /// Returns a struct which can be used to retrieve a [`Single`]. - /// - /// Returns [`None`] if the referenced [`World`] has been dropped. - #[must_use] - pub fn access(&self) -> Option> - { - Some(SingleRef { - sole: self.sole.upgrade()?, - _pd: PhantomData, - }) - } -} - -/// Intermediate between [`Single`] and [`SingleWeakRef`]. Contains a strong reference to -/// a world which is not allowed direct access to. -#[derive(Debug, Clone)] -pub struct SingleRef<'weak_ref, SoleT> -where - SoleT: Sole, -{ - sole: Arc>>, - _pd: PhantomData<&'weak_ref SoleT>, -} - -impl SingleRef<'_, SoleT> -where - SoleT: Sole, -{ - #[must_use] - pub fn to_single(&self) -> Single<'_, SoleT> - { - Single::new(&self.sole) - } -} -- cgit v1.2.3-18-g5258