I was wondering if it was possible to modify fields of a struct after its first declaration, like in the implementation of that struct for example using a procedural attribute macro.
Is it possible to do something that acts like that ?
From my understanding, it's not, but I could very be wrong.
Even tho that example doesn't really make sens, it can be useful for some cases.
as a function my_proc_attribute_macro[1] that gets the source code it's attached to
fn age() -> usize {
0
}
as input[2] and returns new code that replaces the original.
It cannot really do anything beyond inspecting the code it gets[3] syntactically and produce new code in that place instead.
Some macros might do more fancy things, like try to access some global states via statics with interior mutability, or access the file system to get more information, etc, but that's not the most common thing to do, it also doesn't allow the macro to expand anywhere other than where it's declared, and (for the case of keeping global state) might not stay properly supported in the future.
What you could do of course is make a macro that just gets the whole thing as input, and re-writes it accordingly. E.g. a function-style macro that's called like