use std::marker::PhantomData; use crate::component::Local; use crate::system::{System, TypeErased}; use crate::{ComponentStorage, Query}; /// A stateful system. pub struct Stateful { func: Func, local_component: Option, _comps_pd: PhantomData, } impl System, Local)> for Stateful where Func: Fn(Query, Local), { type Input = LocalComponent; type Query<'a> = Query<'a, Comps>; fn initialize(mut self, input: Self::Input) -> Self { self.local_component = Some(input); self } fn run(&mut self, component_storage: &mut ComponentStorage) { (self.func)( Query::new(component_storage), Local::new(self.local_component.as_mut().unwrap()), ); } fn into_type_erased(self) -> TypeErased { TypeErased { data: Box::new(self), func: Box::new(move |data, component_storage| { let this = data.downcast_mut::().unwrap(); this.run(component_storage); }), } } } impl crate::system::Into, Local)> for Func where Func: Fn(Query, Local), { type System = Stateful; fn into_system(self) -> Self::System { Self::System { func: self, local_component: None, _comps_pd: PhantomData, } } }