From 7bb2babe0cb8047b67449b0cc9b5c534a8e39ce0 Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Wed, 6 Sep 2023 14:33:28 +0200 Subject: [PATCH] static_assert!() --- src/debug.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/debug.rs b/src/debug.rs index 74a25d6..b66b7e0 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -228,6 +228,15 @@ macro_rules! dbg_rot { }; } +/// Same as `assert!` but at compile time. +#[macro_export] +macro_rules! static_assert { + ($($arg:tt)+) => { + const _: () = assert!($($arg)+); + }; +} +// There is no corresponding static_assert_{eq,ne} because assert_{eq,ne} call a non-const function. + /// Same as `assert!` but only prints a message without crashing. #[macro_export] macro_rules! soft_assert { @@ -480,7 +489,13 @@ where } } +// +// +// ======================================================================= // The public debugging API is above, shared implementation details below. +// ======================================================================= +// +// // LATER(multithreading) Make debug tools work correctly from all threads. thread_local! { @@ -674,7 +689,12 @@ mod tests { } #[test] - fn test_soft_assert() { + fn test_static_asserts() { + static_assert!(2 + 2 == 4); + } + + #[test] + fn test_soft_asserts() { #![allow(clippy::let_unit_value)] // We need to test that the macros eval to a () #![allow(clippy::nonminimal_bool)] #![allow(clippy::redundant_clone)]