Give all lifetime parameters of a type one lifetime in a macro

Say I have a macro that receives a type T that may look anything like a non-generic type, a T<'a>, a T<'a,'b>, etc. If I write a function fn example(t: T) {...}, the compiler can infer appropriate lifetime parameters in some cases.

I want a more general way of doing this sort of lifetime inference for generating structs in a macro. Is there a way I can give all lifetime parameters of the type, however many there may be, one lifetime of my choice? (For example, something like WithLifetime<'a, T> where WithLifetime gives all lifetime parameters of T the lifetime 'a.)

I don't think so, no. The thing is that proc_macros deal mainly with syntactic transformations, which means they don't really have a notion of what that syntax means semantically. It's syntax in, syntax out.

However, proc_macros are Turing complete, and thus can in principle execute any kind of code that can be run on a regular computer. Therefore, to do any kind of sophisticated analyses on the input, you'd have to write them yourself and call them from the macro.
Perhaps in the (far) future you would be able to leverage librarified pieces of rustc for that, but not in 2022.

Yeah I'm familiar with the limitations of proc macros, I was just wondering whether there was some sort of clever language hack I could use to create a WithLifetime trait/type. I thought this might be possible since something kind of akin to what I was asking about happens with functions, but looks like it might not be.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.