Help for a macro doing "copy-execute-copyback"

I want to make a macro which finds arguments whose type are pointer, and
copy their values on the stack, then call the function with the copied values.
After the function retunes, it copies back to the original variable.
Below is an example.

let var: usize = 0x123;
my_macro!(my_func(&mut var as *mut usize));

macro_rules my_macro {
($f:ident( *mut $var:tt, $($tail:tt)*)) => {
let temp_var = *var;
$f(&temp_var, $($tail:tt)*);
*var = temp_var;

I can handle it with a single pointer argument when it is the first argument of the function. However, I can't handle it when there are multiple pointer arguments and their order is random. I was able to use the incremental TT munchers to figure out which argument is a pointer, but I can only find pointers.

I don't think you can reliably detect pointer types at the macro level, because macros don't have access to type checking. They are a purely syntactic construct, expanded well before the compiler even starts computing and worrying about types.

That said, what is the goal you're trying to achieve with this? Perhaps (and hopefully) it has a cleaner solution.

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