Skip to main content

draco_oxide_core/utils/
debug.rs

1#[macro_export]
2macro_rules! debug_write {
3    ($msg:literal, $writer:expr) => {
4        #[cfg(feature = "debug_format")]
5        {
6            for byte in $msg.as_bytes() {
7                $writer.write_u8(*byte);
8            }
9        }
10    };
11}
12
13#[macro_export]
14macro_rules! debug_expect {
15    ($msg:literal, $reader:expr) => {
16        #[cfg(feature = "debug_format")]
17        {
18            for byte in $msg.as_bytes() {
19                assert!(
20                    *byte == $reader.read_u8().unwrap(),
21                    "Expected {:?}, but did not match.",
22                    $msg
23                );
24            }
25        }
26    };
27}
28
29/// Safety assertion guarding `unsafe` preconditions and internal invariants.
30///
31/// A failing safety assertion indicates a bug in this library, never a runtime or
32/// input condition; it is not a hardening check. The checks run only in debug
33/// builds, and only while the (default-on) `safety_assertions` feature is enabled
34/// — i.e. `all(debug_assertions, feature = "safety_assertions")`. Release builds
35/// never run them, and disabling the feature (`--no-default-features`) drops them
36/// from debug builds too.
37///
38/// Like `debug_write!`/`debug_expect!`, the `feature = "safety_assertions"` cfg
39/// resolves at the call site, so every crate invoking this macro must declare the
40/// (default-on) `safety_assertions` feature (forwarding into core).
41#[macro_export]
42macro_rules! safety_assert {
43    ($($arg:tt)*) => {
44        #[cfg(all(debug_assertions, feature = "safety_assertions"))]
45        {
46            ::core::assert!($($arg)*);
47        }
48    };
49}
50
51/// `assert_eq!` counterpart of [`safety_assert!`]; see that macro for semantics.
52#[macro_export]
53macro_rules! safety_assert_eq {
54    ($($arg:tt)*) => {
55        #[cfg(all(debug_assertions, feature = "safety_assertions"))]
56        {
57            ::core::assert_eq!($($arg)*);
58        }
59    };
60}