konst/macros/control_flow.rs
1/// Emulates the [inline const feature](`const{ ... }`) in pre-1.79 versions.
2///
3/// As opposed to inline const, you must pass the type that the expression evaluates to.
4///
5/// # Limitations
6///
7/// This can't be used with expressions that reference generic parameters.
8///
9/// # Example
10///
11/// ```rust
12/// use konst::{konst, eq_str};
13///
14/// const FOO: &str = "hello";
15///
16/// # const _: bool = konst!{bool, eq_str(FOO, "hi")};
17/// #
18/// // By using `konst` here, the function is unconditionally evaluated at compile-time.
19/// if konst!{bool, eq_str(FOO, "hi")} {
20/// panic!("The constants are equal, this wasn't supposed to happen!!");
21/// }
22///
23/// ```
24///
25/// [Rust 1.79.0]:
26/// https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html#inline-const-expressions
27///
28/// [inline const feature]:
29/// https://blog.rust-lang.org/2024/06/13/Rust-1.79.0.html#inline-const-expressions
30#[macro_export]
31macro_rules! konst {
32 ($type:ty, $expr:expr $(,)*) => {{
33 const __KONST__: $type = $expr;
34 __KONST__
35 }};
36}