async_dispatcher_macros/
lib.rs1use proc_macro::TokenStream;
2use syn::spanned::Spanned as _;
3
4#[proc_macro_attribute]
5pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
6 let input = syn::parse_macro_input!(item as syn::ItemFn);
7
8 let ret = &input.sig.output;
9 let name = &input.sig.ident;
10 let body = &input.block;
11 let attrs = &input.attrs;
12 let vis = &input.vis;
13
14 if input.sig.asyncness.is_none() {
15 return TokenStream::from(quote::quote_spanned! { input.span() =>
16 compile_error!("the async keyword is missing from the function declaration"),
17 });
18 }
19
20 let result = quote::quote! {
21 #[::core::prelude::v1::test]
22 #(#attrs)*
23 #vis fn #name() #ret {
24 ::async_dispatcher::set_dispatcher(::async_dispatcher::thread_dispatcher());
25 ::async_dispatcher::block_on(async { #body })
26 }
27 };
28
29 result.into()
30}
31
32#[proc_macro_attribute]
33pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream {
34 let input = syn::parse_macro_input!(item as syn::ItemFn);
35
36 let ret = &input.sig.output;
37 let body = &input.block;
38 let attrs = &input.attrs;
39
40 if input.sig.asyncness.is_none() {
41 return TokenStream::from(quote::quote_spanned! { input.span() =>
42 compile_error!("the async keyword is missing from the function declaration"),
43 });
44 }
45
46 let result = quote::quote! {
47 #(#attrs)*
48 fn main() #ret {
49 ::async_dispatcher::set_dispatcher(::async_dispatcher::thread_dispatcher());
50 ::async_dispatcher::block_on(async { #body })
51 }
52 };
53
54 result.into()
55}