use std::{any::TypeId, alloc::Layout}; /// A dynamic type id. Supports types that are not known to Rust. #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq)] pub enum DynTypeId { Rust(TypeId), Unknown(u128), } impl From for DynTypeId { fn from(val: u128) -> Self { DynTypeId::Unknown(val) } } impl From for DynTypeId { fn from(val: TypeId) -> Self { DynTypeId::Rust(val) } } /* impl From for DynTypeId { fn from(value: TypeId) -> Self { DynTypeId::Rust(value) } } */ impl DynTypeId { pub fn of() -> Self { Self::Rust(TypeId::of::()) } pub fn is(&self) -> bool { match self { DynTypeId::Rust(tyid) => *tyid == TypeId::of::(), DynTypeId::Unknown(_) => false, } } pub fn is_id>(&self, id: I) -> bool { let id = id.into(); *self == id } /// Force self into a rust TypeId, will panic if this type is not a Rust type. pub fn as_rust(&self) -> TypeId { match self { DynTypeId::Rust(t) => *t, DynTypeId::Unknown(_) => panic!("This type is unknown to rust, cannot construct a TypeId from it!"), } } } /// Some information about a component. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ComponentInfo { type_id: DynTypeId, layout: Layout, } impl ComponentInfo { pub fn new() -> Self { Self { type_id: TypeId::of::().into(), layout: Layout::new::(), } } /// Create ComponentInfo from a type that is not known to rust pub fn new_unknown(name: Option, type_id: D, layout: Layout) -> Self where D: Into, { let _ = name; // would be used at some point Self { type_id: type_id.into(), layout, } } pub fn type_id(&self) -> DynTypeId { self.type_id } pub fn layout(&self) -> Layout { self.layout } }