From ca4603ff028b8768f8eef28ec0fab48d29aaa976 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 18 Jul 2022 11:04:52 +0200 Subject: [PATCH] Add 'static_assert' macro --- src/lib.rs | 1 + src/utils/mod.rs | 2 ++ src/utils/static_assert.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/utils/mod.rs create mode 100644 src/utils/static_assert.rs diff --git a/src/lib.rs b/src/lib.rs index 667c357..3593172 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod board; +pub mod utils; diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..2833a48 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1,2 @@ +pub mod static_assert; +pub use static_assert::*; diff --git a/src/utils/static_assert.rs b/src/utils/static_assert.rs new file mode 100644 index 0000000..81b7a86 --- /dev/null +++ b/src/utils/static_assert.rs @@ -0,0 +1,28 @@ +//! Static assert. + +/// Assert a condition at compile-time. +/// +/// See [RFC 2790] for potential addition into Rust itself. +/// +/// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790 +/// +/// # Examples +/// +/// ``` +/// use seer::utils::static_assert; +/// +/// static_assert!(42 > 0); +/// ``` +#[macro_export] +macro_rules! static_assert { + ($condition:expr) => { + // Based on the latest one in `rustc`'s one before it was [removed]. + // + // [removed]: https://github.com/rust-lang/rust/commit/c2dad1c6b9f9636198d7c561b47a2974f5103f6d + #[allow(dead_code)] + const _: () = [()][!($condition) as usize]; + }; +} + +// I want it namespaced, even though it is exported to the root of the crate by `#[macro_export]`. +pub use static_assert;