use std::any::Any; use std::ops::{Deref, DerefMut}; use crate::component::{Component, Id}; use crate::system::{ComponentRefMut, Param as SystemParam, System}; use crate::World; /// Holds a component which is local to a single system. #[derive(Debug)] pub struct Local<'world, LocalComponent: Component> { local_component: ComponentRefMut<'world, LocalComponent>, } unsafe impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent> where LocalComponent: Component, { type Flags = (); type Input = LocalComponent; fn initialize( system: &mut impl System<'world, SystemImpl>, input: Self::Input, ) { system.set_local_component(input); } fn new( system: &'world impl System<'world, SystemImpl>, _world: &'world World, ) -> Self { let local_component = system .get_local_component_mut::() .expect("Local component is uninitialized"); Self { local_component } } fn is_compatible>() -> bool { let other_comparable = Other::get_comparable(); let Some(other_id) = other_comparable.downcast_ref::() else { return true; }; Id::of::() != *other_id } fn get_comparable() -> Box { Box::new(Id::of::()) } } impl<'world, LocalComponent> Deref for Local<'world, LocalComponent> where LocalComponent: Component, { type Target = LocalComponent; fn deref(&self) -> &Self::Target { &self.local_component } } impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent> where LocalComponent: Component, { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.local_component } }