macro_rules! foo_macro {
($name:ident) => {
pub struct $name;
}
}
/// Doc comment for Foo struct.
foo_macro!(Foo);
/// Doc comment for Bar struct.
foo_macro!(Bar);
在这个简单的代码示例中,用户希望通过宏扩展创建一个结构体,并为每个不同的宏调用编写唯一的注释。
这意味着 struct Foo 和 Bar 的独特注释,目前在 rust 编译器中启用时是不可能的:
#![warn(missing_docs)]
带有以下警告:
warning: missing documentation for a struct
--> src/activation_fn.rs:9:3
|
9 | pub struct $name;
| ^^^^^^^^^^^^^^^^^
...
14 | foo_macro!(Foo);
| ---------------- in this macro invocation
|
note: lint level defined here
--> src/lib.rs:1:9
|
1 | #![warn(missing_docs)]
| ^^^^^^^^^^^^
warning: missing documentation for a struct
--> src/activation_fn.rs:9:3
|
9 | pub struct $name;
| ^^^^^^^^^^^^^^^^^
...
17 | foo_macro!(Bar);
| ---------------- in this macro invocation
|
note: lint level defined here
--> src/lib.rs:1:9
|
1 | #![warn(missing_docs)]
| ^^^^^^^^^^^^
虽然这显然有效:
macro_rules! foo_macro {
/// Unified comment for any macro expansion which is bad!
($name:ident) => {
pub struct $name;
}
}
foo_macro!(Foo);
foo_macro!(Bar);
您可以只转发所有属性(文档注释是#[doc]
属性),但是由于属性的重复不能直接跟在 ident 后面,因此需要稍微更改语法。
示例(游乐场)
macro_rules! foo_macro {
($(#[$attr:meta])* struct $name:ident) => {
$(#[$attr])*
pub struct $name;
}
}
foo_macro!(
/// Doc comment for Foo struct.
struct Foo
);
foo_macro!(
/// Doc comment for Bar struct.
struct Bar
);
fn main() {
}
用户论坛是讨论此类问题的好地方。 https://users.rust-lang.org/
在这里非常欢迎具体的错误报告,但我会关闭它,因为它已经得到了回答,最好在用户论坛上进行进一步的讨论。
这里的问题似乎没有解决。 如何为以ident
开头的宏编写文档注释?
最有用的评论
这里的问题似乎没有解决。 如何为以
ident
开头的宏编写文档注释?