libobs_wrapper/
macros.rs

1#[macro_export]
2macro_rules! run_with_obs_impl {
3    ($runtime:expr, $operation:expr) => {
4        $crate::run_with_obs_impl!($runtime, (), $operation)
5    };
6    ($runtime:expr, ($($var:ident),* $(,)*), $operation:expr) => {
7        {
8            $(let $var = $var.clone();)*
9            $runtime.run_with_obs_result(move || {
10                $(let $var = $var;)*
11                let e = {
12                    $(let $var = $var.0;)*
13                    $operation
14                };
15                return e()
16            })
17        }
18    };
19    (SEPARATE_THREAD, $runtime:expr, ($($var:ident),* $(,)*), $operation:expr) => {
20        {
21            $(let $var = $var.clone();)*
22
23            tokio::task::spawn_blocking(move || {
24                $runtime.run_with_obs_result(move || {
25                    $(let $var = $var;)*
26                    let e = {
27                        $(let $var = $var.0;)*
28                        $operation
29                    };
30                    return e()
31                }).unwrap()
32            })
33        }
34    };
35}
36
37#[macro_export]
38macro_rules! run_with_obs {
39    ($runtime:expr, $operation:expr) => {
40        {
41            $crate::run_with_obs_impl!($runtime, $operation)
42                .map_err(|e| $crate::utils::ObsError::InvocationError(e.to_string()))
43        }
44    };
45    ($runtime:expr, ($($var:ident),* $(,)*), $operation:expr) => {
46        {
47            $crate::run_with_obs_impl!($runtime, ($($var),*), $operation)
48                .map_err(|e| $crate::utils::ObsError::InvocationError(e.to_string()))
49        }
50    };
51}
52
53#[macro_export]
54macro_rules! impl_obs_drop {
55    ($struct_name: ident, $operation:expr) => {
56        $crate::impl_obs_drop!($struct_name, (), $operation);
57    };
58    ($struct_name: ident, ($($var:ident),* $(,)*), $operation:expr) => {
59        impl Drop for $struct_name {
60            fn drop(&mut self) {
61                log::trace!("Dropping {}...", stringify!($struct_name));
62
63                $(let $var = self.$var.clone();)*
64                #[cfg(any(not(feature = "no_blocking_drops"), test, feature="__test_environment"))]
65                {
66                    let r = $crate::run_with_obs!(self.runtime, ($($var),*), $operation);
67                    if std::thread::panicking() {
68                        return;
69                    }
70
71                    r.unwrap();
72                }
73
74                #[cfg(all(feature = "no_blocking_drops", not(test), not(feature="__test_environment")))]
75                {
76                    let __runtime = self.runtime.clone();
77                    $crate::run_with_obs_impl!(SEPARATE_THREAD, __runtime, ($($var),*), $operation);
78                }
79            }
80        }
81    };
82}
83
84macro_rules! impl_eq_of_ptr {
85    ($struct: ty, $ptr: ident) => {
86        impl PartialEq for $struct {
87            fn eq(&self, other: &Self) -> bool {
88                self.$ptr.0 == other.$ptr.0
89            }
90        }
91
92        impl Eq for $struct {}
93
94        impl Hash for $struct {
95            fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
96                self.$ptr.0.hash(state);
97            }
98        }
99    };
100}
101
102#[cfg(windows)]
103macro_rules! enum_from_number {
104    ($var: ident, $numb: expr) => {{
105        use num_traits::FromPrimitive;
106        $var::from_i32($numb)
107    }};
108}
109
110#[cfg(not(windows))]
111macro_rules! enum_from_number {
112    ($var: ident, $numb: expr) => {{
113        use num_traits::FromPrimitive;
114        $var::from_u32($numb)
115    }};
116}
117
118pub(crate) use enum_from_number;
119pub(crate) use impl_eq_of_ptr;