zng_task_proc_macros/
any_all.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! Macro for `task::any!` or `task::all!` calls with more then 8 futures.

use syn::{
    parse::{Parse, ParseStream},
    parse_macro_input,
    punctuated::Punctuated,
    Expr, Path, Token,
};

pub fn expand(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let Input { macro_path, futs, .. } = parse_macro_input!(input as Input);

    let fut_idents = (0..futs.len()).map(|i| ident!("__fut{i}"));
    let futs = futs.iter();

    let r = quote! {
        #macro_path! {
            #(#fut_idents: #futs;)*
        }
    };

    r.into()
}

struct Input {
    macro_path: Path,
    _path_semi: Token![;],
    futs: Punctuated<Expr, Token![,]>,
}
impl Parse for Input {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        Ok(Input {
            macro_path: input.parse()?,
            _path_semi: input.parse()?,
            futs: Punctuated::parse_separated_nonempty(input)?,
        })
    }
}