monoio/macros/
select.rs

1/// Wait on multiple concurrent branches, returning when the **first** branch
2/// completes, cancelling the remaining branches.
3///
4/// The `select!` macro must be used inside of async functions, closures, and
5/// blocks.
6///
7/// The `select!` macro accepts one or more branches with the following pattern:
8///
9/// ```text
10/// <pattern> = <async expression> (, if <precondition>)? => <handler>,
11/// ```
12///
13/// Additionally, the `select!` macro may include a single, optional `else`
14/// branch, which evaluates if none of the other branches match their patterns:
15///
16/// ```text
17/// else => <expression>
18/// ```
19///
20/// The macro aggregates all `<async expression>` expressions and runs them
21/// concurrently on the **current** task. Once the **first** expression
22/// completes with a value that matches its `<pattern>`, the `select!` macro
23/// returns the result of evaluating the completed branch's `<handler>`
24/// expression.
25///
26/// Additionally, each branch may include an optional `if` precondition. If the
27/// precondition returns `false`, then the branch is disabled. The provided
28/// `<async expression>` is still evaluated but the resulting future is never
29/// polled. This capability is useful when using `select!` within a loop.
30///
31/// The complete lifecycle of a `select!` expression is as follows:
32///
33/// 1. Evaluate all provided `<precondition>` expressions. If the precondition returns `false`,
34///    disable the branch for the remainder of the current call to `select!`. Re-entering `select!`
35///    due to a loop clears the "disabled" state.
36/// 2. Aggregate the `<async expression>`s from each branch, including the disabled ones. If the
37///    branch is disabled, `<async expression>` is still evaluated, but the resulting future is not
38///    polled.
39/// 3. Concurrently await on the results for all remaining `<async expression>`s. 4. Once an `<async
40///    expression>` returns a value, attempt to apply the value    to the provided `<pattern>`, if
41///    the pattern matches, evaluate `<handler>`    and return. If the pattern **does not** match,
42///    disable the current branch    and for the remainder of the current call to `select!`.
43///    Continue from step 3. 5. If **all** branches are disabled, evaluate the `else` expression. If
44///    no    else branch is provided, panic.
45///
46/// # Runtime characteristics
47///
48/// By running all async expressions on the current task, the expressions are
49/// able to run **concurrently** but not in **parallel**. This means all
50/// expressions are run on the same thread and if one branch blocks the thread,
51/// all other expressions will be unable to continue. If parallelism is
52/// required, spawn each async expression using [`monoio::spawn`] and pass the
53/// join handle to `select!`.
54///
55/// [`monoio::spawn`]: crate::spawn
56///
57/// # Fairness
58///
59/// By default, `select!` randomly picks a branch to check first. This provides
60/// some level of fairness when calling `select!` in a loop with branches that
61/// are always ready.
62///
63/// This behavior can be overridden by adding `biased;` to the beginning of the
64/// macro usage. See the examples for details. This will cause `select` to poll
65/// the futures in the order they appear from top to bottom. There are a few
66/// reasons you may want this:
67///
68/// - The random number generation of `monoio::select!` has a non-zero CPU cost
69/// - Your futures may interact in a way where known polling order is significant
70///
71/// But there is an important caveat to this mode. It becomes your
72/// responsibility to ensure that the polling order of your futures is fair. If
73/// for example you are selecting between a stream and a shutdown future, and
74/// the stream has a huge volume of messages and zero or nearly zero time
75/// between them, you should place the shutdown future earlier in the `select!`
76/// list to ensure that it is always polled, and will not be ignored due to the
77/// stream being constantly ready.
78///
79/// # Panics
80///
81/// The `select!` macro panics if all branches are disabled **and** there is no
82/// provided `else` branch. A branch is disabled when the provided `if`
83/// precondition returns `false` **or** when the pattern does not match the
84/// result of `<async expression>`.
85///
86/// # Examples
87///
88/// Basic select with two branches.
89///
90/// ```
91/// async fn do_stuff_async() {
92///     // async work
93/// }
94///
95/// async fn more_async_work() {
96///     // more here
97/// }
98///
99/// #[monoio::main]
100/// async fn main() {
101///     monoio::select! {
102///         _ = do_stuff_async() => {
103///             println!("do_stuff_async() completed first")
104///         }
105///         _ = more_async_work() => {
106///             println!("more_async_work() completed first")
107///         }
108///     };
109/// }
110/// ```
111///
112/// Collect the contents of two streams. In this example, we rely on pattern
113/// matching and the fact that `stream::iter` is "fused", i.e. once the stream
114/// is complete, all calls to `next()` return `None`.
115///
116/// Using the same future in multiple `select!` expressions can be done by
117/// passing a reference to the future. Doing so requires the future to be
118/// [`Unpin`]. A future can be made [`Unpin`] by either using [`Box::pin`] or
119/// stack pinning.
120///
121/// [`Unpin`]: std::marker::Unpin
122/// [`Box::pin`]: std::boxed::Box::pin
123///
124/// Using the `biased;` mode to control polling order.
125///
126/// ```
127/// #[monoio::main]
128/// async fn main() {
129///     let mut count = 0u8;
130///
131///     loop {
132///         monoio::select! {
133///             // If you run this example without `biased;`, the polling order is
134///             // pseudo-random, and the assertions on the value of count will
135///             // (probably) fail.
136///             biased;
137///
138///             _ = async {}, if count < 1 => {
139///                 count += 1;
140///                 assert_eq!(count, 1);
141///             }
142///             _ = async {}, if count < 2 => {
143///                 count += 1;
144///                 assert_eq!(count, 2);
145///             }
146///             _ = async {}, if count < 3 => {
147///                 count += 1;
148///                 assert_eq!(count, 3);
149///             }
150///             _ = async {}, if count < 4 => {
151///                 count += 1;
152///                 assert_eq!(count, 4);
153///             }
154///
155///             else => {
156///                 break;
157///             }
158///         };
159///     }
160/// }
161/// ```
162///
163/// ## Avoid racy `if` preconditions
164///
165/// Given that `if` preconditions are used to disable `select!` branches, some
166/// caution must be used to avoid missing values.
167///
168/// For example, here is **incorrect** usage of `sleep` with `if`. The objective
169/// is to repeatedly run an asynchronous task for up to 50 milliseconds.
170/// However, there is a potential for the `sleep` completion to be missed.
171///
172/// ```no_run,should_panic
173/// use monoio::time::{self, Duration};
174///
175/// async fn some_async_work() {
176///     // do work
177/// }
178///
179/// #[monoio::main(timer_enabled = true)]
180/// async fn main() {
181///     let mut sleep = std::pin::pin!(time::sleep(Duration::from_millis(50)));
182///
183///     while !sleep.is_elapsed() {
184///         monoio::select! {
185///             _ = &mut sleep, if !sleep.is_elapsed() => {
186///                 println!("operation timed out");
187///             }
188///             _ = some_async_work() => {
189///                 println!("operation completed");
190///             }
191///         }
192///     }
193///
194///     panic!("This example shows how not to do it!");
195/// }
196/// ```
197///
198/// In the above example, `sleep.is_elapsed()` may return `true` even if
199/// `sleep.poll()` never returned `Ready`. This opens up a potential race
200/// condition where `sleep` expires between the `while !sleep.is_elapsed()`
201/// check and the call to `select!` resulting in the `some_async_work()` call to
202/// run uninterrupted despite the sleep having elapsed.
203///
204/// One way to write the above example without the race would be:
205///
206/// ```
207/// use monoio::time::{self, Duration};
208///
209/// async fn some_async_work() {
210/// # time::sleep(Duration::from_millis(10)).await;
211///     // do work
212/// }
213///
214/// #[monoio::main(timer_enabled = true)]
215/// async fn main() {
216///     let mut sleep = std::pin::pin!(time::sleep(Duration::from_millis(50)));
217///
218///     loop {
219///         monoio::select! {
220///             _ = &mut sleep => {
221///                 println!("operation timed out");
222///                 break;
223///             }
224///             _ = some_async_work() => {
225///                 println!("operation completed");
226///             }
227///         }
228///     }
229/// }
230/// ```
231#[macro_export]
232#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
233macro_rules! select {
234    // Uses a declarative macro to do **most** of the work. While it is possible
235    // to implement fully with a declarative macro, a procedural macro is used
236    // to enable improved error messages.
237    //
238    // The macro is structured as a tt-muncher. All branches are processed and
239    // normalized. Once the input is normalized, it is passed to the top-most
240    // rule. When entering the macro, `@{ }` is inserted at the front. This is
241    // used to collect the normalized input.
242    //
243    // The macro only recurses once per branch. This allows using `select!`
244    // without requiring the user to increase the recursion limit.
245
246    // All input is normalized, now transform.
247    (@ {
248        // The index of the future to poll first (in bias mode), or the RNG
249        // expression to use to pick a future to poll first.
250        start=$start:expr;
251
252        // One `_` for each branch in the `select!` macro. Passing this to
253        // `count!` converts $skip to an integer.
254        ( $($count:tt)* )
255
256        // Normalized select branches. `( $skip )` is a set of `_` characters.
257        // There is one `_` for each select branch **before** this one. Given
258        // that all input futures are stored in a tuple, $skip is useful for
259        // generating a pattern to reference the future for the current branch.
260        // $skip is also used as an argument to `count!`, returning the index of
261        // the current select branch.
262        $( ( $($skip:tt)* ) $bind:pat = $fut:expr, if $c:expr => $handle:expr, )+
263
264        // Fallback expression used when all select branches have been disabled.
265        ; $else:expr
266
267    }) => {{
268        // Enter a context where stable "function-like" proc macros can be used.
269        //
270        // This module is defined within a scope and should not leak out of this
271        // macro.
272        mod util {
273            // Generate an enum with one variant per select branch
274            $crate::select_priv_declare_output_enum!( ( $($count)* ) );
275        }
276
277        // `monoio::macros::support` is a public, but doc(hidden) module
278        // including a re-export of all types needed by this macro.
279        use $crate::macros::support::Future;
280        use $crate::macros::support::Pin;
281        use $crate::macros::support::Poll::{Ready, Pending};
282
283        const BRANCHES: u32 = $crate::count!( $($count)* );
284
285        let mut disabled: util::Mask = Default::default();
286
287        // First, invoke all the pre-conditions. For any that return true,
288        // set the appropriate bit in `disabled`.
289        $(
290            if !$c {
291                let mask: util::Mask = 1 << $crate::count!( $($skip)* );
292                disabled |= mask;
293            }
294        )*
295
296        // Create a scope to separate polling from handling the output. This
297        // adds borrow checker flexibility when using the macro.
298        let mut output = {
299            // Safety: Nothing must be moved out of `futures`. This is to
300            // satisfy the requirement of `Pin::new_unchecked` called below.
301            let mut futures = ( $( $fut , )+ );
302
303            $crate::macros::support::poll_fn(|cx| {
304                // Track if any branch returns pending. If no branch completes
305                // **or** returns pending, this implies that all branches are
306                // disabled.
307                let mut is_pending = false;
308
309                // Choose a starting index to begin polling the futures at. In
310                // practice, this will either be a pseudo-randomly generated
311                // number by default, or the constant 0 if `biased;` is
312                // supplied.
313                let start = $start;
314
315                for i in 0..BRANCHES {
316                    let branch;
317                    #[allow(clippy::modulo_one)]
318                    {
319                        branch = (start + i) % BRANCHES;
320                    }
321                    match branch {
322                        $(
323                            #[allow(unreachable_code)]
324                            $crate::count!( $($skip)* ) => {
325                                // First, if the future has previously been
326                                // disabled, do not poll it again. This is done
327                                // by checking the associated bit in the
328                                // `disabled` bit field.
329                                let mask = 1 << branch;
330
331                                if disabled & mask == mask {
332                                    // The future has been disabled.
333                                    continue;
334                                }
335
336                                // Extract the future for this branch from the
337                                // tuple
338                                let ( $($skip,)* fut, .. ) = &mut futures;
339
340                                // Safety: future is stored on the stack above
341                                // and never moved.
342                                let mut fut = unsafe { Pin::new_unchecked(fut) };
343
344                                // Try polling it
345                                let out = match fut.poll(cx) {
346                                    Ready(out) => out,
347                                    Pending => {
348                                        // Track that at least one future is
349                                        // still pending and continue polling.
350                                        is_pending = true;
351                                        continue;
352                                    }
353                                };
354
355                                // Disable the future from future polling.
356                                disabled |= mask;
357
358                                // The future returned a value, check if matches
359                                // the specified pattern.
360                                #[allow(unused_variables)]
361                                #[allow(unused_mut)]
362                                match &out {
363                                    $bind => {}
364                                    _ => continue,
365                                }
366
367                                // The select is complete, return the value
368                                return Ready($crate::select_variant!(util::Out, ($($skip)*))(out));
369                            }
370                        )*
371                        _ => unreachable!("reaching this means there probably is an off by one bug"),
372                    }
373                }
374
375                if is_pending {
376                    Pending
377                } else {
378                    // All branches have been disabled.
379                    Ready(util::Out::Disabled)
380                }
381            }).await
382        };
383
384        match output {
385            $(
386                $crate::select_variant!(util::Out, ($($skip)*) ($bind)) => $handle,
387            )*
388            util::Out::Disabled => $else,
389            _ => unreachable!("failed to match bind"),
390        }
391    }};
392
393    // ==== Normalize =====
394
395    // These rules match a single `select!` branch and normalize it for
396    // processing by the first rule.
397
398    (@ { start=$start:expr; $($t:tt)* } ) => {
399        // No `else` branch
400        $crate::select!(@{ start=$start; $($t)*; panic!("all branches are disabled and there is no else branch") })
401    };
402    (@ { start=$start:expr; $($t:tt)* } else => $else:expr $(,)?) => {
403        $crate::select!(@{ start=$start; $($t)*; $else })
404    };
405    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr, if $c:expr => $h:block, $($r:tt)* ) => {
406        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if $c => $h, } $($r)*)
407    };
408    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr => $h:block, $($r:tt)* ) => {
409        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if true => $h, } $($r)*)
410    };
411    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr, if $c:expr => $h:block $($r:tt)* ) => {
412        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if $c => $h, } $($r)*)
413    };
414    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr => $h:block $($r:tt)* ) => {
415        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if true => $h, } $($r)*)
416    };
417    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr, if $c:expr => $h:expr ) => {
418        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if $c => $h, })
419    };
420    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr => $h:expr ) => {
421        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if true => $h, })
422    };
423    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr, if $c:expr => $h:expr, $($r:tt)* ) => {
424        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if $c => $h, } $($r)*)
425    };
426    (@ { start=$start:expr; ( $($s:tt)* ) $($t:tt)* } $p:pat = $f:expr => $h:expr, $($r:tt)* ) => {
427        $crate::select!(@{ start=$start; ($($s)* _) $($t)* ($($s)*) $p = $f, if true => $h, } $($r)*)
428    };
429
430    // ===== Entry point =====
431
432    (biased; $p:pat = $($t:tt)* ) => {
433        $crate::select!(@{ start=0; () } $p = $($t)*)
434    };
435
436    ( $p:pat = $($t:tt)* ) => {
437        // Randomly generate a starting point. This makes `select!` a bit more
438        // fair and avoids always polling the first future.
439        $crate::select!(@{ start={ $crate::macros::support::thread_rng_n(BRANCHES) }; () } $p = $($t)*)
440    };
441    () => {
442        compile_error!("select! requires at least one branch.")
443    };
444}
445
446// And here... we manually list out matches for up to 64 branches... I'm not
447// happy about it either, but this is how we manage to use a declarative macro!
448
449#[macro_export]
450#[doc(hidden)]
451macro_rules! count {
452    () => {
453        0
454    };
455    (_) => {
456        1
457    };
458    (_ _) => {
459        2
460    };
461    (_ _ _) => {
462        3
463    };
464    (_ _ _ _) => {
465        4
466    };
467    (_ _ _ _ _) => {
468        5
469    };
470    (_ _ _ _ _ _) => {
471        6
472    };
473    (_ _ _ _ _ _ _) => {
474        7
475    };
476    (_ _ _ _ _ _ _ _) => {
477        8
478    };
479    (_ _ _ _ _ _ _ _ _) => {
480        9
481    };
482    (_ _ _ _ _ _ _ _ _ _) => {
483        10
484    };
485    (_ _ _ _ _ _ _ _ _ _ _) => {
486        11
487    };
488    (_ _ _ _ _ _ _ _ _ _ _ _) => {
489        12
490    };
491    (_ _ _ _ _ _ _ _ _ _ _ _ _) => {
492        13
493    };
494    (_ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
495        14
496    };
497    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
498        15
499    };
500    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
501        16
502    };
503    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
504        17
505    };
506    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
507        18
508    };
509    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
510        19
511    };
512    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
513        20
514    };
515    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
516        21
517    };
518    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
519        22
520    };
521    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
522        23
523    };
524    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
525        24
526    };
527    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
528        25
529    };
530    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
531        26
532    };
533    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
534        27
535    };
536    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
537        28
538    };
539    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
540        29
541    };
542    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
543        30
544    };
545    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
546        31
547    };
548    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
549        32
550    };
551    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
552        33
553    };
554    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
555        34
556    };
557    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
558        35
559    };
560    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
561        36
562    };
563    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
564        37
565    };
566    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
567        38
568    };
569    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
570        39
571    };
572    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
573        40
574    };
575    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
576        41
577    };
578    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
579        42
580    };
581    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
582        43
583    };
584    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
585        44
586    };
587    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
588        45
589    };
590    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
591        46
592    };
593    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
594        47
595    };
596    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
597        48
598    };
599    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
600        49
601    };
602    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
603        50
604    };
605    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
606        51
607    };
608    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
609        52
610    };
611    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
612        53
613    };
614    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
615        54
616    };
617    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
618        55
619    };
620    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
621        56
622    };
623    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
624        57
625    };
626    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
627        58
628    };
629    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
630        59
631    };
632    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
633        60
634    };
635    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
636        61
637    };
638    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
639        62
640    };
641    (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) => {
642        63
643    };
644}
645
646#[macro_export]
647#[doc(hidden)]
648macro_rules! select_variant {
649    ($($p:ident)::*, () $($t:tt)*) => {
650        $($p)::*::_0 $($t)*
651    };
652    ($($p:ident)::*, (_) $($t:tt)*) => {
653        $($p)::*::_1 $($t)*
654    };
655    ($($p:ident)::*, (_ _) $($t:tt)*) => {
656        $($p)::*::_2 $($t)*
657    };
658    ($($p:ident)::*, (_ _ _) $($t:tt)*) => {
659        $($p)::*::_3 $($t)*
660    };
661    ($($p:ident)::*, (_ _ _ _) $($t:tt)*) => {
662        $($p)::*::_4 $($t)*
663    };
664    ($($p:ident)::*, (_ _ _ _ _) $($t:tt)*) => {
665        $($p)::*::_5 $($t)*
666    };
667    ($($p:ident)::*, (_ _ _ _ _ _) $($t:tt)*) => {
668        $($p)::*::_6 $($t)*
669    };
670    ($($p:ident)::*, (_ _ _ _ _ _ _) $($t:tt)*) => {
671        $($p)::*::_7 $($t)*
672    };
673    ($($p:ident)::*, (_ _ _ _ _ _ _ _) $($t:tt)*) => {
674        $($p)::*::_8 $($t)*
675    };
676    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _) $($t:tt)*) => {
677        $($p)::*::_9 $($t)*
678    };
679    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
680        $($p)::*::_10 $($t)*
681    };
682    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
683        $($p)::*::_11 $($t)*
684    };
685    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
686        $($p)::*::_12 $($t)*
687    };
688    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
689        $($p)::*::_13 $($t)*
690    };
691    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
692        $($p)::*::_14 $($t)*
693    };
694    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
695        $($p)::*::_15 $($t)*
696    };
697    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
698        $($p)::*::_16 $($t)*
699    };
700    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
701        $($p)::*::_17 $($t)*
702    };
703    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
704        $($p)::*::_18 $($t)*
705    };
706    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
707        $($p)::*::_19 $($t)*
708    };
709    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
710        $($p)::*::_20 $($t)*
711    };
712    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
713        $($p)::*::_21 $($t)*
714    };
715    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
716        $($p)::*::_22 $($t)*
717    };
718    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
719        $($p)::*::_23 $($t)*
720    };
721    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
722        $($p)::*::_24 $($t)*
723    };
724    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
725        $($p)::*::_25 $($t)*
726    };
727    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
728        $($p)::*::_26 $($t)*
729    };
730    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
731        $($p)::*::_27 $($t)*
732    };
733    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
734        $($p)::*::_28 $($t)*
735    };
736    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
737        $($p)::*::_29 $($t)*
738    };
739    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
740        $($p)::*::_30 $($t)*
741    };
742    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
743        $($p)::*::_31 $($t)*
744    };
745    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
746        $($p)::*::_32 $($t)*
747    };
748    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
749        $($p)::*::_33 $($t)*
750    };
751    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
752        $($p)::*::_34 $($t)*
753    };
754    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
755        $($p)::*::_35 $($t)*
756    };
757    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
758        $($p)::*::_36 $($t)*
759    };
760    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
761        $($p)::*::_37 $($t)*
762    };
763    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
764        $($p)::*::_38 $($t)*
765    };
766    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
767        $($p)::*::_39 $($t)*
768    };
769    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
770        $($p)::*::_40 $($t)*
771    };
772    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
773        $($p)::*::_41 $($t)*
774    };
775    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
776        $($p)::*::_42 $($t)*
777    };
778    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
779        $($p)::*::_43 $($t)*
780    };
781    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
782        $($p)::*::_44 $($t)*
783    };
784    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
785        $($p)::*::_45 $($t)*
786    };
787    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
788        $($p)::*::_46 $($t)*
789    };
790    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
791        $($p)::*::_47 $($t)*
792    };
793    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
794        $($p)::*::_48 $($t)*
795    };
796    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
797        $($p)::*::_49 $($t)*
798    };
799    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
800        $($p)::*::_50 $($t)*
801    };
802    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
803        $($p)::*::_51 $($t)*
804    };
805    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
806        $($p)::*::_52 $($t)*
807    };
808    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
809        $($p)::*::_53 $($t)*
810    };
811    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
812        $($p)::*::_54 $($t)*
813    };
814    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
815        $($p)::*::_55 $($t)*
816    };
817    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
818        $($p)::*::_56 $($t)*
819    };
820    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
821        $($p)::*::_57 $($t)*
822    };
823    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
824        $($p)::*::_58 $($t)*
825    };
826    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
827        $($p)::*::_59 $($t)*
828    };
829    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
830        $($p)::*::_60 $($t)*
831    };
832    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
833        $($p)::*::_61 $($t)*
834    };
835    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
836        $($p)::*::_62 $($t)*
837    };
838    ($($p:ident)::*, (_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) $($t:tt)*) => {
839        $($p)::*::_63 $($t)*
840    };
841}