use std::{any::TypeId, cell::{Ref, RefMut}, ptr::NonNull}; use lyra_ecs::{World, ResourceObject}; use crate::{Reflect, FromType}; #[derive(Clone)] pub struct ReflectedResource { pub type_id: TypeId, fn_reflect: for<'a> fn (world: &'a World) -> Option>, fn_reflect_mut: for<'a> fn (world: &'a mut World) -> Option>, fn_reflect_ptr: fn (world: &mut World) -> Option>, } impl ReflectedResource { /// Retrieves the reflected resource from the world. pub fn reflect<'a>(&self, world: &'a World) -> Option> { (self.fn_reflect)(world) } /// Retrieves a mutable reflected resource from the world. pub fn reflect_mut<'a>(&self, world: &'a mut World) -> Option> { (self.fn_reflect_mut)(world) } pub fn reflect_ptr(&self, world: &mut World) -> Option> { (self.fn_reflect_ptr)(world) } } impl FromType for ReflectedResource { fn from_type() -> Self { Self { type_id: TypeId::of::(), fn_reflect: |world: &World| { world.try_get_resource::() .map(|r| r as Ref) }, fn_reflect_mut: |world: &mut World| { world.try_get_resource_mut::() .map(|r| r as RefMut) }, fn_reflect_ptr: |world: &mut World| unsafe { world.try_get_resource_ptr::() .map(|ptr| ptr.cast::()) }, } } }