2023-12-09 16:40:41 +00:00
|
|
|
use std::{any::{TypeId, type_name}, alloc::{Layout, LayoutError}};
|
|
|
|
|
2023-12-09 17:23:10 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2023-12-09 16:40:41 +00:00
|
|
|
pub struct MemoryLayout {
|
|
|
|
pub size: usize,
|
|
|
|
pub alignment: usize
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<Layout> for MemoryLayout {
|
|
|
|
type Error = LayoutError;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<Layout, Self::Error> {
|
|
|
|
Layout::from_size_align(self.size, self.alignment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Layout> 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<DynTypeId> for u128 {
|
|
|
|
fn into(self) -> DynTypeId {
|
|
|
|
DynTypeId::Unknown(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 15:09:16 +00:00
|
|
|
impl Into<DynTypeId> for TypeId {
|
|
|
|
fn into(self) -> DynTypeId {
|
|
|
|
DynTypeId::Rust(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* impl From<TypeId> for DynTypeId {
|
2023-12-09 16:40:41 +00:00
|
|
|
fn from(value: TypeId) -> Self {
|
|
|
|
DynTypeId::Rust(value)
|
|
|
|
}
|
2023-12-16 15:09:16 +00:00
|
|
|
} */
|
2023-12-09 16:40:41 +00:00
|
|
|
|
|
|
|
impl DynTypeId {
|
|
|
|
pub fn of<T: 'static>() -> Self {
|
|
|
|
Self::Rust(TypeId::of::<T>())
|
|
|
|
}
|
2023-12-16 15:09:16 +00:00
|
|
|
|
|
|
|
pub fn is<T: 'static>(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
DynTypeId::Rust(tyid) => tyid.clone() == TypeId::of::<T>(),
|
|
|
|
DynTypeId::Unknown(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_id<I: Into<DynTypeId>>(&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!"),
|
|
|
|
}
|
|
|
|
}
|
2023-12-09 16:40:41 +00:00
|
|
|
}
|
2023-11-25 23:43:11 +00:00
|
|
|
|
2023-12-09 19:39:01 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2023-11-25 23:43:11 +00:00
|
|
|
pub struct ComponentInfo {
|
2023-12-09 16:40:41 +00:00
|
|
|
pub type_id: DynTypeId,
|
2023-12-09 19:39:01 +00:00
|
|
|
//pub name: String,
|
2023-12-09 16:40:41 +00:00
|
|
|
pub layout: MemoryLayout,
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ComponentInfo {
|
|
|
|
pub fn new<T: 'static>() -> Self {
|
|
|
|
Self {
|
2023-12-16 15:09:16 +00:00
|
|
|
type_id: TypeId::of::<T>().into(),
|
2023-12-09 19:39:01 +00:00
|
|
|
//name: type_name::<T>().to_string(),
|
2023-12-09 16:40:41 +00:00
|
|
|
layout: MemoryLayout::from(Layout::new::<T>()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create ComponentInfo from a type that is not known to rust
|
|
|
|
pub fn new_unknown<D>(type_id: D, name: &str, layout: MemoryLayout) -> Self
|
|
|
|
where
|
|
|
|
D: Into<DynTypeId>,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
type_id: type_id.into(),
|
2023-12-09 19:39:01 +00:00
|
|
|
//name: name.to_string(),
|
2023-12-09 16:40:41 +00:00
|
|
|
layout,
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|