2019-09-30 18:35:52 -04:00
|
|
|
#![doc(html_root_url = "https://docs.rs/tokio-macros/0.2.0-alpha.6")]
|
2019-08-11 04:28:52 +09:00
|
|
|
#![warn(
|
|
|
|
|
missing_debug_implementations,
|
|
|
|
|
missing_docs,
|
|
|
|
|
rust_2018_idioms,
|
|
|
|
|
unreachable_pub
|
|
|
|
|
)]
|
2019-09-13 06:46:19 -10:00
|
|
|
#![deny(intra_doc_link_resolution_failure)]
|
2019-09-19 15:50:12 +09:00
|
|
|
#![doc(test(
|
|
|
|
|
no_crate_inject,
|
|
|
|
|
attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
|
|
|
|
|
))]
|
2019-05-14 10:27:36 -07:00
|
|
|
|
|
|
|
|
//! Macros for use with Tokio
|
2019-04-25 19:22:32 -07:00
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
2019-08-10 02:28:22 +09:00
|
|
|
use quote::quote;
|
2019-04-25 19:22:32 -07:00
|
|
|
|
2019-06-27 20:40:22 +03:00
|
|
|
/// Marks async function to be executed by selected runtime.
|
2019-04-25 19:22:32 -07:00
|
|
|
///
|
2019-06-27 20:40:22 +03:00
|
|
|
/// ## Options:
|
|
|
|
|
///
|
|
|
|
|
/// - `single_thread` - Uses `current_thread`.
|
|
|
|
|
/// - `multi_thread` - Uses multi-threaded runtime. Used by default.
|
|
|
|
|
///
|
|
|
|
|
/// ## Usage
|
2019-04-25 19:22:32 -07:00
|
|
|
///
|
2019-06-27 20:40:22 +03:00
|
|
|
/// ### Select runtime
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
|
|
|
|
/// #[tokio::main(single_thread)]
|
|
|
|
|
/// async fn main() {
|
|
|
|
|
/// println!("Hello world");
|
|
|
|
|
/// }
|
2019-04-25 19:22:32 -07:00
|
|
|
/// ```
|
2019-06-27 20:40:22 +03:00
|
|
|
/// ### Using default
|
|
|
|
|
///
|
|
|
|
|
/// ```rust
|
2019-04-25 19:22:32 -07:00
|
|
|
/// #[tokio::main]
|
|
|
|
|
/// async fn main() {
|
2019-06-27 20:40:22 +03:00
|
|
|
/// println!("Hello world");
|
2019-04-25 19:22:32 -07:00
|
|
|
/// }
|
2019-06-25 20:14:21 -07:00
|
|
|
/// ```
|
2019-04-25 19:22:32 -07:00
|
|
|
#[proc_macro_attribute]
|
2019-06-25 20:14:21 -07:00
|
|
|
#[cfg(not(test))] // Work around for rust-lang/rust#62127
|
2019-06-27 20:40:22 +03:00
|
|
|
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
|
|
|
|
|
enum RuntimeType {
|
|
|
|
|
Single,
|
|
|
|
|
Multi,
|
2019-09-24 11:39:36 -07:00
|
|
|
Auto,
|
2019-06-27 20:40:22 +03:00
|
|
|
}
|
|
|
|
|
|
2019-04-25 19:22:32 -07:00
|
|
|
let input = syn::parse_macro_input!(item as syn::ItemFn);
|
2019-06-27 20:40:22 +03:00
|
|
|
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
|
2019-04-25 19:22:32 -07:00
|
|
|
|
2019-08-14 06:11:26 +02:00
|
|
|
let ret = &input.sig.output;
|
|
|
|
|
let name = &input.sig.ident;
|
2019-04-25 19:22:32 -07:00
|
|
|
let body = &input.block;
|
2019-06-27 20:40:22 +03:00
|
|
|
let attrs = &input.attrs;
|
2019-04-25 19:22:32 -07:00
|
|
|
|
2019-08-14 06:11:26 +02:00
|
|
|
if input.sig.asyncness.is_none() {
|
2019-08-10 02:28:22 +09:00
|
|
|
let msg = "the async keyword is missing from the function declaration";
|
2019-08-14 06:11:26 +02:00
|
|
|
return syn::Error::new_spanned(input.sig.fn_token, msg)
|
2019-08-10 02:28:22 +09:00
|
|
|
.to_compile_error()
|
|
|
|
|
.into();
|
2019-08-14 06:11:26 +02:00
|
|
|
} else if !input.sig.inputs.is_empty() {
|
2019-08-10 02:28:22 +09:00
|
|
|
let msg = "the main function cannot accept arguments";
|
2019-08-14 06:11:26 +02:00
|
|
|
return syn::Error::new_spanned(&input.sig.inputs, msg)
|
2019-08-10 02:28:22 +09:00
|
|
|
.to_compile_error()
|
|
|
|
|
.into();
|
2019-04-25 19:22:32 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-24 11:39:36 -07:00
|
|
|
let mut runtime = RuntimeType::Auto;
|
2019-06-27 20:40:22 +03:00
|
|
|
|
|
|
|
|
for arg in args {
|
2019-09-23 03:05:04 +09:00
|
|
|
if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg {
|
|
|
|
|
let ident = path.get_ident();
|
|
|
|
|
if ident.is_none() {
|
|
|
|
|
let msg = "Must have specified ident";
|
|
|
|
|
return syn::Error::new_spanned(path, msg).to_compile_error().into();
|
|
|
|
|
}
|
|
|
|
|
match ident.unwrap().to_string().to_lowercase().as_str() {
|
2019-07-26 03:47:14 +09:00
|
|
|
"multi_thread" => runtime = RuntimeType::Multi,
|
|
|
|
|
"single_thread" => runtime = RuntimeType::Single,
|
2019-08-10 02:28:22 +09:00
|
|
|
name => {
|
|
|
|
|
let msg = format!("Unknown attribute {} is specified", name);
|
2019-09-23 03:05:04 +09:00
|
|
|
return syn::Error::new_spanned(path, msg).to_compile_error().into();
|
2019-08-10 02:28:22 +09:00
|
|
|
}
|
2019-06-27 11:33:36 -07:00
|
|
|
}
|
2019-06-27 20:40:22 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = match runtime {
|
|
|
|
|
RuntimeType::Multi => quote! {
|
|
|
|
|
#(#attrs)*
|
|
|
|
|
fn #name() #ret {
|
|
|
|
|
let mut rt = tokio::runtime::Runtime::new().unwrap();
|
|
|
|
|
rt.block_on(async { #body })
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
RuntimeType::Single => quote! {
|
|
|
|
|
#(#attrs)*
|
|
|
|
|
fn #name() #ret {
|
|
|
|
|
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
|
|
|
|
|
rt.block_on(async { #body })
|
|
|
|
|
}
|
2019-06-27 11:33:36 -07:00
|
|
|
},
|
2019-09-24 11:39:36 -07:00
|
|
|
RuntimeType::Auto => quote! {
|
|
|
|
|
#(#attrs)*
|
|
|
|
|
fn #name() #ret {
|
|
|
|
|
let mut rt = tokio::runtime::__main::Runtime::new().unwrap();
|
|
|
|
|
rt.block_on(async { #body })
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-04-25 19:22:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.into()
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 20:40:22 +03:00
|
|
|
/// Marks async function to be executed by runtime, suitable to test enviornment
|
|
|
|
|
///
|
|
|
|
|
/// Uses `current_thread` runtime.
|
2019-04-25 19:22:32 -07:00
|
|
|
///
|
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
2019-08-09 17:04:41 +02:00
|
|
|
/// ```no_run
|
2019-04-25 19:22:32 -07:00
|
|
|
/// #[tokio::test]
|
|
|
|
|
/// async fn my_test() {
|
|
|
|
|
/// assert!(true);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[proc_macro_attribute]
|
2019-09-23 04:09:30 +09:00
|
|
|
pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
|
2019-04-25 19:22:32 -07:00
|
|
|
let input = syn::parse_macro_input!(item as syn::ItemFn);
|
2019-09-23 04:09:30 +09:00
|
|
|
let _ = syn::parse_macro_input!(args as syn::parse::Nothing);
|
2019-04-25 19:22:32 -07:00
|
|
|
|
2019-08-14 06:11:26 +02:00
|
|
|
let ret = &input.sig.output;
|
|
|
|
|
let name = &input.sig.ident;
|
2019-04-25 19:22:32 -07:00
|
|
|
let body = &input.block;
|
2019-04-30 19:55:22 -07:00
|
|
|
let attrs = &input.attrs;
|
2019-04-25 19:22:32 -07:00
|
|
|
|
2019-07-22 18:28:07 +02:00
|
|
|
for attr in attrs {
|
|
|
|
|
if attr.path.is_ident("test") {
|
2019-08-10 02:28:22 +09:00
|
|
|
let msg = "second test attribute is supplied";
|
|
|
|
|
return syn::Error::new_spanned(&attr, msg)
|
|
|
|
|
.to_compile_error()
|
|
|
|
|
.into();
|
2019-07-22 18:28:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 06:11:26 +02:00
|
|
|
if input.sig.asyncness.is_none() {
|
2019-08-10 02:28:22 +09:00
|
|
|
let msg = "the async keyword is missing from the function declaration";
|
2019-09-23 04:09:30 +09:00
|
|
|
return syn::Error::new_spanned(&input.sig.fn_token, msg)
|
2019-08-10 02:28:22 +09:00
|
|
|
.to_compile_error()
|
|
|
|
|
.into();
|
2019-08-14 06:11:26 +02:00
|
|
|
} else if !input.sig.inputs.is_empty() {
|
2019-08-10 02:28:22 +09:00
|
|
|
let msg = "the test function cannot accept arguments";
|
2019-08-14 06:11:26 +02:00
|
|
|
return syn::Error::new_spanned(&input.sig.inputs, msg)
|
2019-08-10 02:28:22 +09:00
|
|
|
.to_compile_error()
|
|
|
|
|
.into();
|
2019-04-25 19:22:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = quote! {
|
|
|
|
|
#[test]
|
2019-04-30 19:55:22 -07:00
|
|
|
#(#attrs)*
|
2019-04-25 19:22:32 -07:00
|
|
|
fn #name() #ret {
|
|
|
|
|
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
|
2019-06-27 02:41:36 +08:00
|
|
|
rt.block_on(async { #body })
|
2019-04-25 19:22:32 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.into()
|
|
|
|
|
}
|