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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Procedural macro for the `#[async_stream]` attribute.

#![recursion_limit = "256"]
#![doc(html_root_url = "https://docs.rs/futures-async-stream-macro/0.1.4")]
#![doc(test(
    no_crate_inject,
    attr(deny(warnings, rust_2018_idioms, single_use_lifetimes), allow(dead_code))
))]
#![warn(unsafe_code)]
#![warn(rust_2018_idioms, single_use_lifetimes, unreachable_pub)]
#![warn(clippy::all)]

use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
use quote::ToTokens;
use syn::{Expr, ExprForLoop};

#[macro_use]
mod utils;

mod elision;
mod stream;
mod try_stream;
mod visitor;

/// Processes streams using a for loop.
#[proc_macro_attribute]
pub fn for_await(args: TokenStream, input: TokenStream) -> TokenStream {
    if let Err(e) = utils::parse_as_empty(&args.into()) {
        return e.to_compile_error().into();
    }
    let mut expr: ExprForLoop = syn::parse_macro_input!(input);

    expr.attrs.push(syn::parse_quote!(#[for_await]));
    let mut expr = Expr::ForLoop(expr);
    visitor::Visitor::default().visit_for_loop(&mut expr);
    expr.into_token_stream().into()
}

/// Creates streams via generators.
#[proc_macro_attribute]
pub fn async_stream(args: TokenStream, input: TokenStream) -> TokenStream {
    stream::attribute(args.into(), input.into()).unwrap_or_else(|e| e.to_compile_error()).into()
}

/// Creates streams via generators.
#[proc_macro]
pub fn async_stream_block(input: TokenStream) -> TokenStream {
    let input = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Brace, input)));
    stream::block_macro(input.into()).unwrap_or_else(|e| e.to_compile_error()).into()
}

/// Creates streams via generators.
#[proc_macro_attribute]
pub fn async_try_stream(args: TokenStream, input: TokenStream) -> TokenStream {
    try_stream::attribute(args.into(), input.into()).unwrap_or_else(|e| e.to_compile_error()).into()
}

/// Creates streams via generators.
#[proc_macro]
pub fn async_try_stream_block(input: TokenStream) -> TokenStream {
    let input = TokenStream::from(TokenTree::Group(Group::new(Delimiter::Brace, input)));
    try_stream::block_macro(input.into()).unwrap_or_else(|e| e.to_compile_error()).into()
}