From 18cef1901fc805e7ef3443538c0931836891eab7 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 24 Sep 2019 11:39:36 -0700 Subject: tokio: add `rt-current-thread` optional feature - Adds a minimum `rt-current-thread` optional feature that exports `tokio::runtime::current_thread`. - Adds a `macros` optional feature to enable the `#[tokio::main]` and `#[tokio::test]` attributes. - Adjusts `#[tokio::main]` macro to select a runtime "automatically" if a specific strategy isn't specified. Allows using the macro with only the rt-current-thread feature. --- tokio-macros/src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'tokio-macros') diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs index 9a33c5fd..d8eedb02 100644 --- a/tokio-macros/src/lib.rs +++ b/tokio-macros/src/lib.rs @@ -49,6 +49,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { enum RuntimeType { Single, Multi, + Auto, } let input = syn::parse_macro_input!(item as syn::ItemFn); @@ -71,7 +72,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { .into(); } - let mut runtime = RuntimeType::Multi; + let mut runtime = RuntimeType::Auto; for arg in args { if let syn::NestedMeta::Meta(syn::Meta::Path(path)) = arg { @@ -106,6 +107,13 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { rt.block_on(async { #body }) } }, + RuntimeType::Auto => quote! { + #(#attrs)* + fn #name() #ret { + let mut rt = tokio::runtime::__main::Runtime::new().unwrap(); + rt.block_on(async { #body }) + } + }, }; result.into() -- cgit v1.2.3