I have trait like this to describe the structure of a type, if I know the type at compile-time, of course I can inspect all the associate constants, associate types, and static member functions of it. But the point is, there're hundreds(or even more countless) of types defined in a module, which all satisfy this TypeModule
trait, but I only got a TypeID
value from run-time, how can I identify which is the right type I want and inspect all its associate constants, associate types, and static member functions?
pub trait Value: std::fmt::Debug {
const SERIALIZED_SIZE_HINT: usize; //此NBT数据类型序列化的预估大小
fn deserialize_from(src: &[u8]) -> DynamicValue;//必须正确实现,返回的type_id必须正确,不允许失败,无论src为何都必须返回一个正确的DynamicValue
fn serialize_into(dynamic_value: &DynamicValue) -> Vec<u8>;//不允许失败,因为内存中的DynamicValue的数据一定处于正确的状态
}
///每个ID写一个类型,实现这个Trait,静态分发用
pub trait TypeModule {
const TYPE_ID: TypeID<'static>;
const TAG_LIST: &'static [Tag];
type BlockValue: Value;
type EntityValue: Value;
type ItemValue: Value;
fn get_type_info() -> TypeInfo {
let block = if !is_same_type::<Self::BlockValue, ()>() {Some(SerializeDeserializeFunctions{
deserialize_from: Self::BlockValue::deserialize_from,
serialize_into: Self::BlockValue::serialize_into,
serialize_size_hint: Self::BlockValue::SERIALIZED_SIZE_HINT,
})} else {None};
let entity = if !is_same_type::<Self::EntityValue, ()>() {Some(SerializeDeserializeFunctions{
deserialize_from: Self::EntityValue::deserialize_from,
serialize_into: Self::EntityValue::serialize_into,
serialize_size_hint: Self::EntityValue::SERIALIZED_SIZE_HINT,
})} else {None};
let item = if !is_same_type::<Self::ItemValue, ()>() {Some(SerializeDeserializeFunctions{
deserialize_from: Self::ItemValue::deserialize_from,
serialize_into: Self::ItemValue::serialize_into,
serialize_size_hint: Self::ItemValue::SERIALIZED_SIZE_HINT,
})} else {None};
TypeInfo {
type_id: Self::TYPE_ID,
tags: Self::TAG_LIST,
block_functions: block,
entity_functions: entity,
item_functions: item,
}
}
}
Is that possible to write code iterating all the types in a module, so I can compare their associate TYPE_ID with the run-time given value and get the correct TAG_LIST,SERIALIZED_SIZE_HINT and all the member function pointers?
The closest effort I made is adding an function to TypeModule to turn a type into a TypeInfo value:
///Helper Function
struct SameType<T1, T2> {}
impl<T1, T2> SameType<T1, T2> {
const VALUE :bool = false;
}
impl<T> SameType<T, T> {
const VALUE :bool = true;
}
fn is_same_type<T1, T2>() -> bool {SameType::<T1, T2>::VALUE}
#[derive(PartialEq,Eq,Copy,Clone)]
pub enum Tag {
}
pub struct SerializeDeserializeFunctions {
deserialize_from: fn(&[u8]) -> DynamicValue,
serialize_into: fn(&DynamicValue) -> Vec<u8>,
serialize_size_hint: usize,
}
///write an instance of this type for every id, for dynamic dispatching
pub struct TypeInfo {
type_id: TypeID<'static>,
tags: &'static [Tag],
block_functions: Option<SerializeDeserializeFunctions>,
entity_functions: Option<SerializeDeserializeFunctions>,
item_functions: Option<SerializeDeserializeFunctions>,
}
I can turn a pre-known type into TypeInfo
but I don't know how check for hundreds of types of a module.