Generating unique IDs for types at compile time

前端 未结 3 1773
抹茶落季
抹茶落季 2020-12-11 10:01

I want to generate a unique id for every type at compile time. Is this possible in Rust?

So far, I have the following code

//Pseudo code
struct Class         


        
3条回答
  •  北海茫月
    2020-12-11 10:48

    If you want to manage type ids manually, you can use my unique-type-id crate. It allows you to specify what ids a type has in a special file. It will generate them at compile time. Currently it can be used in this way:

    use unique_type_id::UniqueTypeId;
    #[derive(UniqueTypeId)]
    struct Test1;
    #[derive(UniqueTypeId)]
    struct Test2;
    
    assert_eq!(Test1::id().0, 1u64);
    assert_eq!(Test2::id().0, 2u64);
    

    It can both generate types using incremental number and use the id from a file.

提交回复
热议问题