From 7d218b2525f90dfedcae02f3b3d0d2f7b9c99bd2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 16 Aug 2024 20:13:22 +0200 Subject: feat(ecs): make relationships creatable without reference to world --- ecs/src/relationship.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) (limited to 'ecs/src/relationship.rs') diff --git a/ecs/src/relationship.rs b/ecs/src/relationship.rs index 82c9e70..fa3f761 100644 --- a/ecs/src/relationship.rs +++ b/ecs/src/relationship.rs @@ -1,6 +1,5 @@ use std::any::{type_name, Any}; use std::marker::PhantomData; -use std::sync::{Arc, Weak}; use crate::component::storage::Storage as ComponentStorage; use crate::component::{ @@ -11,7 +10,7 @@ use crate::component::{ Metadata as ComponentMetadata, }; use crate::entity::Uid as EntityUid; -use crate::lock::{Lock, ReadGuard}; +use crate::lock::ReadGuard; use crate::system::{ComponentRefMut, Input as SystemInput}; use crate::type_name::TypeName; use crate::World; @@ -20,7 +19,6 @@ use crate::World; pub struct Relationship { entity_uid: EntityUid, - component_storage: Weak>, _pd: PhantomData<(Kind, ComponentT)>, } @@ -28,13 +26,9 @@ impl Relationship where ComponentT: Component, { - pub fn new(world: &World, entity_uid: EntityUid) -> Self + pub fn new(entity_uid: EntityUid) -> Self { - Self { - entity_uid, - component_storage: Arc::downgrade(&world.data.component_storage), - _pd: PhantomData, - } + Self { entity_uid, _pd: PhantomData } } } @@ -86,7 +80,6 @@ where { component_storage_lock: ReadGuard<'static, ComponentStorage>, relationship_comp: ComponentRefMut<'rel_comp, Relationship>, - _component_storage: Arc>, } impl<'rel_comp, Kind, ComponentT> ComponentFromOptional<'rel_comp> @@ -98,19 +91,18 @@ where optional_component: Option< crate::lock::WriteGuard<'rel_comp, Box>, >, + world: &'rel_comp World, ) -> Self { let relationship_comp = ComponentRefMut::>::from_optional_component( optional_component, + world, ); - let component_storage = relationship_comp + let component_storage_lock = world + .data .component_storage - .upgrade() - .expect("World has been dropped"); - - let component_storage_lock = component_storage .read_nonblock() .expect("Failed to aquire read-only component storage lock"); @@ -119,7 +111,6 @@ where // SAFETY: The component lock is not used for longer than the original // lifetime component_storage_lock: unsafe { component_storage_lock.upgrade_lifetime() }, - _component_storage: component_storage, } } } -- cgit v1.2.3-18-g5258