在定义表单的某些元组结构时,我有几个宏来减少样板:
macro_rules! new_type (($name:ident, $bytes:expr) => (
pub struct $name(pub [u8; $bytes]);
// some common operations on $name
));
Run Code Online (Sandbox Code Playgroud)
但是,我还要记录这些新结构.如果我能在宏调用之前编写我的文档,最好的事情就是.
/// A certain type
new_type!(CertainType, 42);
Run Code Online (Sandbox Code Playgroud)
但是,Rust不会生成CertainType
发生这种情况的文档.
另一种(不那么灵活)替代方案是做类似的事情:
macro_rules! new_type (($name:ident, $bytes:expr) => (
/// Some more generic documentation for $name
pub struct $name(pub [u8; $bytes]);
// some common operations on $name
));
Run Code Online (Sandbox Code Playgroud)
但是,在执行此操作时,Rust宏系统不会$name
在文档注释中展开令牌.剩下的唯一选择是在宏中编写非常通用的文档,但这会导致我的库文档记录得比它更糟糕.
您对此有何建议?对我来说最好的解决方案是能够为每个宏调用编写特定的文档,但如果不可能,我会很感激有关如何在文档注释中扩展标记的提示.
我想在宏生成的文档中使用宏变量:
macro_rules! impl_foo {
($name:ident) => {
/// Returns a new `$name`.
fn myfoo() -> $name {
}
};
}
Run Code Online (Sandbox Code Playgroud)
但是,变量不会被替换.我也尝试过使用这个#[doc]
属性:
macro_rules! impl_foo {
($name:ident) => {
#[doc = concat!("Returns a new `", $name, "`.")]
fn myfoo() -> $name {
}
};
}
Run Code Online (Sandbox Code Playgroud)
这个甚至无法解析: unexpected token: 'concat'
如果readme
Cargo.toml
设置了键,doc.rs 会在 crate 的索引页面上呈现 README。在cargo doc
本地运行时有没有办法模拟这一点?
如果我添加:
#![doc = r###"contents
of
README.md
here
"###]
Run Code Online (Sandbox Code Playgroud)
作为字面意思,我得到了我正在寻找的行为,但是内联整个 README.md 的副本对于进行更新非常不方便。
我试过:
#![doc = include!("README.md")]
Run Code Online (Sandbox Code Playgroud)
但这给出了一个错误:
error: unexpected token: `include`
--> src/lib.rs:3:10
|
3 | #![doc = include!("README.md")]
| ^^^^^^^
Run Code Online (Sandbox Code Playgroud) 我正在尝试做类似以下的事情:
macro_rules! attr_trial {
($msg:expr) => {{
let id = env!("SOME_ENV");
#[link_section = env!("SOME_ENV")]
static MESSAGE: &'static str = $msg;
}};
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: unexpected token: `env`
--> src/main.rs:34:18
|
34 | #[link_section = env!("SOME_ENV")]
| ^
Run Code Online (Sandbox Code Playgroud)