Feature request for declarative macro: make tt to match TraitName for any struct implemented TraitName

For implementing a great declarative macro, it need tt to match a TraitName. For example, the following my_great_macro! have to call set_value! marcro:

macro_rules! my_great_macro {

......
set_value!(name, $t);//$t is a tt!
......

}

macro_rules! set_u32_value {

($name:expr, u32) => { //it's good to match when tt==u32
    ...
};
($name:expr, bool) => { //it's good to match when tt==bool
    ...
};
($name:expr, TraitName) => { //however, it cannot match TraitName!
    ...
};
($name:expr, StructName) => { //even StructName, it's good to match when tt==StructName
    ...
};

($($else:tt)*) => {};

}

It is good to match the tt pattern for u32, bool, and even StructName. But, it cannot match the pattern for any struct which impl TraitName.

Is it possible to implement this feature for a future version of rust language?

Are TraitName and StructName just placeholders here? Because I can't see why one would match and not the other...

Also, I think you are looking for they $ty matcher.

No, $ty is not what I need.

Well, do you notice 'struct which impl TraitName' I mentioned? TraitName is not the actual placeholders,but StructName - struct implemented TraitName.

Well, my mistake. I have modified the title to fit what it really mean.

This is impossible: macros don't have access to information about what types implement what traits, they run at an earlier phase of the compilation process.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.