use std::{any::{TypeId, type_name}, alloc::{Layout, LayoutError}}; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct MemoryLayout { pub size: usize, pub alignment: usize } impl TryInto for MemoryLayout { type Error = LayoutError; fn try_into(self) -> Result { Layout::from_size_align(self.size, self.alignment) } } impl From for MemoryLayout { fn from(value: Layout) -> Self { Self { size: value.size(), alignment: value.align(), } } } impl MemoryLayout { pub fn new(size: usize, alignment: usize) -> Self { MemoryLayout { size, alignment } } pub unsafe fn into_layout_unchecked(self) -> Layout { Layout::from_size_align_unchecked(self.size, self.alignment) } } /// 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 Into for u128 { fn into(self) -> DynTypeId { DynTypeId::Unknown(self) } } impl Into for TypeId { fn into(self) -> DynTypeId { DynTypeId::Rust(self) } } /* 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.clone() == 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.clone(), DynTypeId::Unknown(_) => panic!("This type is unknown to rust, cannot construct a TypeId from it!"), } } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ComponentInfo { pub type_id: DynTypeId, //pub name: String, pub layout: MemoryLayout, } impl ComponentInfo { pub fn new() -> Self { Self { type_id: TypeId::of::().into(), //name: type_name::().to_string(), layout: MemoryLayout::from(Layout::new::()), } } /// Create ComponentInfo from a type that is not known to rust pub fn new_unknown(type_id: D, name: &str, layout: MemoryLayout) -> Self where D: Into, { Self { type_id: type_id.into(), //name: name.to_string(), layout, } } }