Skip to content

Commit 3824493

Browse files
authored
take custom system path in actix_rt::main macro (#363)
1 parent 3be3e11 commit 3824493

8 files changed

+80
-3
lines changed

Diff for: actix-macros/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changes
22

33
## Unreleased - 2021-xx-xx
4+
* Add optional argument `system` to `main` macro which can be used to specify the path to `actix_rt::System` (useful for re-exports). [#363]
5+
6+
[#363]: https://github.com/actix/actix-net/pull/363
47

58

69
## 0.2.0 - 2021-02-02

Diff for: actix-macros/src/lib.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ use quote::quote;
2727
#[allow(clippy::needless_doctest_main)]
2828
#[proc_macro_attribute]
2929
#[cfg(not(test))] // Work around for rust-lang/rust#62127
30-
pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
30+
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
3131
let mut input = syn::parse_macro_input!(item as syn::ItemFn);
32+
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
33+
3234
let attrs = &input.attrs;
3335
let vis = &input.vis;
3436
let sig = &mut input.sig;
@@ -43,13 +45,47 @@ pub fn main(_: TokenStream, item: TokenStream) -> TokenStream {
4345
.into();
4446
}
4547

48+
let mut system = syn::parse_str::<syn::Path>("::actix_rt::System").unwrap();
49+
50+
for arg in &args {
51+
match arg {
52+
syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
53+
lit: syn::Lit::Str(lit),
54+
path,
55+
..
56+
})) => match path
57+
.get_ident()
58+
.map(|i| i.to_string().to_lowercase())
59+
.as_deref()
60+
{
61+
Some("system") => match lit.parse() {
62+
Ok(path) => system = path,
63+
Err(_) => {
64+
return syn::Error::new_spanned(lit, "Expected path")
65+
.to_compile_error()
66+
.into();
67+
}
68+
},
69+
_ => {
70+
return syn::Error::new_spanned(arg, "Unknown attribute specified")
71+
.to_compile_error()
72+
.into();
73+
}
74+
},
75+
_ => {
76+
return syn::Error::new_spanned(arg, "Unknown attribute specified")
77+
.to_compile_error()
78+
.into();
79+
}
80+
}
81+
}
82+
4683
sig.asyncness = None;
4784

4885
(quote! {
4986
#(#attrs)*
5087
#vis #sig {
51-
actix_rt::System::new()
52-
.block_on(async move { #body })
88+
<#system>::new().block_on(async move { #body })
5389
}
5490
})
5591
.into()

Diff for: actix-macros/tests/trybuild.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ fn compile_macros() {
44
t.pass("tests/trybuild/main-01-basic.rs");
55
t.compile_fail("tests/trybuild/main-02-only-async.rs");
66
t.pass("tests/trybuild/main-03-fn-params.rs");
7+
t.pass("tests/trybuild/main-04-system-path.rs");
8+
t.compile_fail("tests/trybuild/main-05-system-expect-path.rs");
9+
t.compile_fail("tests/trybuild/main-06-unknown-attr.rs");
710

811
t.pass("tests/trybuild/test-01-basic.rs");
912
t.pass("tests/trybuild/test-02-keep-attrs.rs");

Diff for: actix-macros/tests/trybuild/main-04-system-path.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod system {
2+
pub use actix_rt::System as MySystem;
3+
}
4+
5+
#[actix_rt::main(system = "system::MySystem")]
6+
async fn main() {
7+
futures_util::future::ready(()).await
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[actix_rt::main(system = "!@#*&")]
2+
async fn main2() {}
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: Expected path
2+
--> $DIR/main-05-system-expect-path.rs:1:27
3+
|
4+
1 | #[actix_rt::main(system = "!@#*&")]
5+
| ^^^^^^^

Diff for: actix-macros/tests/trybuild/main-06-unknown-attr.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[actix_rt::main(foo = "bar")]
2+
async fn async_main() {}
3+
4+
#[actix_rt::main(bar::baz)]
5+
async fn async_main2() {}
6+
7+
fn main() {}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: Unknown attribute specified
2+
--> $DIR/main-06-unknown-attr.rs:1:18
3+
|
4+
1 | #[actix_rt::main(foo = "bar")]
5+
| ^^^^^^^^^^^
6+
7+
error: Unknown attribute specified
8+
--> $DIR/main-06-unknown-attr.rs:4:18
9+
|
10+
4 | #[actix_rt::main(bar::baz)]
11+
| ^^^^^^^^

0 commit comments

Comments
 (0)