Skip to content

Commit d410487

Browse files
committed
Add godot-ffi/assertions.rs for both safeguard + static assertions
1 parent 18f94ac commit d410487

File tree

3 files changed

+118
-30
lines changed

3 files changed

+118
-30
lines changed

godot-ffi/src/assertions.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}

godot-ffi/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub(crate) mod gen {
5050

5151
pub mod conv;
5252

53+
mod assertions;
5354
mod extras;
5455
mod global;
5556
mod godot_ffi;

godot-ffi/src/toolbox.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,6 @@ use crate as sys;
1414
// ----------------------------------------------------------------------------------------------------------------------------------------------
1515
// Macros
1616

17-
/// Verifies a condition at compile time.
18-
// https://blog.rust-lang.org/2021/12/02/Rust-1.57.0.html#panic-in-const-contexts
19-
#[macro_export]
20-
macro_rules! static_assert {
21-
($cond:expr) => {
22-
const _: () = assert!($cond);
23-
};
24-
($cond:expr, $msg:literal) => {
25-
const _: () = assert!($cond, $msg);
26-
};
27-
}
28-
29-
/// Verifies at compile time that two types `T` and `U` have the same size and alignment.
30-
#[macro_export]
31-
macro_rules! static_assert_eq_size_align {
32-
($T:ty, $U:ty) => {
33-
godot_ffi::static_assert!(
34-
std::mem::size_of::<$T>() == std::mem::size_of::<$U>()
35-
&& std::mem::align_of::<$T>() == std::mem::align_of::<$U>()
36-
);
37-
};
38-
($T:ty, $U:ty, $msg:literal) => {
39-
godot_ffi::static_assert!(
40-
std::mem::size_of::<$T>() == std::mem::size_of::<$U>()
41-
&& std::mem::align_of::<$T>() == std::mem::align_of::<$U>(),
42-
$msg
43-
);
44-
};
45-
}
46-
4717
/// Trace output.
4818
#[cfg(feature = "debug-log")]
4919
#[macro_export]

0 commit comments

Comments
 (0)