Rust是否有可能遍历模块中的所有类型?

 收藏

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,
        }
    }
}

是否可以编写代码来迭代模块中的所有类型,所以我可以将它们的关联TYPE_ID与运行时给定值进行比较,并获得正确的TAG_LIST,SERIALIZED_SIZE_HINT和所有成员函数指针?

我所做的最接近的工作是向TypeModule添加一个函数,以将类型转换为TypeInfo值:

///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.

回复