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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Block, FnArg, Ident, ImplItem, ItemImpl, LitStr};

/// Generate a blocking method for each async method in an impl block. Supports either `tokio` or `async-std` backend.
/// Generated methods are suffixed with `_blocking`.
///
/// # Example `tokio`
/// ```
/// use block_on::block_on;
///
/// struct Tokio {}
///
/// #[block_on("tokio")]
/// impl Tokio {
///     async fn test_async(&self) {}        
/// }
/// ```
///
/// Generates the following impl block
/// ```no_run
/// # struct Dummy {}
/// # impl Dummy {
/// async fn test_async(&self) {}
///         
/// fn test_async_blocking(&self) {
///     use tokio::runtime::Runtime;
///     let mut rt = Runtime::new().unwrap();
///     rt.block_on(self.test_async())
/// }
/// # }
/// ```
///
/// # Example `async-std`
/// ```
/// use block_on::block_on;
///
/// struct AsyncStd {}
///
/// #[block_on("async-std")]
/// impl AsyncStd {
///     async fn test_async(&self) {}        
/// }
/// ```
///
/// Generates the following method in the same impl block
/// ```no_run
/// # struct Dummy {}
/// # impl Dummy {
/// async fn test_async(&self) {}        
///
/// fn test_async_blocking(&self) {
///       use async_std::task;
///       task::block_on(self.test_async())
/// }
/// # }
/// ```

#[proc_macro_attribute]
pub fn block_on(attr: TokenStream, tokens: TokenStream) -> TokenStream {
    let attr = parse_macro_input!(attr as LitStr).value();

    let orig_tokens = tokens.clone();

    let in_impl = parse_macro_input!(orig_tokens as ItemImpl);
    let strct = in_impl.self_ty.clone();
    let mut orig_impl = in_impl.clone();
    let mut out_impl = in_impl.clone();
    out_impl.items = Vec::new();

    for item in in_impl.items {
        match item {
            ImplItem::Method(method) => {
                let name = &method.sig.ident;
                let mut out_method = method.clone();
                if out_method.sig.asyncness.is_none() {
                    continue;
                }
                out_method.sig.asyncness = None;

                out_method.sig.ident = Ident::new(
                    &format!("{}_blocking", method.sig.ident.to_string()),
                    method.sig.ident.span(),
                );

                let inputs = &method.sig.inputs;

                let rec = inputs.into_iter().any(|arg| match arg {
                    FnArg::Receiver(_) => true,
                    FnArg::Typed(_) => false,
                });

                let call_args = inputs
                    .into_iter()
                    .map(|arg| match arg {
                        FnArg::Receiver(_) => None,
                        FnArg::Typed(arg) => Some(arg.pat.clone()),
                    })
                    .filter(|pat| pat.is_some())
                    .map(|arg| arg.unwrap());

                let block_proc2 = if rec {
                    {
                        if attr == "tokio" {
                            quote! {
                                    {
                                        use tokio::runtime::Runtime;
                                        let mut rt = Runtime::new().unwrap();
                                        rt.block_on(self.#name(#(#call_args),*))
                                    }
                            }
                        } else if attr == "async-std" {
                            quote! {
                                    {
                                        use async_std::task;
                                        task::block_on(self.#name(#(#call_args),*))
                                    }
                            }
                        } else {
                            panic!("Only `tokio` and `async-std` backends are supported!")
                        }
                    }
                } else {
                    if attr == "tokio" {
                        quote! {
                                {
                                    use tokio::runtime::Runtime;
                                    let mut rt = Runtime::new().unwrap();
                                    rt.block_on(#strct::#name(#(#call_args),*))
                                }
                        }
                    } else if attr == "async-std" {
                        quote! {
                                {
                                    use async_std::task;
                                    task::block_on(#strct::#name(#(#call_args),*))
                                }
                        }
                    } else {
                        panic!("Only `tokio` and `async-std` backends are supported!")
                    }
                };

                let block_proc = proc_macro::TokenStream::from(block_proc2);
                out_method.block = parse_macro_input!(block_proc as Block);
                orig_impl.items.push(ImplItem::Method(out_method));
            }
            _ => {}
        }
    }

    // Returns generated tokens
    let out = quote! {
        #orig_impl
    };

    out.into()
}

#[test]
fn ui() {
    let t = trybuild::TestCases::new();
    t.pass("src/test.rs");
}