Generating unique IDs for types at compile time

前端 未结 3 1778
抹茶落季
抹茶落季 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:45

    This sounds like a job for the bitflags! macro:

    #[macro_use] extern crate rustc_bitflags;
    
    bitflags!(
        #[derive(Debug)]
        flags ComponentMask: u8 {
            const Render     = 0b00000001,
            const Position   = 0b00000010,
            const Physics    = 0b00000100
        }
    );
    
    // the set of components owned by an entity:
    let owned_components:  = Render | Position;
    
    // check whether an entity has a certain component:
    if owned_components.contains(Physics) { ... }
    

    http://doc.rust-lang.org/rustc_bitflags/macro.bitflags!.html

提交回复
热议问题