2024-03-03 02:20:19 +00:00
|
|
|
use std::{any::TypeId, alloc::Layout};
|
2023-12-09 16:40:41 +00:00
|
|
|
|
|
|
|
/// 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),
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
impl From<u128> for DynTypeId {
|
|
|
|
fn from(val: u128) -> Self {
|
|
|
|
DynTypeId::Unknown(val)
|
2023-12-09 16:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 03:53:58 +00:00
|
|
|
impl From<TypeId> for DynTypeId {
|
|
|
|
fn from(val: TypeId) -> Self {
|
|
|
|
DynTypeId::Rust(val)
|
2023-12-16 15:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 {
|
2023-12-28 03:53:58 +00:00
|
|
|
DynTypeId::Rust(tyid) => *tyid == TypeId::of::<T>(),
|
2023-12-16 15:09:16 +00:00
|
|
|
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 {
|
2023-12-28 03:53:58 +00:00
|
|
|
DynTypeId::Rust(t) => *t,
|
2023-12-16 15:09:16 +00:00
|
|
|
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
|
|
|
|
2024-03-03 02:20:19 +00:00
|
|
|
/// Some information about a component.
|
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 {
|
2024-03-03 02:34:20 +00:00
|
|
|
type_id: DynTypeId,
|
|
|
|
layout: Layout,
|
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(),
|
2024-03-03 02:20:19 +00:00
|
|
|
layout: Layout::new::<T>(),
|
2023-12-09 16:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create ComponentInfo from a type that is not known to rust
|
2024-04-11 02:26:49 +00:00
|
|
|
pub fn new_unknown<D>(name: Option<String>, type_id: D, layout: Layout) -> Self
|
2023-12-09 16:40:41 +00:00
|
|
|
where
|
|
|
|
D: Into<DynTypeId>,
|
|
|
|
{
|
2023-12-28 03:53:58 +00:00
|
|
|
let _ = name; // would be used at some point
|
2023-12-09 16:40:41 +00:00
|
|
|
Self {
|
|
|
|
type_id: type_id.into(),
|
|
|
|
layout,
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-03 02:34:20 +00:00
|
|
|
|
|
|
|
pub fn type_id(&self) -> DynTypeId {
|
|
|
|
self.type_id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn layout(&self) -> Layout {
|
|
|
|
self.layout
|
|
|
|
}
|
2023-11-25 23:43:11 +00:00
|
|
|
}
|