pub use super::Query; use crate::{ resource::Resources, system::{System, SystemId, ThreadLocalExecution}, ArchetypeComponent, TypeAccess, World, }; use std::{any::TypeId, borrow::Cow}; #[derive(Debug)] pub(crate) struct ThreadLocalSystemFn where Func: FnMut(&mut World, &mut Resources) + Send + Sync, { pub func: Func, pub resource_access: TypeAccess, pub archetype_component_access: TypeAccess, pub name: Cow<'static, str>, pub id: SystemId, } impl System for ThreadLocalSystemFn where Func: FnMut(&mut World, &mut Resources) + Send + Sync, { fn name(&self) -> Cow<'static, str> { self.name.clone() } fn update(&mut self, _world: &World) {} fn archetype_component_access(&self) -> &TypeAccess { &self.archetype_component_access } fn resource_access(&self) -> &TypeAccess { &self.resource_access } fn thread_local_execution(&self) -> ThreadLocalExecution { ThreadLocalExecution::Immediate } fn run(&mut self, _world: &World, _resources: &Resources) {} fn run_thread_local(&mut self, world: &mut World, resources: &mut Resources) { (self.func)(world, resources); } fn initialize(&mut self, _world: &mut World, _resources: &mut Resources) {} fn id(&self) -> SystemId { self.id } fn is_initialized(&self) -> bool { true } } /// Converts `Self` into a thread local system pub trait IntoThreadLocalSystem { fn thread_local_system(self) -> Box; } impl IntoThreadLocalSystem for F where F: FnMut(&mut World, &mut Resources) + Send + Sync + 'static, { fn thread_local_system(mut self) -> Box { Box::new(ThreadLocalSystemFn { func: move |world, resources| (self)(world, resources), name: core::any::type_name::().into(), id: SystemId::new(), resource_access: TypeAccess::default(), archetype_component_access: TypeAccess::default(), }) } }