Hi guys, this is my demo code:
use std::fmt::Display;
trait A: Display {
fn f(&self) -> String;
}
struct Aa {}
impl Display for Aa {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
impl A for Aa {
fn f(&self) -> String {
format!("{}", self)
}
}
So far, everything is good enough. However, I want to write the macro to generate this demo code.
macro_rules! demo_impl {
($implName:ident, $body:block) => {
impl $implName for Aa {
fn f(&self) -> String
$body
}
};
}
demo_impl!(A, { format!("{}", self) });
Then it gives me the error that
--> src/main.rs:30:31
|
24 | / fn f(&self) -> String
25 | | $body
| |_____________________- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
...
30 | demo_impl!(A, { format!("{}", self) });
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
I expanded this macro but it looks fine. Is there anyway I can let macro ignore the body it is trying to check? Or there is another way to make this? Thanks!