1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! A collection of unsafe functions to use. It is highly recommended you understand
//! `gdnative-rust`'s memory model and read the documentation on these methods. These methods are
//! only really unsafe if you don't understand what they are doing.

pub mod node_ext;
pub mod option_ext;
pub mod result_ext;
pub mod spatial_ext;

/// Same functionality as `panic!()`, but also outputs to the godot output.
#[macro_export]
macro_rules! godot_panic {
    ($($args:tt)*) => {
        {
            gdnative::godot_error!($($args)*);
            panic!($($args)*);
        }
    }
}

/// Same functionality as `assert!()`, but also outputs to the godot output.
#[macro_export]
macro_rules! godot_assert {
    ($condition:expr $(,)?) => {
        if !$condition {
            gdnative::godot_error!("Assertion error: {}", stringify!($condition));
            panic!("Assertion error: {}", stringify!($condition));
        }
    };
    ($condition:expr, $($args:tt)*) => {
        if !$condition {
            gdnative::godot_error!($($args)*);
            panic!($($args)*);
        }
    };
}

/// Same functionality as `debug_assert!()`, but also outputs to the godot output.
#[macro_export]
macro_rules! godot_debug_assert {
    ($condition:expr $(,)?) => {

        if cfg!(debug_assertions) && !$condition {
            gdnative::godot_error!("Assertion error: {}", stringify!($condition));
            panic!("Assertion error: {}", stringify!($condition));
        }
    };
    ($condition:expr, $($args:tt)*) => {
        if cfg!(debug_assertions) && !$condition {
            gdnative::godot_error!($($args)*);
            panic!($($args)*);
        }
    };
}

#[cfg(test)]
mod test {
    #[test]
    fn godot_assert_true() {
        godot_assert!(true)
    }

    #[test]
    fn godot_assert_message_true() {
        godot_assert!(true, "this should not {}", "happen")
    }

    #[test]
    fn godot_debug_assert_true() {
        godot_debug_assert!(true)
    }

    #[test]
    fn godot_debug_assert_message_true() {
        godot_debug_assert!(true, "this should not {}", "happen")
    }
}