如何编写一个为多个结构实现相同方法的宏?方法应该根据结构略有不同。
代码应该是这样的:
macro_rules! say_name {
(for $($t:ty),+) => {
$(impl $t {
fn say_name(&self) {
//if $t == A then self.say_a();
//if $t == B then self.say_b();
}
})*
};
}
struct A {
}
impl A {
fn say_a(&self) {
println!("A");
}
}
struct B {
}
impl B {
fn say_b(&self) {
println!("B");
}
}
say_name!(for A, B);
fn main() {
let a = A {};
let b = B {};
a.say_name();
b.say_name();
}
我的代码有点复杂,所以我为这个问题提供了一个伪代码。
您可以像这样将方法名称传递给宏
macro_rules! say_name {
(for $($t:ty = ($f:ident) ),+) => {
$(impl $t {
fn say_name(&self) {
<$t>::$f(self)
}
})*
};
}
它现在被称为
say_name!(for A = (say_a), B = (say_b));
操场
您可以在宏定义旁边定义一个特征,宏定义可以使用该特征;specialized";对于A或B:
pub mod private { // this mod has to be pub so the expanded code can use it
pub trait SayNameImpl {
fn say_name_impl(&self);
}
impl SayNameImpl for super::A {
fn say_name_impl(&self) {
self.say_a();
}
}
impl SayNameImpl for super::B {
fn say_name_impl(&self) {
self.say_b();
}
}
}
macro_rules! say_name {
(for $($t:ty),+) => {
$(impl $t {
fn say_name(&self) {
use $crate::private::SayNameImpl;
self.say_name_impl();
}
})*
};
}
游乐场