The idea is to implement a derive macro for enum that wraps generic and prints wrapped struct name.
struct MyStruct {}
// #[derive(PrintWrapped)]
enum GenericStruct<X> {
Variant1(X),
}
// I need so that derived code is something like this
impl<X> GenericStruct<X> {
pub fn print_wrapped_name(&self) {
// Somehow print here "MyStruct"
print!("");
}
}
fn main(){
let x = GenericStruct::Variant1(MyStruct {});
x.print_wrapped_name();
}
The challenge is that in proc-macro crate that implements PrintWrapped code generation, i can access only variants and fields for enum GenericStruct, so at that point in code i have only X available, there is no way to get MyStruct. There is a macro in quote crate format_ident, which almost does what i need, it can convert X to string, but i need to convert MyStruct to string somehow.
I could require MyStruct to implement specific trait, i.e. StructNameAsString and then use specific method in derived code, but that would force to write extra code for MyStruct (not convenient for library users) and would incur extra cost on performance for extra function call where the string in reality is const (maybe this is negligible, but... ).
Theoretically at some point compiler is filling in MyStruct in generic definition. Practically i do not know a way how could i get that info.
How would you solve this? If requiring specific trait bound is the only option in this situation, is there already an existing trait available (in a way it is similar to Display/Debug)?