libobs_wrapper/
macros.rs

1#[macro_export]
2macro_rules! run_with_obs_impl {
3    ($self:expr, $function:ident, $operation:expr) => {
4        $crate::run_with_obs_impl!($self, $function, (), $operation)
5    };
6    ($self:expr, $function:ident, ($($var:ident),* $(,)*), $operation:expr) => {
7        {
8            $(let $var = $var.clone();)*
9            $self.$function(move || {
10                $(let $var = $var;)*
11                let e = {
12                    $(let $var = $var.0;)*
13                    $operation
14                };
15                return e()
16            })
17        }
18    };
19}
20
21#[macro_export]
22macro_rules! run_with_obs {
23    ($self:expr, $operation:expr) => {
24        {
25            #[cfg(not(feature="blocking"))]
26            use futures_util::TryFutureExt;
27
28            $crate::run_with_obs_impl!($self, run_with_obs_result, $operation)
29                .map_err(|e| crate::utils::ObsError::InvocationError(e.to_string()))
30        }
31    };
32    ($self:expr, ($($var:ident),* $(,)*), $operation:expr) => {
33        {
34            #[cfg(not(feature="blocking"))]
35            use futures_util::TryFutureExt;
36
37            $crate::run_with_obs_impl!($self, run_with_obs_result, ($($var),*), $operation)
38                .map_err(|e| crate::utils::ObsError::InvocationError(e.to_string()))
39        }
40    };
41}
42
43#[macro_export]
44/// This function can only be called OUTSIDE of the OBS thread.
45macro_rules! run_with_obs_blocking {
46    ($self:expr, $operation:expr) => {
47        $crate::run_with_obs_impl!($self, run_with_obs_result_blocking, (), $operation)
48        .map_err(|e| crate::utils::ObsError::InvocationError(e.to_string()))
49    };
50    ($self:expr, ($($var:ident),* $(,)*), $operation:expr) => {
51        $crate::run_with_obs_impl!($self, run_with_obs_result_blocking, ($($var),*), $operation)
52        .map_err(|e| crate::utils::ObsError::InvocationError(e.to_string()))
53    };
54}
55
56#[macro_export]
57macro_rules! impl_obs_drop {
58    ($struct_name: ident, $operation:expr) => {
59        crate::impl_obs_drop!($struct_name, (), $operation);
60    };
61    ($struct_name: ident, ($($var:ident),* $(,)*), $operation:expr) => {
62        impl Drop for $struct_name {
63            fn drop(&mut self) {
64                $(let $var = self.$var.clone();)*
65                #[cfg(not(feature="blocking"))]
66                let r = futures::executor::block_on(async {
67                    return crate::run_with_obs!(self.runtime, ($($var),*), $operation).await
68                });
69
70                #[cfg(feature="blocking")]
71                let r = crate::run_with_obs!(self.runtime, ($($var),*), $operation);
72                if std::thread::panicking() {
73                    return;
74                }
75
76                r.unwrap();
77            }
78        }
79    };
80    (is_runtime, $struct_name: ident, ($($var:ident),* $(,)*), $operation:expr) => {
81        impl Drop for $struct_name {
82            fn drop(&mut self) {
83                $(let $var = self.$var.clone();)*
84                let r = crate::run_with_obs_blocking!(self, ($($var),*), $operation);
85                if std::thread::panicking() {
86                    return;
87                }
88
89                r.unwrap();
90            }
91        }
92    };
93}