Access to self from the parameters of a macro that creates a struct

前端 未结 2 1017
无人及你
无人及你 2021-01-22 05:54

I\'m trying to write a macro that generates a struct. The implementation for the struct will be generated by the macro, but some blocks of code will be provided as macro argumen

2条回答
  •  梦谈多话
    2021-01-22 06:25

    You can pass a closure instead of a block.

    make_struct!(Test |this| println!("Foo: {:?}", this.foo));
    

    Then the macro can use the closure and call it with self:

    macro_rules! make_struct {
        ($name:ident $closure:expr) => {
            struct $name {
                foo: i32,
            }
    
            impl $name {
                fn new() -> Self {
                    $name { foo: 42 }
                }
                fn act (&self) {
                    let x: &Fn(&Self) = &$closure;
                    x(self)
                }
            }
        };
    }
    

    The dance with the let binding is necessary, because the type of this in the closure can't be inferred (yet?). And it also makes your macro's error reporting a little more readable when something other than a closure is passed.

提交回复
热议问题