2019-12-18 13:07:27 -08:00
|
|
|
#![doc(html_root_url = "https://docs.rs/tokio-macros/0.2.1")]
|
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-11-16 07:19:45 -08:00
|
|
|
enum Runtime {
|
|
|
|
|
Basic,
|
|
|
|
|
Threaded,
|
2019-10-02 13:58:34 -04:00
|
|
|
Auto,
|
|
|
|
|
}
|
|
|
|
|
|
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:
|
|
|
|
|
///
|
2019-11-16 07:19:45 -08:00
|
|
|
/// - `basic_scheduler` - All tasks are executed on the current thread.
|
|
|
|
|
/// - `threaded_scheduler` - Uses the multi-threaded scheduler. Used by default.
|
2019-06-27 20:40:22 +03:00
|
|
|
///
|
2019-09-24 16:03:26 +02:00
|
|
|
/// ## Function arguments:
|
|
|
|
|
///
|
|
|
|
|
/// Arguments are allowed for any functions aside from `main` which is special
|
|
|
|
|
///
|
2019-06-27 20:40:22 +03:00
|
|
|
/// ## Usage
|
2019-04-25 19:22:32 -07:00
|
|
|
///
|
2019-11-16 07:19:45 -08:00
|
|
|
/// ### Using default
|
2019-06-27 20:40:22 +03:00
|
|
|
///
|
|
|
|
|
/// ```rust
|
2019-11-16 07:19:45 -08:00
|
|
|
/// #[tokio::main]
|
2019-06-27 20:40:22 +03:00
|
|
|
/// async fn main() {
|
|
|
|
|
/// println!("Hello world");
|
|
|
|
|
/// }
|
2019-04-25 19:22:32 -07:00
|
|
|
/// ```
|
2019-11-16 07:19:45 -08:00
|
|
|
///
|
|
|
|
|
/// ### Select runtime
|
2019-06-27 20:40:22 +03:00
|
|
|
///
|
|
|
|
|
/// ```rust
|
2019-11-16 07:19:45 -08:00
|
|
|
/// #[tokio::main(basic_scheduler)]
|
2019-04-25 19:22:32 -07:00
|
|
|
/// 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 {
|
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-09-24 16:03:26 +02:00
|
|
|
let inputs = &input.sig.inputs;
|
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-12-13 17:53:11 +01:00
|
|
|
let vis = input.vis;
|
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-09-24 16:03:26 +02:00
|
|
|
} else if name == "main" && !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-11-16 07:19:45 -08:00
|
|
|
let mut runtime = Runtime::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-11-16 07:19:45 -08:00
|
|
|
"threaded_scheduler" => runtime = Runtime::Threaded,
|
|
|
|
|
"basic_scheduler" => runtime = Runtime::Basic,
|
2019-08-10 02:28:22 +09:00
|
|
|
name => {
|
2019-11-16 07:19:45 -08:00
|
|
|
let msg = format!("Unknown attribute {} is specified; expected `basic_scheduler` or `threaded_scheduler`", 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 {
|
2019-11-16 07:19:45 -08:00
|
|
|
Runtime::Threaded | Runtime::Auto => quote! {
|
2019-06-27 20:40:22 +03:00
|
|
|
#(#attrs)*
|
2019-12-13 17:53:11 +01:00
|
|
|
#vis fn #name(#inputs) #ret {
|
2019-09-24 16:03:26 +02:00
|
|
|
tokio::runtime::Runtime::new().unwrap().block_on(async { #body })
|
2019-06-27 20:40:22 +03:00
|
|
|
}
|
|
|
|
|
},
|
2019-11-16 07:19:45 -08:00
|
|
|
Runtime::Basic => quote! {
|
2019-06-27 20:40:22 +03:00
|
|
|
#(#attrs)*
|
2019-12-13 17:53:11 +01:00
|
|
|
#vis fn #name(#inputs) #ret {
|
2019-11-01 13:18:52 -07:00
|
|
|
tokio::runtime::Builder::new()
|
2019-11-16 07:19:45 -08:00
|
|
|
.basic_scheduler()
|
2019-11-21 23:28:39 -08:00
|
|
|
.enable_all()
|
2019-11-01 13:18:52 -07:00
|
|
|
.build()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.block_on(async { #body })
|
2019-09-24 11:39:36 -07:00
|
|
|
}
|
|
|
|
|
},
|
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
|
|
|
|
|
///
|
2019-10-02 13:58:34 -04:00
|
|
|
/// ## Options:
|
|
|
|
|
///
|
2019-11-16 07:19:45 -08:00
|
|
|
/// - `basic_scheduler` - All tasks are executed on the current thread. Used by default.
|
|
|
|
|
/// - `threaded_scheduler` - Use multi-threaded scheduler.
|
2019-10-02 13:58:34 -04:00
|
|
|
///
|
|
|
|
|
/// ## Usage
|
|
|
|
|
///
|
|
|
|
|
/// ### Select runtime
|
|
|
|
|
///
|
|
|
|
|
/// ```no_run
|
2019-11-16 07:19:45 -08:00
|
|
|
/// #[tokio::test(threaded_scheduler)]
|
2019-10-02 13:58:34 -04:00
|
|
|
/// async fn my_test() {
|
|
|
|
|
/// assert!(true);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2019-04-25 19:22:32 -07:00
|
|
|
///
|
2019-10-02 13:58:34 -04:00
|
|
|
/// ### Using default
|
2019-04-25 19:22:32 -07:00
|
|
|
///
|
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-10-02 13:58:34 -04: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-04-30 19:55:22 -07:00
|
|
|
let attrs = &input.attrs;
|
2019-12-13 17:53:11 +01:00
|
|
|
let vis = input.vis;
|
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
|
|
|
}
|
|
|
|
|
|
2019-11-16 07:19:45 -08:00
|
|
|
let mut runtime = Runtime::Auto;
|
2019-10-02 13:58:34 -04:00
|
|
|
|
|
|
|
|
for arg in args {
|
|
|
|
|
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-11-16 07:19:45 -08:00
|
|
|
"threaded_scheduler" => runtime = Runtime::Threaded,
|
|
|
|
|
"basic_scheduler" => runtime = Runtime::Basic,
|
2019-10-02 13:58:34 -04:00
|
|
|
name => {
|
2019-11-16 07:19:45 -08:00
|
|
|
let msg = format!("Unknown attribute {} is specified; expected `basic_scheduler` or `threaded_scheduler`", name);
|
2019-10-02 13:58:34 -04:00
|
|
|
return syn::Error::new_spanned(path, msg).to_compile_error().into();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-25 19:22:32 -07:00
|
|
|
}
|
2019-10-02 13:58:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = match runtime {
|
2019-11-16 07:19:45 -08:00
|
|
|
Runtime::Threaded => quote! {
|
2019-10-02 13:58:34 -04:00
|
|
|
#[test]
|
|
|
|
|
#(#attrs)*
|
2019-12-13 17:53:11 +01:00
|
|
|
#vis fn #name() #ret {
|
2019-10-02 13:58:34 -04:00
|
|
|
tokio::runtime::Runtime::new().unwrap().block_on(async { #body })
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-11-16 07:19:45 -08:00
|
|
|
Runtime::Basic | Runtime::Auto => quote! {
|
2019-10-02 13:58:34 -04:00
|
|
|
#[test]
|
|
|
|
|
#(#attrs)*
|
2019-12-13 17:53:11 +01:00
|
|
|
#vis fn #name() #ret {
|
2019-11-01 13:18:52 -07:00
|
|
|
tokio::runtime::Builder::new()
|
2019-11-16 07:19:45 -08:00
|
|
|
.basic_scheduler()
|
2019-11-21 23:28:39 -08:00
|
|
|
.enable_all()
|
2019-11-01 13:18:52 -07:00
|
|
|
.build()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.block_on(async { #body })
|
2019-10-02 13:58:34 -04:00
|
|
|
}
|
|
|
|
|
},
|
2019-04-25 19:22:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.into()
|
|
|
|
|
}
|