From dc5fa80a09ff65728d5e9848f2efc1b83de38741 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Tue, 25 Jun 2019 20:14:21 -0700 Subject: [PATCH] macros: re-export `main` macro from `tokio` (#1198) Includes minor fixes and a very basic example. Fixes #1183 --- Cargo.toml | 2 +- azure-pipelines.yml | 1 + ci/azure-test-stable.yml | 2 +- tokio-macros/src/lib.rs | 8 +++++--- tokio/Cargo.toml | 3 ++- tokio/src/lib.rs | 3 +++ 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e522bc7d3..04c3d94cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ members = [ # "tokio-fs", "tokio-futures", "tokio-io", - # "tokio-macros", + "tokio-macros", "tokio-reactor", # "tokio-signal", "tokio-sync", diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 6e948b034..7214cc984 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -48,6 +48,7 @@ jobs: - tokio-current-thread - tokio-executor - tokio-io + - tokio-macros - tokio-sync # - tokio-threadpool # - tokio-timer diff --git a/ci/azure-test-stable.yml b/ci/azure-test-stable.yml index f2fa78975..4280b3c1f 100644 --- a/ci/azure-test-stable.yml +++ b/ci/azure-test-stable.yml @@ -34,7 +34,7 @@ jobs: - template: azure-patch-crates.yml - ${{ each crate in parameters.crates }}: - - script: cargo test --lib && cargo test --tests + - script: cargo test --lib && cargo test --tests && cargo test --examples env: LOOM_MAX_DURATION: 10 CI: 'True' diff --git a/tokio-macros/src/lib.rs b/tokio-macros/src/lib.rs index 000b3773e..80c838e61 100644 --- a/tokio-macros/src/lib.rs +++ b/tokio-macros/src/lib.rs @@ -17,9 +17,11 @@ use syn::spanned::Spanned; /// ``` /// #[tokio::main] /// async fn main() { -/// println!("Hello world"); +/// println!("Hello from Tokio!"); /// } +/// ``` #[proc_macro_attribute] +#[cfg(not(test))] // Work around for rust-lang/rust#62127 pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(item as syn::ItemFn); @@ -38,7 +40,7 @@ pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream { let result = quote! { fn #name() #ret { let mut rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on_async(async { #body }) + rt.block_on(async { #body }) } }; @@ -49,7 +51,7 @@ pub fn main(_attr: TokenStream, item: TokenStream) -> TokenStream { /// /// # Examples /// -/// ``` +/// ```ignore /// #[tokio::test] /// async fn my_test() { /// assert!(true); diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 79d8dca8c..23ad5faf1 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -48,6 +48,7 @@ rt-full = [ # "timer", "tokio-current-thread", "tokio-executor", + "tokio-macros", # "tokio-threadpool", # "tokio-trace-core", ] @@ -69,7 +70,7 @@ tokio-current-thread = { version = "0.2.0", optional = true, path = "../tokio-cu #tokio-fs = { version = "0.2.0", optional = true, path = "../tokio-fs" } tokio-io = { version = "0.2.0", optional = true, path = "../tokio-io" } tokio-executor = { version = "0.2.0", optional = true, path = "../tokio-executor" } -#tokio-macros = { version = "0.1.0", optional = true, path = "../tokio-macros" } +tokio-macros = { version = "0.1.0", optional = true, path = "../tokio-macros" } tokio-reactor = { version = "0.2.0", optional = true, path = "../tokio-reactor" } tokio-sync = { version = "0.2.0", optional = true, path = "../tokio-sync" } #tokio-threadpool = { version = "0.2.0", optional = true, path = "../tokio-threadpool" } diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 7436a2409..b96c2580a 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -99,4 +99,7 @@ if_runtime! { pub use crate::executor::spawn; pub use crate::runtime::run; + + #[cfg(not(test))] // Work around for rust-lang/rust#62127 + pub use tokio_macros::main; }