3个不稳定版本
0.2.0 | 2024年1月24日 |
---|---|
0.1.1 | 2022年7月24日 |
0.1.0 | 2022年7月24日 |
#444 在 开发工具
7,177 每月下载量
用于 diesel-connection
11KB
86 行
cfg_block
一个简单的库,用于将过程宏应用于块,以简化 const
值和更易于阅读的代码。在此获取文档 https://docs.rs/cfg_block。
以下是一个简单的示例,用于根据平台定义变量
use cfg_block::cfg_block;
cfg_block!{
#[cfg(target_family = "unix")] {
const PLATFORM: &str = "posix !";
const MY_NUMBER: u8 = 5;
}
#[cfg(target_family = "windows")] {
const PLATFORM: &str = "window !";
const MY_NUMBER: u16 = 20;
}
#[cfg(target_family = "wasm")] {
const PLATFORM: &str = "web !";
const MY_NUMBER: i32 = -5;
}
}
// Assuming this test runs on linux/macos...
assert_eq!(PLATFORM, "posix !");
assert_eq!(MY_NUMBER, 5);
上述示例演示了使用 #[cfg()]
,但它适用于任何过程宏。在幕后,它只是在每个块的项目之前插入宏属性,并移除块包装器。
此宏还支持简单的if/else配置
cfg_block!{
if #[cfg(mips)] {
const STR_A: &str = "where did you get this processor";
const STR_B: &str = "mips just makes a good passing doctest";
} else {
const STR_A: &str = "good!";
const STR_B: &str = "better!";
}
}
assert_eq(STR_A, "good!");
assert_eq(STR_B, "better!");
请注意,与通用语法不同,此if/else语法仅适用于 #[cfg(something)]
(它只是将其替换为 #[cfg(not(something))]
)。
链接
到 crates.io
页面的链接: https://crates.io/crates/cfg_block
在此获取文档: https://docs.rs/cfg_block
源代码仓库: https://github.com/pluots/cfg_block
如果您有任何改进或想法,请随时在GitHub页面上提出!