latexml_core/
aux_macros.rs1#[macro_export]
5macro_rules! static_map {
6 ($( $key:expr_2021 => $val:expr_2021 ),*) => {{
7 let mut map : HashMap<&'static str, &'static str> = HashMap::default();
8 $( map.insert($key, $val); )*
9 map
10 }}
11}
12
13#[macro_export]
15macro_rules! map {
16 ($( $key:expr_2021 => $val:expr_2021 ),*) => {{
17 let mut map = HashMap::default();
18 $( map.insert($key.to_string(), $val); )*
19 map
20 }}
21}
22
23#[macro_export]
25macro_rules! stored_map {
26 ($( $key:expr_2021 => $val:expr_2021 ),*) => {{
27 let mut map : $crate::common::arena::SymHashMap<$crate::common::store::Stored> =
28 $crate::common::arena::SymHashMap::default();
29 $( map.insert($key, $val.into()); )*
30 map
31 }}
32}
33
34#[macro_export]
36macro_rules! sym_map {
37 ($( $key:expr_2021 => $val:expr_2021 ),*) => {{
38 let mut map = $crate::common::arena::SymHashMap::default();
39 $( map.insert($key, $val); )*
40 map
41 }}
42}
43
44#[macro_export]
46macro_rules! string_keys_map {
47 ($( $key:expr_2021 => $val:expr_2021 ),*) => {{
48 let mut map = HashMap::default();
49 $( map.insert($key.to_string(), $val.into()); )*
50 map
51 }}
52}
53
54#[macro_export]
56macro_rules! string_map {
57 ($( $key:expr_2021 => $val:expr_2021 ),*) => {{
58 let mut map = HashMap::default();
59 $( map.insert($key.to_string(), $val.to_string()); )*
60 map
61 }}
62}
63
64#[macro_export]
67macro_rules! raw_map {
68 ($( $key:expr_2021 => $val:expr_2021 ),* $(,)?) => {{
69 #[allow(unused_mut)]
70 let mut map = HashMap::default();
71 $( map.insert($key, $val); )*
72 map
73 }}
74}
75
76#[macro_export]
78macro_rules! raw_char_map {
79 ($( $key:literal => $val:expr_2021 ),*) => {{
80 let mut map : HashMap<char,_> = HashMap::default();
81 $( map.insert($key, $val); )*
82 map
83 }}
84}
85
86#[macro_export]
88macro_rules! s {
89 ($($arg : tt )*) => {format!($($arg)*)};
90}
91
92#[macro_export]
94macro_rules! some {
95 ($arg:expr_2021) => {
96 Some($arg.into())
97 };
98}
99
100#[macro_export]
109macro_rules! mixvec {
110 ($( $val:expr_2021 ),*) => {{
111 vec![ $($val.into()),*]
112 }}
113}
114
115#[macro_export]
124macro_rules! mixrc {
125 ($( $val:expr_2021 ),*) => {{
126 Rc::new([ $($val.into()),*])
127 }}
128}
129
130#[macro_export]
133macro_rules! fontmap {
134 ($($key:ident => $value:expr_2021),*) => (
135 Font { $($key: Some($value.into()),)* .. Font::default() })
136}
137
138#[macro_export]
141macro_rules! set {
142 ( $( $x:expr_2021 ),* ) => { {
144 use rustc_hash::FxHashSet as HashSet;
146 let mut temp_set = HashSet::default(); $(
148 temp_set.insert($x); )*
150 temp_set }
152 };
153}
154
155macro_rules! thread_root {
160 ($(#[$meta:meta])* $vis:vis fn $name:ident() -> $ty:ty = $init:expr_2021;) => {
161 $(#[$meta])*
162 #[inline(always)]
163 $vis fn $name() -> &'static $ty {
164 ::std::thread_local! {
165 static SLOT: ::std::cell::Cell<Option<&'static $ty>> =
166 const { ::std::cell::Cell::new(None) };
167 }
168 #[cold]
169 #[inline(never)]
170 fn init() -> &'static $ty {
171 let root: &'static $ty = Box::leak(Box::new($init));
172 debug_assert!(SLOT.get().is_none(), "a thread root's initializer re-entered its own root");
174 SLOT.set(Some(root));
175 root
176 }
177 match SLOT.get() {
178 Some(root) => root,
179 None => init(),
180 }
181 }
182 };
183}
184
185pub struct ThreadRoot<T: 'static>(pub fn() -> &'static T);
188
189impl<T: 'static> std::ops::Deref for ThreadRoot<T> {
190 type Target = T;
191 #[inline(always)]
192 fn deref(&self) -> &T { (self.0)() }
193}
194
195macro_rules! thread_static {
198 ($(#[$meta:meta])* $vis:vis static $name:ident: $ty:ty = $init:expr_2021;) => {
199 $(#[$meta])*
200 $vis static $name: $crate::aux_macros::ThreadRoot<$ty> = $crate::aux_macros::ThreadRoot({
201 #[inline(always)]
202 fn root() -> &'static $ty {
203 ::std::thread_local! {
204 static SLOT: ::std::cell::Cell<Option<&'static $ty>> =
205 const { ::std::cell::Cell::new(None) };
206 }
207 #[cold]
208 #[inline(never)]
209 fn init() -> &'static $ty {
210 let root: &'static $ty = Box::leak(Box::new($init));
211 debug_assert!(SLOT.get().is_none(), "a thread root's initializer re-entered its own root");
213 SLOT.set(Some(root));
214 root
215 }
216 match SLOT.get() {
217 Some(root) => root,
218 None => init(),
219 }
220 }
221 root
222 });
223 };
224}