Crates.io | cfg_block |
lib.rs | cfg_block |
version | 0.2.0 |
source | src |
created_at | 2022-07-24 00:26:46.206084 |
updated_at | 2024-01-24 18:45:27.071545 |
description | Simple crate to allow using #[cfg(...)] and other macros on blocks |
homepage | |
repository | https://github.com/pluots/cfg_block |
max_upload_size | |
id | 631780 |
size | 14,170 |
A simple library for applying procedural macros to a block, for easier const
values and more readable code. Grab the docs here https://docs.rs/cfg_block.
Here's a simple example for defining variables based on platform:
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);
The above example demonstrates using #[cfg()]
, but it should work for any
procedural macro. Behind the scenes, it just inserts the macro attribute before
every item in the block, and removes the block wrapper.
This macro also supports a simple if/else configuration:
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!");
Please note that unlike the general syntax, this if/else syntax only works with
#[cfg(something)]
(it just replaces it with #[cfg(not(something))]
).
Link to crates.io
page: https://crates.io/crates/cfg_block
Get the docs here: https://docs.rs/cfg_block
Source repository: https://github.com/pluots/cfg_block
If you have any improvements or ideas, feel free to bring them up on the Github page!