1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Simple `static_assert` macro for compile time  assertions
//!
//! Uses `const_panic` within `const` variable to produce compile error hence only usable in `const` context.
//!
//! ## Usage
//!
//! ```rust
//! use sa::static_assert;
//!
//! static_assert!(1 == 1);
//! static_assert!(1 == 1, "Must be equal");
//! ```
//!
//! ```compile_fail
//! use sa::static_assert;
//!
//! static_assert!(0 == 1, "Must be equal"); //should fail
//! ```
//!
//! ```compile_fail
//! use sa::static_assert;
//!
//! static_assert!(0 == 1); //should fail
//! ```
#![no_std]

#[macro_export]
///If expression evaluates to `true`, this macro has no effect.
///
///Otherwise a compile-time error is issued via panic.
///
///## Arguments:
///
///- `exp` - expression to evaluate. Result of expression must be `bool`
///- `msg` - optional string literal to add to the error message.
macro_rules! static_assert {
    ($exp:expr) => {
        #[deny(const_err)]
        #[allow(unused_must_use)]
        const _: () = {
            if !($exp) {
                core::panic!(core::concat!("Static assertion '", core::stringify!($exp), "' failed"));
            }

            ()
        };
    };
    ($exp:expr, $msg:literal) => {
        #[deny(const_err)]
        #[allow(unused_must_use)]
        const _: () = {
            if !($exp) {
                core::panic!(core::concat!("Static assertion '", core::stringify!($exp), "' failed: ", $msg));
            }

            ()
        };
    };
}