diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-21 20:18:37 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-21 20:18:37 +0200 |
commit | 58f29444ac234c285ce0e3a729ade2b52a303f58 (patch) | |
tree | 03bb210d62bf98acc1ee30ce9df1344d8239f7c6 /ecs/src/lib.rs | |
parent | c3c96f2d7b63d1d7e55f5a9a85da85444089c2a2 (diff) |
refactor(ecs): fix clippy lints
Diffstat (limited to 'ecs/src/lib.rs')
-rw-r--r-- | ecs/src/lib.rs | 75 |
1 files changed, 31 insertions, 44 deletions
diff --git a/ecs/src/lib.rs b/ecs/src/lib.rs index e298919..40b2021 100644 --- a/ecs/src/lib.rs +++ b/ecs/src/lib.rs @@ -15,12 +15,12 @@ use crate::entity::Uid as EntityUid; use crate::event::component::{ create_added_id as create_component_added_event_id, create_removed_id as create_component_removed_event_id, - ComponentToAddedEvent, + TypeTransformComponentsToAddedEvents, }; use crate::event::start::Start as StartEvent; use crate::event::{Event, Id as EventId, Ids, Sequence as EventSequence}; use crate::extension::{Collector as ExtensionCollector, Extension}; -use crate::lock::Lock; +use crate::lock::{Lock, WriteGuard}; use crate::query::options::Options as QueryOptions; use crate::sole::Sole; use crate::system::{System, TypeErased as TypeErasedSystem}; @@ -69,7 +69,7 @@ impl World /// Will panic if mutable internal lock cannot be acquired. pub fn create_entity<Comps>(&mut self, components: Comps) -> EntityUid where - Comps: ComponentSequence + TupleReduce<ComponentToAddedEvent>, + Comps: ComponentSequence + TupleReduce<TypeTransformComponentsToAddedEvents>, Comps::Out: EventSequence, { let (_, entity_uid) = self @@ -145,7 +145,7 @@ impl World Comps: ComponentSequence, OptionsT: QueryOptions, { - Query::new(&self) + Query::new(self) } /// Peforms the actions that have been queued up using [`Actions`]. @@ -172,10 +172,7 @@ impl World for action in active_action_queue.drain(..) { match action { Action::Spawn(components) => { - let mut component_storage_lock = - self.data.component_storage.write_nonblock().expect( - "Failed to acquire read-write component storage lock", - ); + let mut component_storage_lock = self.lock_component_storage_rw(); let component_ids = components .iter() @@ -188,15 +185,7 @@ impl World drop(component_storage_lock); if !has_swapped_active_queue { - let mut active_queue = - self.data.action_queue.active_queue.borrow_mut(); - - *active_queue = match *active_queue { - ActiveActionQueue::A => ActiveActionQueue::B, - ActiveActionQueue::B => ActiveActionQueue::A, - }; - - has_swapped_active_queue = true; + self.swap_event_queue(&mut has_swapped_active_queue); } for component_id in component_ids { @@ -206,10 +195,7 @@ impl World } } Action::AddComponents(entity_uid, components) => { - let mut component_storage_lock = - self.data.component_storage.write_nonblock().expect( - "Failed to acquire read-write component storage lock", - ); + let mut component_storage_lock = self.lock_component_storage_rw(); let component_ids = components .iter() @@ -222,15 +208,7 @@ impl World drop(component_storage_lock); if !has_swapped_active_queue { - let mut active_queue = - self.data.action_queue.active_queue.borrow_mut(); - - *active_queue = match *active_queue { - ActiveActionQueue::A => ActiveActionQueue::B, - ActiveActionQueue::B => ActiveActionQueue::A, - }; - - has_swapped_active_queue = true; + self.swap_event_queue(&mut has_swapped_active_queue); } for component_id in component_ids { @@ -240,10 +218,7 @@ impl World } } Action::RemoveComponents(entity_uid, components_metadata) => { - let mut component_storage_lock = - self.data.component_storage.write_nonblock().expect( - "Failed to acquire read-write component storage lock", - ); + let mut component_storage_lock = self.lock_component_storage_rw(); component_storage_lock.remove_components_from_entity( entity_uid, @@ -255,15 +230,7 @@ impl World drop(component_storage_lock); if !has_swapped_active_queue { - let mut active_queue = - self.data.action_queue.active_queue.borrow_mut(); - - *active_queue = match *active_queue { - ActiveActionQueue::A => ActiveActionQueue::B, - ActiveActionQueue::B => ActiveActionQueue::A, - }; - - has_swapped_active_queue = true; + self.swap_event_queue(&mut has_swapped_active_queue); } for component_metadata in components_metadata { @@ -314,10 +281,30 @@ impl World // SAFETY: The world lives long enough unsafe { - system.run(&self); + system.run(self); } } } + + fn swap_event_queue(&self, has_swapped_active_queue: &mut bool) + { + let mut active_queue = self.data.action_queue.active_queue.borrow_mut(); + + *active_queue = match *active_queue { + ActiveActionQueue::A => ActiveActionQueue::B, + ActiveActionQueue::B => ActiveActionQueue::A, + }; + + *has_swapped_active_queue = true; + } + + fn lock_component_storage_rw(&self) -> WriteGuard<'_, ComponentStorage> + { + self.data + .component_storage + .write_nonblock() + .expect("Failed to acquire read-write component storage lock") + } } #[derive(Debug, Default)] |