mlua_codemp_patch/
macros.rs

1macro_rules! bug_msg {
2    ($arg:expr) => {
3        concat!(
4            "mlua internal error: ",
5            $arg,
6            " (this is a bug, please file an issue)"
7        )
8    };
9}
10
11macro_rules! cstr {
12    ($s:expr) => {
13        concat!($s, "\0") as *const str as *const [::std::os::raw::c_char] as *const ::std::os::raw::c_char
14    };
15}
16
17macro_rules! mlua_panic {
18    ($msg:expr) => {
19        panic!(bug_msg!($msg))
20    };
21
22    ($msg:expr,) => {
23        mlua_panic!($msg)
24    };
25
26    ($msg:expr, $($arg:expr),+) => {
27        panic!(bug_msg!($msg), $($arg),+)
28    };
29
30    ($msg:expr, $($arg:expr),+,) => {
31        mlua_panic!($msg, $($arg),+)
32    };
33}
34
35macro_rules! mlua_assert {
36    ($cond:expr, $msg:expr) => {
37        assert!($cond, bug_msg!($msg));
38    };
39
40    ($cond:expr, $msg:expr,) => {
41        mlua_assert!($cond, $msg);
42    };
43
44    ($cond:expr, $msg:expr, $($arg:expr),+) => {
45        assert!($cond, bug_msg!($msg), $($arg),+);
46    };
47
48    ($cond:expr, $msg:expr, $($arg:expr),+,) => {
49        mlua_assert!($cond, $msg, $($arg),+);
50    };
51}
52
53macro_rules! mlua_debug_assert {
54    ($cond:expr, $msg:expr) => {
55        debug_assert!($cond, bug_msg!($msg));
56    };
57
58    ($cond:expr, $msg:expr,) => {
59        mlua_debug_assert!($cond, $msg);
60    };
61
62    ($cond:expr, $msg:expr, $($arg:expr),+) => {
63        debug_assert!($cond, bug_msg!($msg), $($arg),+);
64    };
65
66    ($cond:expr, $msg:expr, $($arg:expr),+,) => {
67        mlua_debug_assert!($cond, $msg, $($arg),+);
68    };
69}
70
71macro_rules! mlua_expect {
72    ($res:expr, $msg:expr) => {
73        $res.expect(bug_msg!($msg))
74    };
75
76    ($res:expr, $msg:expr,) => {
77        mlua_expect!($res, $msg)
78    };
79}
80
81#[cfg(feature = "module")]
82#[doc(hidden)]
83#[macro_export]
84macro_rules! require_module_feature {
85    () => {};
86}
87
88#[cfg(not(feature = "module"))]
89#[doc(hidden)]
90#[macro_export]
91macro_rules! require_module_feature {
92    () => {
93        compile_error!("Feature `module` must be enabled in the `mlua` crate");
94    };
95}
96
97macro_rules! protect_lua {
98    ($state:expr, $nargs:expr, $nresults:expr, $f:expr) => {
99        crate::util::protect_lua_closure($state, $nargs, $nresults, $f)
100    };
101
102    ($state:expr, $nargs:expr, $nresults:expr, fn($state_inner:ident) $code:expr) => {{
103        use ::std::os::raw::c_int;
104        unsafe extern "C-unwind" fn do_call($state_inner: *mut ffi::lua_State) -> c_int {
105            $code;
106            let nresults = $nresults;
107            if nresults == ::ffi::LUA_MULTRET {
108                ffi::lua_gettop($state_inner)
109            } else {
110                nresults
111            }
112        }
113
114        crate::util::protect_lua_call($state, $nargs, do_call)
115    }};
116}