#proc-macro #cfg #block #const

cfg_block

简单的crate,允许在块中使用 #[cfg(...)] 和其他宏

3个不稳定版本

0.2.0 2024年1月24日
0.1.1 2022年7月24日
0.1.0 2022年7月24日

#444开发工具

Download history 479/week @ 2024-04-08 515/week @ 2024-04-15 938/week @ 2024-04-22 477/week @ 2024-04-29 757/week @ 2024-05-06 557/week @ 2024-05-13 555/week @ 2024-05-20 477/week @ 2024-05-27 583/week @ 2024-06-03 1839/week @ 2024-06-10 2313/week @ 2024-06-17 2347/week @ 2024-06-24 2178/week @ 2024-07-01 1888/week @ 2024-07-08 1466/week @ 2024-07-15 1504/week @ 2024-07-22

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页面上提出!

无运行时依赖