|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +//! Assertion macros for compile-time and runtime checks with different safeguard levels. |
| 9 | +
|
| 10 | +// ---------------------------------------------------------------------------------------------------------------------------------------------- |
| 11 | +// Compile-time assertions |
| 12 | + |
| 13 | +/// Verifies a condition at compile time. |
| 14 | +// https://blog.rust-lang.org/2021/12/02/Rust-1.57.0.html#panic-in-const-contexts |
| 15 | +#[macro_export] |
| 16 | +macro_rules! static_assert { |
| 17 | + ($cond:expr) => { |
| 18 | + const _: () = assert!($cond); |
| 19 | + }; |
| 20 | + ($cond:expr, $msg:literal) => { |
| 21 | + const _: () = assert!($cond, $msg); |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +/// Verifies at compile time that two types `T` and `U` have the same size and alignment. |
| 26 | +#[macro_export] |
| 27 | +macro_rules! static_assert_eq_size_align { |
| 28 | + ($T:ty, $U:ty) => { |
| 29 | + godot_ffi::static_assert!( |
| 30 | + std::mem::size_of::<$T>() == std::mem::size_of::<$U>() |
| 31 | + && std::mem::align_of::<$T>() == std::mem::align_of::<$U>() |
| 32 | + ); |
| 33 | + }; |
| 34 | + ($T:ty, $U:ty, $msg:literal) => { |
| 35 | + godot_ffi::static_assert!( |
| 36 | + std::mem::size_of::<$T>() == std::mem::size_of::<$U>() |
| 37 | + && std::mem::align_of::<$T>() == std::mem::align_of::<$U>(), |
| 38 | + $msg |
| 39 | + ); |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +// ---------------------------------------------------------------------------------------------------------------------------------------------- |
| 44 | +// Runtime assertions - strict mode |
| 45 | + |
| 46 | +/// Acts like `assert!` when `safeguards_strict` is enabled (default in debug builds), and becomes a no-op otherwise. |
| 47 | +#[macro_export] |
| 48 | +macro_rules! strict_assert { |
| 49 | + ($($arg:tt)*) => { |
| 50 | + #[cfg(safeguards_strict)] |
| 51 | + assert!($($arg)*); |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/// Acts like `assert_eq!` when `safeguards_strict` is enabled (default in debug builds), and becomes a no-op otherwise. |
| 56 | +#[macro_export] |
| 57 | +macro_rules! strict_assert_eq { |
| 58 | + ($actual:expr, $expected:expr) => { |
| 59 | + #[cfg(safeguards_strict)] |
| 60 | + assert_eq!($actual, $expected); |
| 61 | + }; |
| 62 | + ($actual:expr, $expected:expr, $($arg:tt)*) => { |
| 63 | + #[cfg(safeguards_strict)] |
| 64 | + assert_eq!($actual, $expected, $($arg)*); |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +/// Acts like `assert_ne!` when `safeguards_strict` is enabled (default in debug builds), and becomes a no-op otherwise. |
| 69 | +#[macro_export] |
| 70 | +macro_rules! strict_assert_ne { |
| 71 | + ($actual:expr, $expected:expr) => { |
| 72 | + #[cfg(safeguards_strict)] |
| 73 | + assert_ne!($actual, $expected); |
| 74 | + }; |
| 75 | + ($actual:expr, $expected:expr, $($arg:tt)*) => { |
| 76 | + #[cfg(safeguards_strict)] |
| 77 | + assert_ne!($actual, $expected, $($arg)*); |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +// ---------------------------------------------------------------------------------------------------------------------------------------------- |
| 82 | +// Runtime assertions - balanced mode |
| 83 | + |
| 84 | +/// Acts like `assert!` when `safeguards_balanced` is enabled, and becomes a no-op otherwise. |
| 85 | +#[macro_export] |
| 86 | +macro_rules! balanced_assert { |
| 87 | + ($($arg:tt)*) => { |
| 88 | + #[cfg(safeguards_balanced)] |
| 89 | + assert!($($arg)*); |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +/// Acts like `assert_eq!` when `safeguards_balanced` is enabled, and becomes a no-op otherwise. |
| 94 | +#[macro_export] |
| 95 | +macro_rules! balanced_assert_eq { |
| 96 | + ($actual:expr, $expected:expr) => { |
| 97 | + #[cfg(safeguards_balanced)] |
| 98 | + assert_eq!($actual, $expected); |
| 99 | + }; |
| 100 | + ($actual:expr, $expected:expr, $($arg:tt)*) => { |
| 101 | + #[cfg(safeguards_balanced)] |
| 102 | + assert_eq!($actual, $expected, $($arg)*); |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +/// Acts like `assert_ne!` when `safeguards_balanced` is enabled, and becomes a no-op otherwise. |
| 107 | +#[macro_export] |
| 108 | +macro_rules! balanced_assert_ne { |
| 109 | + ($actual:expr, $expected:expr) => { |
| 110 | + #[cfg(safeguards_balanced)] |
| 111 | + assert_ne!($actual, $expected); |
| 112 | + }; |
| 113 | + ($actual:expr, $expected:expr, $($arg:tt)*) => { |
| 114 | + #[cfg(safeguards_balanced)] |
| 115 | + assert_ne!($actual, $expected, $($arg)*); |
| 116 | + }; |
| 117 | +} |
0 commit comments