use crate::{ archetype::{Archetype, ArchetypeComponentId}, component::ComponentId, query::{Access, FilteredAccessSet}, system::{System, SystemId, SystemParam, SystemParamFetch, SystemParamState}, world::World, }; use bevy_ecs_macros::all_tuples; use std::{borrow::Cow, marker::PhantomData}; pub struct SystemState { pub(crate) id: SystemId, pub(crate) name: Cow<'static, str>, pub(crate) component_access_set: FilteredAccessSet, pub(crate) archetype_component_access: Access, // NOTE: this must be kept private. making a SystemState non-send is irreversible to prevent SystemParams from overriding each other is_send: bool, } impl SystemState { fn new() -> Self { Self { name: std::any::type_name::().into(), archetype_component_access: Access::default(), component_access_set: FilteredAccessSet::default(), is_send: true, id: SystemId::new(), } } #[inline] pub fn is_send(&self) -> bool { self.is_send } #[inline] pub fn set_non_send(&mut self) { self.is_send = false; } } pub trait IntoSystem { fn system(self) -> SystemType; } // Systems implicitly implement IntoSystem impl IntoSystem<(), Sys> for Sys { fn system(self) -> Sys { self } } pub struct In(pub In); pub struct InputMarker; pub struct FunctionSystem where Param: SystemParam, { func: F, param_state: Option, system_state: SystemState, config: Option<::Config>, // NOTE: PhantomData T> gives this safe Send/Sync impls marker: PhantomData (In, Out, Marker)>, } impl FunctionSystem { pub fn config( mut self, f: impl FnOnce(&mut ::Config), ) -> Self { f(self.config.as_mut().unwrap()); self } } impl IntoSystem> for F where In: 'static, Out: 'static, Param: SystemParam + 'static, Marker: 'static, F: SystemParamFunction + Send + Sync + 'static, { fn system(self) -> FunctionSystem { FunctionSystem { func: self, param_state: None, config: Some(Default::default()), system_state: SystemState::new::(), marker: PhantomData, } } } impl System for FunctionSystem where In: 'static, Out: 'static, Param: SystemParam + 'static, Marker: 'static, F: SystemParamFunction + Send + Sync + 'static, { type In = In; type Out = Out; #[inline] fn name(&self) -> Cow<'static, str> { self.system_state.name.clone() } #[inline] fn id(&self) -> SystemId { self.system_state.id } #[inline] fn new_archetype(&mut self, archetype: &Archetype) { let param_state = self.param_state.as_mut().unwrap(); param_state.new_archetype(archetype, &mut self.system_state); } #[inline] fn component_access(&self) -> &Access { &self.system_state.component_access_set.combined_access() } #[inline] fn archetype_component_access(&self) -> &Access { &self.system_state.archetype_component_access } #[inline] fn is_send(&self) -> bool { self.system_state.is_send } #[inline] unsafe fn run_unsafe(&mut self, input: Self::In, world: &World) -> Self::Out { self.func.run( input, self.param_state.as_mut().unwrap(), &self.system_state, world, ) } #[inline] fn apply_buffers(&mut self, world: &mut World) { let param_state = self.param_state.as_mut().unwrap(); param_state.apply(world); } #[inline] fn initialize(&mut self, world: &mut World) { self.param_state = Some(::init( world, &mut self.system_state, self.config.take().unwrap(), )); } } pub trait SystemParamFunction: Send + Sync + 'static { fn run( &mut self, input: In, state: &mut Param::Fetch, system_state: &SystemState, world: &World, ) -> Out; } macro_rules! impl_system_function { ($($param: ident),*) => { #[allow(non_snake_case)] impl SystemParamFunction<(), Out, ($($param,)*), ()> for Func where Func: FnMut($($param),*) -> Out + FnMut($(<<$param as SystemParam>::Fetch as SystemParamFetch>::Item),*) -> Out + Send + Sync + 'static, Out: 'static { #[inline] fn run(&mut self, _input: (), state: &mut <($($param,)*) as SystemParam>::Fetch, system_state: &SystemState, world: &World) -> Out { unsafe { let ($($param,)*) = <<($($param,)*) as SystemParam>::Fetch as SystemParamFetch>::get_param(state, system_state, world); self($($param),*) } } } #[allow(non_snake_case)] impl SystemParamFunction for Func where Func: FnMut(In, $($param),*) -> Out + FnMut(In, $(<<$param as SystemParam>::Fetch as SystemParamFetch>::Item),*) -> Out + Send + Sync + 'static, Out: 'static { #[inline] fn run(&mut self, input: Input, state: &mut <($($param,)*) as SystemParam>::Fetch, system_state: &SystemState, world: &World) -> Out { unsafe { let ($($param,)*) = <<($($param,)*) as SystemParam>::Fetch as SystemParamFetch>::get_param(state, system_state, world); self(In(input), $($param),*) } } } }; } all_tuples!(impl_system_function, 0, 12, F);