Macros 如何在一个宏中创建多个项?

Macros 如何在一个宏中创建多个项?,macros,rust,Macros,Rust,我正在尝试创建一个宏,在这样使用时: foo!() static x: uint = 5; fn bar() -> uint { x + 1 } 扩展为两个项,一个静态项和一个函数。大概是这样的: foo!() static x: uint = 5; fn bar() -> uint { x + 1 } 有人向我指出,MacResult的make\u items方法支持这一点,所以我只需要创建一个正确实现它的类型。我是这样做的: struct MacItems {

我正在尝试创建一个宏,在这样使用时:

foo!()
static x: uint = 5;

fn bar() -> uint { x + 1 }
扩展为两个项,一个静态项和一个函数。大概是这样的:

foo!()
static x: uint = 5;

fn bar() -> uint { x + 1 }
有人向我指出,
MacResult
make\u items
方法支持这一点,所以我只需要创建一个正确实现它的类型。我是这样做的:

struct MacItems {
    items: Vec<::std::gc::Gc<Item>>,
}

impl MacResult for MacItems {
    fn make_def(&self) -> Option<::syntax::ext::base::MacroDef> { None }
    fn make_expr(&self) -> Option<::std::gc::Gc<ast::Expr>> { None }
    fn make_pat(&self) -> Option<::std::gc::Gc<ast::Pat>> { None }
    fn make_stmt(&self) -> Option<::std::gc::Gc<ast::Stmt>> { None }

    fn make_items(&self) -> Option<::syntax::util::small_vector::SmallVector<::std::gc::Gc<Item>>> {
        Some(::syntax::util::small_vector::SmallVector::many(self.items.clone()))
    }
}
下面是错误:

test.rs:37:13: 37:57 error: use of moved value: `cx`
test.rs:37     v.push( quote_item!(cx, fn bar() -> uint { x + 1 }).unwrap() );
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of quote_item!
test.rs:37:13: 37:57 note: expansion site
test.rs:36:13: 36:50 note: `cx` moved here because it has type `&mut syntax::ext::base::ExtCtxt<'_>`, which is moved by default (use `ref` to override)
test.rs:36     v.push( quote_item!(cx, static x: uint = 5;).unwrap() );
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of quote_item!
test.rs:36:13: 36:50 note: expansion site
error: aborting due to previous error
test.rs:37:13:37:57错误:使用移动值:`cx`
test.rs:37v.push(quote_item!(cx,fn bar()->uint{x+1}).unwrap());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
注:在报价单项目的扩展中!
测试rs:37:13:37:57注:扩展站点

test.rs:36:13:36:50注意:`cx`移到这里是因为它的类型为`&mut syntax::ext::base::ExtCtxt,您需要手动将
cx
重新加载到临时
&mut ExtCtxt
。通常,编译器可以自动插入重新启动,但是
quote.*
宏不会扩展到符合此要求的内容

就是

quote_item!(&mut *cx, ...)