From 3824493fd3cb3e7fbaf6cc46abca72c818436143 Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Tue, 8 Jun 2021 12:33:05 -0400 Subject: [PATCH] take custom system path in `actix_rt::main` macro (#363) --- actix-macros/CHANGES.md | 3 ++ actix-macros/src/lib.rs | 42 +++++++++++++++++-- actix-macros/tests/trybuild.rs | 3 ++ .../tests/trybuild/main-04-system-path.rs | 8 ++++ .../trybuild/main-05-system-expect-path.rs | 4 ++ .../main-05-system-expect-path.stderr | 5 +++ .../tests/trybuild/main-06-unknown-attr.rs | 7 ++++ .../trybuild/main-06-unknown-attr.stderr | 11 +++++ 8 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 actix-macros/tests/trybuild/main-04-system-path.rs create mode 100644 actix-macros/tests/trybuild/main-05-system-expect-path.rs create mode 100644 actix-macros/tests/trybuild/main-05-system-expect-path.stderr create mode 100644 actix-macros/tests/trybuild/main-06-unknown-attr.rs create mode 100644 actix-macros/tests/trybuild/main-06-unknown-attr.stderr diff --git a/actix-macros/CHANGES.md b/actix-macros/CHANGES.md index e5245775..96098c91 100644 --- a/actix-macros/CHANGES.md +++ b/actix-macros/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx +* Add optional argument `system` to `main` macro which can be used to specify the path to `actix_rt::System` (useful for re-exports). [#363] + +[#363]: https://github.com/actix/actix-net/pull/363 ## 0.2.0 - 2021-02-02 diff --git a/actix-macros/src/lib.rs b/actix-macros/src/lib.rs index 0d634a59..d8aa5f77 100644 --- a/actix-macros/src/lib.rs +++ b/actix-macros/src/lib.rs @@ -27,8 +27,10 @@ use quote::quote; #[allow(clippy::needless_doctest_main)] #[proc_macro_attribute] #[cfg(not(test))] // Work around for rust-lang/rust#62127 -pub fn main(_: TokenStream, item: TokenStream) -> TokenStream { +pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { let mut input = syn::parse_macro_input!(item as syn::ItemFn); + let args = syn::parse_macro_input!(args as syn::AttributeArgs); + let attrs = &input.attrs; let vis = &input.vis; let sig = &mut input.sig; @@ -43,13 +45,47 @@ pub fn main(_: TokenStream, item: TokenStream) -> TokenStream { .into(); } + let mut system = syn::parse_str::("::actix_rt::System").unwrap(); + + for arg in &args { + match arg { + syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { + lit: syn::Lit::Str(lit), + path, + .. + })) => match path + .get_ident() + .map(|i| i.to_string().to_lowercase()) + .as_deref() + { + Some("system") => match lit.parse() { + Ok(path) => system = path, + Err(_) => { + return syn::Error::new_spanned(lit, "Expected path") + .to_compile_error() + .into(); + } + }, + _ => { + return syn::Error::new_spanned(arg, "Unknown attribute specified") + .to_compile_error() + .into(); + } + }, + _ => { + return syn::Error::new_spanned(arg, "Unknown attribute specified") + .to_compile_error() + .into(); + } + } + } + sig.asyncness = None; (quote! { #(#attrs)* #vis #sig { - actix_rt::System::new() - .block_on(async move { #body }) + <#system>::new().block_on(async move { #body }) } }) .into() diff --git a/actix-macros/tests/trybuild.rs b/actix-macros/tests/trybuild.rs index a726d3bd..410d9499 100644 --- a/actix-macros/tests/trybuild.rs +++ b/actix-macros/tests/trybuild.rs @@ -4,6 +4,9 @@ fn compile_macros() { t.pass("tests/trybuild/main-01-basic.rs"); t.compile_fail("tests/trybuild/main-02-only-async.rs"); t.pass("tests/trybuild/main-03-fn-params.rs"); + t.pass("tests/trybuild/main-04-system-path.rs"); + t.compile_fail("tests/trybuild/main-05-system-expect-path.rs"); + t.compile_fail("tests/trybuild/main-06-unknown-attr.rs"); t.pass("tests/trybuild/test-01-basic.rs"); t.pass("tests/trybuild/test-02-keep-attrs.rs"); diff --git a/actix-macros/tests/trybuild/main-04-system-path.rs b/actix-macros/tests/trybuild/main-04-system-path.rs new file mode 100644 index 00000000..549dc5c7 --- /dev/null +++ b/actix-macros/tests/trybuild/main-04-system-path.rs @@ -0,0 +1,8 @@ +mod system { + pub use actix_rt::System as MySystem; +} + +#[actix_rt::main(system = "system::MySystem")] +async fn main() { + futures_util::future::ready(()).await +} diff --git a/actix-macros/tests/trybuild/main-05-system-expect-path.rs b/actix-macros/tests/trybuild/main-05-system-expect-path.rs new file mode 100644 index 00000000..af541977 --- /dev/null +++ b/actix-macros/tests/trybuild/main-05-system-expect-path.rs @@ -0,0 +1,4 @@ +#[actix_rt::main(system = "!@#*&")] +async fn main2() {} + +fn main() {} diff --git a/actix-macros/tests/trybuild/main-05-system-expect-path.stderr b/actix-macros/tests/trybuild/main-05-system-expect-path.stderr new file mode 100644 index 00000000..7b9c3d83 --- /dev/null +++ b/actix-macros/tests/trybuild/main-05-system-expect-path.stderr @@ -0,0 +1,5 @@ +error: Expected path + --> $DIR/main-05-system-expect-path.rs:1:27 + | +1 | #[actix_rt::main(system = "!@#*&")] + | ^^^^^^^ diff --git a/actix-macros/tests/trybuild/main-06-unknown-attr.rs b/actix-macros/tests/trybuild/main-06-unknown-attr.rs new file mode 100644 index 00000000..72163d25 --- /dev/null +++ b/actix-macros/tests/trybuild/main-06-unknown-attr.rs @@ -0,0 +1,7 @@ +#[actix_rt::main(foo = "bar")] +async fn async_main() {} + +#[actix_rt::main(bar::baz)] +async fn async_main2() {} + +fn main() {} diff --git a/actix-macros/tests/trybuild/main-06-unknown-attr.stderr b/actix-macros/tests/trybuild/main-06-unknown-attr.stderr new file mode 100644 index 00000000..d2ae1ca8 --- /dev/null +++ b/actix-macros/tests/trybuild/main-06-unknown-attr.stderr @@ -0,0 +1,11 @@ +error: Unknown attribute specified + --> $DIR/main-06-unknown-attr.rs:1:18 + | +1 | #[actix_rt::main(foo = "bar")] + | ^^^^^^^^^^^ + +error: Unknown attribute specified + --> $DIR/main-06-unknown-attr.rs:4:18 + | +4 | #[actix_rt::main(bar::baz)] + | ^^^^^^^^