konst_macro_rules/iter/
iter_eval_macro.rs

1#[macro_export]
2macro_rules! iter_eval {
3    () => {
4        $crate::__::compile_error!{ "`eval` expects an iterator argument" }
5    };
6    ($iter:expr $(, $($rem:tt)*)?) => {
7        $crate::__process_iter_args!{
8            ($crate::__iter_eval)
9            ()
10            (item, 'zxe7hgbnjs, consumer,)
11            $iter, $($($rem)*)?
12        }
13    };
14}
15
16#[doc(hidden)]
17#[macro_export]
18macro_rules! __iter_eval {
19    ($fixed:tt () $item:ident for_each($($closure:tt)*), $(,)* ) => {
20        $crate::utils::__parse_closure_1!{
21            ($crate::__ie_for_each)
22            ($fixed $item,)
23            (for_each),
24            $($closure)*
25        }
26    };
27    ($fixed:tt $vars:tt $item:ident any($($closure:tt)*), $(,)* ) => {
28        $crate::utils::__parse_closure_1!{
29            ($crate::__ie_any)
30            ($fixed $vars $item,)
31            (any),
32            $($closure)*
33        }
34    };
35    ($fixed:tt $vars:tt $item:ident all($($closure:tt)*), $(,)* ) => {
36        $crate::utils::__parse_closure_1!{
37            ($crate::__ie_all)
38            ($fixed $vars $item,)
39            (all),
40            $($closure)*
41        }
42    };
43    ($fixed:tt ($var:ident) $item:ident count($($args:tt)*), $(,)* ) => ({
44        $crate::__cim_error_on_args!{count ($($args)*)}
45
46        $crate::__ie_output!{
47            $fixed
48            { $var += 1; }
49        }
50    });
51    // there's guaranteed to be an identifier for the method name,
52    // so it is required to be either position or rposition.
53    //
54    // `rposition` reverses the iterator in `__cim_preprocess_methods`
55    (
56        $fixed:tt
57        $vars:tt
58        $item:ident
59        $(position $(@$position:tt)?)? $(rposition $(@$rposition:tt)?)? ($($closure:tt)*), $(,)*
60    ) => {
61        $crate::utils::__parse_closure_1!{
62            ($crate::__ie_position)
63            ($fixed $vars $item,)
64            ($(position $(@$position)?)? $(rposition $(@$rposition)?)?),
65            $($closure)*
66        }
67    };
68    ($fixed:tt $vars:tt $item:ident find_map ($($closure:tt)*), $(,)* ) => {
69        $crate::utils::__parse_closure_1!{
70            ($crate::__ie_find_map)
71            ($fixed $vars $item,)
72            (find_map),
73            $($closure)*
74        }
75    };
76    (
77        $fixed:tt
78        $vars:tt
79        $item:ident
80        $(find $(@$find:tt)?)? $(rfind $(@$rfind:tt)?)? ($($closure:tt)*),
81        $(,)*
82    ) => {
83        $crate::utils::__parse_closure_1!{
84            ($crate::__ie_find)
85            ($fixed $vars $item,)
86            ($(find $(@$find)?)? $(rfind $(@$rfind)?)?),
87            $($closure)*
88        }
89    };
90    ($fixed:tt $vars:tt $item:ident fold (), $(,)* ) => {
91        $crate::__::compile_error! {"fold method expects accumulator and closure arguments"}
92    };
93    ($fixed:tt $vars:tt $item:ident rfold (), $(,)* ) => {
94        $crate::__::compile_error! {"rfold method expects accumulator and closure arguments"}
95    };
96    (
97        $fixed:tt
98        $vars:tt
99        $item:ident
100        $(fold $(@$fold:tt)?)? $(rfold $(@$rfold:tt)?)? ($accum:expr $(, $($closure:tt)*)?), $(,)*
101    ) => {
102        $crate::utils::__parse_closure_2!{
103            ($crate::__ie_fold)
104            ($fixed $vars $item,)
105            ($(fold $(@$fold)?)? $(rfold $(@$rfold)?)?),
106            $($($closure)*)?
107        }
108    };
109    ($fixed:tt $vars:tt $item:ident fold $args:tt, $(,)* ) => {
110        $crate::__::compile_error! {"fold method expects accumulator and closure arguments"}
111    };
112    ($fixed:tt $vars:tt $item:ident rfold $args:tt, $(,)* ) => {
113        $crate::__::compile_error! {"rfold method expects accumulator and closure arguments"}
114    };
115    ($fixed:tt ($var:ident) $item:ident next($($args:tt)*), $(,)* ) => ({
116        $crate::__cim_error_on_args!{next ($($args)*)}
117        $crate::__ie_output!{
118            $fixed
119            {
120                $var = $crate::__::Some($item);
121                $crate::__ie_break!{$fixed}
122            }
123        }
124    });
125    ($fixed:tt ($nth:ident $ret:ident) $item:ident nth($($args:tt)*), $(,)* ) => ({
126        $crate::__ie_output!{
127            $fixed
128            {
129                let _: $crate::__::usize = $nth;
130                if $nth == 0 {
131                    $ret = $crate::__::Some($item);
132                    $crate::__ie_break!{$fixed}
133                } else {
134                    $nth -= 1;
135                }
136            }
137        }
138    });
139    ($fixed:tt () $item:ident $comb:ident $($rem:tt)*) => {
140        $crate::__::compile_error! {$crate::__::concat!(
141            "Unsupported iterator method: `",
142            $crate::__::stringify!($comb),
143            "`",
144        )}
145    };
146    ($fixed:tt () $item:ident $(,)*) => {
147        $crate::__ie_output!{$fixed {}}
148    };
149    ($fixed:tt () $item:ident $($rem:tt)*) => {
150        $crate::__::compile_error! {$crate::__::concat!(
151            "Unsupported trailing syntax: `",
152            $crate::__::stringify!($($rem)*),
153            "`",
154        )}
155    };
156}
157
158#[doc(hidden)]
159#[macro_export]
160macro_rules! __ie_for_each {
161    ($fixed:tt $item:ident, |$elem:pat| $value:expr) => {
162        $crate::__ie_output! {
163            $fixed
164            {let $elem = $item; $value;}
165        }
166    };
167}
168
169#[doc(hidden)]
170#[macro_export]
171macro_rules! __ie_any {
172    ($fixed:tt ($cond:ident) $item:ident, |$elem:pat| $v:expr) => {
173        $crate::__ie_output! {
174            $fixed
175            {
176                let $elem = $item;
177                let cond: $crate::__::bool = $v;
178                if cond {
179                    $cond = true;
180                    $crate::__ie_break!{$fixed}
181                }
182            }
183        }
184    };
185}
186
187#[doc(hidden)]
188#[macro_export]
189macro_rules! __ie_all {
190    ($fixed:tt ($cond:ident) $item:ident, |$elem:pat| $v:expr) => {
191        $crate::__ie_output! {
192            $fixed
193            {
194                let $elem = $item;
195                let cond: $crate::__::bool = $v;
196                if !cond {
197                    $cond = false;
198                    $crate::__ie_break!{$fixed}
199                }
200            }
201        }
202    };
203}
204
205#[doc(hidden)]
206#[macro_export]
207macro_rules! __ie_position {
208    ($fixed:tt ($i:ident $position:ident) $item:ident, |$elem:pat| $v:expr) => {
209        $crate::__ie_output! {
210            $fixed
211            {
212                let $elem = $item;
213                let cond: $crate::__::bool = $v;
214                if cond {
215                    $position = $crate::__::Some($i);
216                    $crate::__ie_break!{$fixed}
217                } else {
218                    $i += 1;
219                }
220            }
221        }
222    };
223}
224
225#[doc(hidden)]
226#[macro_export]
227macro_rules! __ie_find_map {
228    ($fixed:tt ($ret:ident) $item:ident, |$elem:pat| $v:expr) => {
229        $crate::__ie_output! {
230            $fixed
231            {
232                let $elem = $item;
233                $ret = $v;
234                if let $crate::__::Some(_) = $ret {
235                    $crate::__ie_break!{$fixed}
236                }
237            }
238        }
239    };
240}
241
242#[doc(hidden)]
243#[macro_export]
244macro_rules! __ie_find {
245    ($fixed:tt ($ret:ident) $item:ident, |$elem:pat| $v:expr) => {
246        $crate::__ie_output! {
247            $fixed
248            {
249                let $elem = &$item;
250                let cond: $crate::__::bool = $v;
251                if cond {
252                    $ret = $crate::__::Some($item);
253                    $crate::__ie_break!{$fixed}
254                }
255            }
256        }
257    };
258}
259
260#[doc(hidden)]
261#[macro_export]
262macro_rules! __ie_fold {
263    ($fixed:tt ($accum:ident) $item:ident, |$accum_pat:pat, $elem:pat| $v:expr) => {
264        $crate::__ie_output! {
265            $fixed
266            {
267                let $accum_pat = $accum;
268                let $elem = $item;
269                $accum = $v;
270            }
271        }
272    };
273}
274
275#[doc(hidden)]
276#[macro_export]
277macro_rules! __ie_output {
278    (
279        (
280            ($break_label:tt $($label:tt)?)
281            $item:ident
282            (
283                $((
284                    {$($var_a:ident = $var_a_expr:expr),* $(,)?}
285                    $($code:tt)*
286                ))*
287            )
288        )
289        $each:tt
290    ) => ({
291        match ($($( $var_a_expr,)?)*) {
292            ($($(mut $var_a,)?)*) => {
293                $($label:)? loop {
294                    $($($code)*)*
295                    $each
296                }
297            },
298        }
299    });
300}
301
302#[doc(hidden)]
303#[macro_export]
304macro_rules! __ie_break {
305    (
306        (
307            ($break_label:tt $($label:tt)?)
308            $item:ident
309            $iter:tt
310        )
311        $($break_with:expr)?
312    ) => {
313        break $break_label $($break_with)?;
314    };
315}