rust - 将成员函数体作为宏参数传递

标签 rust

我正在尝试将成员函数的主体作为宏参数传递。是否可以更改下面的代码以使其工作?

macro_rules! iterator{
    ($ty:ty, $ident:ident; $($state_ident:ident: $state_ty:ty), *; $next:block) => (
        struct $ident {                                         // ^ the parameter
            $($state_ident: $state_ty), *
        }

        impl Iterator for $ident {
            type Item = $ty;

            fn next(&mut self) -> Option<$ty> {
                $next // <- cannot refer to 'self' parameter in this block
            }
        }
    );
}

iterator!(i32, TestIterator; index: i32; {
    let value = Some(self.index);
    self.index += 1;
    value
});

playground

编译器错误:

error[E0424]: expected value, found module `self`
  --> src/main.rs:18:22
   |
18 |     let value = Some(self.index);
   |                      ^^^^ `self` value is only available in methods with `self` parameter

error[E0424]: expected value, found module `self`
  --> src/main.rs:19:5
   |
19 |     self.index += 1;
   |     ^^^^ `self` value is only available in methods with `self` parameter

最佳答案

一个解决方案是接受闭包而不是 block :

macro_rules! iterator{
    ($ty:ty, $ident:ident; $($state_ident:ident: $state_ty:ty),*; $next:expr) => (
        struct $ident {
            $($state_ident: $state_ty), *
        }

        impl Iterator for $ident {
            type Item = $ty;

            fn next(&mut self) -> Option<$ty> {
                $next(self)
            }
        }
    );
}

iterator!(i32, TestIterator; index: i32; |me: &mut TestIterator| {
    let value = Some(me.index);
    me.index += 1;
    value
});

fn main() {}

这需要明确地将 self 传递给闭包。您不能在闭包中使用标识符 self,因为 self 只允许在函数的参数列表中声明。

您还需要指定闭包参数的类型,这是定义为变量并稍后使用而不是立即使用的闭包的限制。

关于rust - 将成员函数体作为宏参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953262/

相关文章:

ipc - 你如何在 Rust 中进行进程间通信 (IPC)?

pointers - 堆对象的堆栈引用

rust - 为什么不引用其中一个字符串就无法在 Rust 中连接两个字符串?

rust - 如何正确退出 mpsc::Receiver 上的线程阻塞

rust - GTK4 : How to specify rows and columns of a GtkGrid object within a GtkBuilder ui file

rust - 为引用和非引用类型实现特征会导致实现冲突

string - 将 int 转换为字符串向量

rust - Rust 1.3.0 中的猜谜游戏

rust - 错误 : duplicate lang item in crate `sp_io`

windows - OpenGL gl::GetUniformLocation仅在某些情况下无法定位统一