jay_config/
macros.rs

1/// Declares the entry point of the configuration.
2#[macro_export]
3macro_rules! config {
4    ($f:path) => {
5        #[unsafe(no_mangle)]
6        #[used]
7        pub static mut JAY_CONFIG_ENTRY_V1: $crate::_private::ConfigEntry = {
8            struct X;
9            impl $crate::_private::Config for X {
10                extern "C" fn configure() {
11                    $f();
12                }
13            }
14            $crate::_private::ConfigEntryGen::<X>::ENTRY
15        };
16    };
17}
18
19macro_rules! try_get {
20    () => {{
21        unsafe {
22            let client = crate::_private::client::CLIENT.with(|client| client.get());
23            if client.is_null() {
24                None
25            } else {
26                Some(&*client)
27            }
28        }
29    }};
30}
31
32macro_rules! get {
33    () => {{ get!(Default::default()) }};
34    ($def:expr) => {{
35        let client = unsafe {
36            let client = crate::_private::client::CLIENT.with(|client| client.get());
37            if client.is_null() {
38                return $def;
39            }
40            &*client
41        };
42        client
43    }};
44}
45
46macro_rules! bitflags {
47    (
48        $(#[$attr1:meta])*
49        $vis1:vis struct $name:ident($vis2:vis $rep:ty) {
50            $(
51                $(#[$attr2:meta])*
52                $vis3:vis const $var:ident = $val:expr,
53            )*
54        }
55    ) => {
56        $(#[$attr1])*
57        $vis1 struct $name($vis2 $rep);
58
59        $(
60            $(#[$attr2])*
61            $vis3 const $var: $name = $name($val);
62        )*
63
64        impl std::ops::BitOr for $name {
65            type Output = Self;
66
67            fn bitor(self, rhs: Self) -> Self::Output {
68                Self(self.0 | rhs.0)
69            }
70        }
71
72        impl std::ops::BitAnd for $name {
73            type Output = Self;
74
75            fn bitand(self, rhs: Self) -> Self::Output {
76                Self(self.0 & rhs.0)
77            }
78        }
79
80        impl std::ops::BitOrAssign for $name {
81            fn bitor_assign(&mut self, rhs: Self) {
82                self.0 |= rhs.0;
83            }
84        }
85
86        impl std::ops::BitAndAssign for $name {
87            fn bitand_assign(&mut self, rhs: Self) {
88                self.0 &= rhs.0;
89            }
90        }
91
92        impl std::ops::BitXorAssign for $name {
93            fn bitxor_assign(&mut self, rhs: Self) {
94                self.0 ^= rhs.0;
95            }
96        }
97
98        impl std::ops::Not for $name {
99            type Output = Self;
100
101            fn not(self) -> Self::Output {
102                Self(!self.0)
103            }
104        }
105
106        impl std::fmt::Debug for $name {
107            #[allow(clippy::allow_attributes, clippy::bad_bit_mask, unused_mut)]
108            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109                let mut any = false;
110                let mut v = self.0;
111                $(
112                    if $val != 0 && v & $val == $val {
113                        if any {
114                            write!(f, "|")?;
115                        }
116                        any = true;
117                        write!(f, "{}", stringify!($var))?;
118                        v &= !$val;
119                    }
120                )*
121                if !any || v != 0 {
122                    if any {
123                        write!(f, "|")?;
124                    }
125                    write!(f, "0x{:x}", v)?;
126                }
127                Ok(())
128            }
129        }
130    }
131}