I would love to be able to something like the following:
macro_rules! impl_a_method(
($obj:ident, $body:block) => (
fn a_method(foo: Foo, bar: Bar, baz: Baz) -> $obj $body
)
)
// Implementation would look like:
impl_a_method!(MyType, {
MyType {
foo: foo.blah(),
bar: bar.bloo(),
baz: baz.floozy(),
}
})
My real-world example features methods with much larger signatures which I have to implement in unique ways for 30+ different types.
I have tried something similar to the above macro, however I run into errors where rustc considers foo
, bar
and baz
unresolved names at the expansion site (even though I'm sure the macro declaration lexically precedes the use).
Is it possible to do something like this?
If not, can you recommend an approach that would achieve something similar?