rust - 在编译时将对象添加到数组的宏?
问题描述
我想创建一个静态编译时生成的变量列表。也就是说,当程序启动时,它们已经存在,我不需要对其进行变异。
目前我正在这样做:
pub type ProfilerInfo = (&'static str, &'static str);
pub const Fps: ProfilerInfo = ("fps", "Frames Per Second");
pub const DecodingElapsed: ProfilerInfo = ("det", "Decoding Elapsed Time");
pub const DecodedPerSec: ProfilerInfo = ("dps", "Decoded Per Second");
pub const BytesPerSec: ProfilerInfo = ("bps", "Bytes Per Second");
pub const ALL_VARIABLES: &'static [&'static ProfilerInfo] = &[&Fps, &DecodingElapsed, &DecodedPerSec, &BytesPerSec];
但是这很容易出错,我可能会忘记在列表中添加变量之一ALL_VARIABLES
。
有没有办法创建一个创建ProfilerInfo
并添加到的宏ALL_VARIABLES
?
背景:为什么我需要这个?好吧,这个列表是这样由命令行返回的my_program --list-variables
,所以最好在应用程序启动时完成。
我认为不可能像我在顶部那样使用数组,但也许使用Vec
?
解决方案
您需要一个为您创建ALL_VARIABLES
静态切片的宏。
pub type ProfilerInfo = (&'static str, &'static str);
macro_rules! profiler_variables {
($(pub const $id:ident: ProfilerInfo = $rhs:expr;)*) => {
$(
pub const $id: ProfilerInfo = $rhs;
)*
pub const ALL_VARIABLES: &'static [&'static ProfilerInfo] = &[$(&$id),*];
}
}
profiler_variables! {
pub const Fps: ProfilerInfo = ("fps", "Frames Per Second");
pub const DecodingElapsed: ProfilerInfo = ("det", "Decoding Elapsed Time");
pub const DecodedPerSec: ProfilerInfo = ("dps", "Decoded Per Second");
pub const BytesPerSec: ProfilerInfo = ("bps", "Bytes Per Second");
}
fn main() {
for (name, descr) in ALL_VARIABLES.iter() {
println!("Variable: {}, description: {}", name, descr)
}
}
输出:
Variable: fps, description: Frames Per Second
Variable: det, description: Decoding Elapsed Time
Variable: dps, description: Decoded Per Second
Variable: bps, description: Bytes Per Second
推荐阅读
- javascript - Discord.JS 包本身的语法错误
- mysql - CentOS 7 上的 MariaDB 拒绝读取一些更新的变量
- ansible - 获取变量的正确值
- python - 有没有办法可以列出这个清单?
- c++ - 在没有单例的情况下使用基于函数指针的操作系统抽象层
- apache-flink - FLINK:java.io.IOException:网络缓冲区数量不足
- excel - 从excel中的大数据表中提取报告表
- python - 在 rdkit 中处理 SMILES 数据时出现 python 参数错误
- laravel-4 - 无法使用 laravel 4.2 swiftmailer 随机连接 TLS 加密
- php - Woocommerce:在帐户下载中显示产品缩略图