如何在隐式导出宏时使用模块中定义的构造

How to use constructs defined in module in exporting macro implicitly

正在尝试从模块中导出宏。宏生成实现模块中定义的一些特征的结构。有没有办法在不手动导入特征的情况下获得宏?

// src/lib.rs
#![crate_name="macro_test"]
#![crate_type="lib"]
#![crate_type="rlib"]

pub trait B<T> where T: Copy {
    fn new(x: T) -> Self;
}

#[macro_export]
macro_rules! test {
    ( $n:ident ) => {
        struct $n<T> where T: Copy {
            x: T
        }

        impl<T> B<T> for $n<T> where T: Copy {
            fn new(x: T) -> Self {
                $n { x: x }
            }
        }
    } 
}

// tests/test_simple.rs
#[macro_use]
extern crate macro_test;

test!(Test);

#[test]
fn test_macro() {
    let a = Test::<i32>::new(1);
}

在那种情况下我得到一个错误:

<macro_test macros>:2:54: 2:61 error: use of undeclared trait name `B` [E0405]
<macro_test macros>:2 struct $ n < T > where T : Copy { x : T } impl < T > B < T > for $ n < T >

如果我用 $crate 变量重写特征实现:

impl<T> $crate::B<T> for $n<T> where T: Copy {

错误消息更改为下一个:

tests\test_simple.rs:8:13: 8:29 error: no associated item named `new` found for type `Test<i32>` in the current scope
tests\test_simple.rs:8     let a = Test::<i32>::new(1);
                               ^~~~~~~~~~~~~~~~
tests\test_simple.rs:8:13: 8:29 help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a `use` for it:
tests\test_simple.rs:8:13: 8:29 help: candidate #1: use `macro_test::B`

为什么会这样?

因为你不能在没有 use 特征的情况下调用特征方法。这与宏无关——这只是 Rust 中的标准规则。

也许您希望宏生成一个固有的 impl?

impl<T> $n<T> where T: Copy {
    pub fn new(x: T) -> Self {
        $n { x: x }
    }
}

而不是你目前拥有的。