arcon_state/backend/
macros.rs

1// region helper macros for `with_backend_type!`
2#[doc(hidden)]
3#[cfg(feature = "rocks")]
4#[macro_export]
5macro_rules! cfg_if_rocks {
6    (@pat $i: pat) => {
7        $i
8    };
9    ($($body:tt)*) => {
10        $($body)*
11    };
12}
13
14#[doc(hidden)]
15#[cfg(not(feature = "rocks"))]
16#[macro_export]
17macro_rules! cfg_if_rocks {
18    (@pat $i: pat) => {
19        _
20    };
21    ($($body:tt)*) => {
22        unreachable!()
23    };
24}
25
26#[doc(hidden)]
27#[cfg(feature = "sled")]
28#[macro_export]
29macro_rules! cfg_if_sled {
30    (@pat $i: pat) => {
31        $i
32    };
33    ($($body:tt)*) => {
34        $($body)*
35    };
36}
37
38#[doc(hidden)]
39#[cfg(not(feature = "sled"))]
40#[macro_export]
41macro_rules! cfg_if_sled {
42    (@pat $i: pat) => {
43        _
44    };
45    ($($body:tt)*) => {
46        unreachable!()
47    };
48}
49// endregion
50
51/// Runs `$body` with `$type_ident` bound to a concrete state backend type based on the runtime
52/// value of `$type_value` (which has to be of type [BackendType](crate::BackendType)). The caller
53/// has to make sure that the return type of this expression is the same regardless of what
54/// `$type_ident` happens to be bound to.
55///
56/// # Examples
57///
58/// ```no_run
59/// # extern crate arcon_state;
60/// # use arcon_state::backend::{BackendType, Backend};
61/// # use arcon_state::with_backend_type;
62/// # use std::any::Any;
63/// let runtime_type = BackendType::Sled;
64/// let boxed: Box<dyn Any> = with_backend_type!(runtime_type,
65///     |SB| Box::new(SB::create("test_dir".as_ref(), "testDB".to_string()).unwrap()) as Box<dyn Any>
66/// );
67/// ```
68#[macro_export]
69macro_rules! with_backend_type {
70    ($type_value:expr, |$type_ident:ident| $body:expr) => {{
71        use $crate::backend::BackendType::*;
72        #[allow(unreachable_patterns)]
73        match $type_value {
74            $crate::cfg_if_sled!(@pat Sled) => {
75                $crate::cfg_if_sled! {
76                    type $type_ident = $crate::backend::sled::Sled;
77                    $body
78                }
79            }
80            $crate::cfg_if_rocks!(@pat Rocks) => {
81                $crate::cfg_if_rocks! {
82                    type $type_ident = $crate::backend::rocks::Rocks;
83                    $body
84                }
85            }
86        }
87    }};
88}