Is there some way to store values externally, in a macro call, so that over multiple invocations of the macro I could build up a collection of those values?
The context: I'm thinking about a Rust wrapper for a C++ API with virtual function callbacks, like Button in How to use C++ polymorphism in Rust
What's missing there is that in such classes, you don't usually implement all the callbacks. There may in fact be quite a boatload of callbacks, of which most are rarely used. The Rust wrapper can implement them all and let the library user override the default definitions (I think), but that's a fair amount of function calls and marshaling for virtual functions where perhaps no one will ever actually provide a non-default implementation. So if there were a convenient way to pick up which functions the user actually does implement, it might be worth some trouble.
like,
impl Button for CounterButton {
#[buttonVfn(Click)]
fn click(&mut self) {
...
(Which also takes care of the C++ API's camel case.)
I'm sure there's a means in Rust's intriguing macro system to pull an enum ButtonCallback::Click(click)
out of that -- but I need to put it somewhere, so I think (?) I need to accumulate those data in a compile time collection, to, say, dump them out at the end of the impl
block in an endButtonVfn!()
or something. I tried redefining a macro, but Rust just saw that as an ambiguous set of macro definitions.