Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 //! Macros for Actix system and runtime.
2 //!
3 //! The [`actix-rt`](https://docs.rs/actix-rt) crate must be available for macro output to compile.
4 //!
5 //! # Entry-point
6 //! See docs for the [`#[main]`](macro@main) macro.
7 //!
8 //! # Tests
9 //! See docs for the [`#[test]`](macro@test) macro.
10
11 #![deny(rust_2018_idioms, nonstandard_style)]
12 #![warn(future_incompatible)]
13 #![doc(html_logo_url = "https://actix.rs/img/logo.png")]
14 #![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
15
16 use proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#;
17 use quoterust-analyzer cargo quote 1.0.47 crate/::quoterust-analyzer cargo quote 1.0.47 quote!;
18 use synrust-analyzer cargo syn 2.0.119 crate/::parserust-analyzer cargo syn 2.0.119 parse/::Parserrust-analyzer cargo syn 2.0.119 parse/Parser# as _;
19
20 type AttributeArgsrust-analyzer cargo actix-macros 0.2.4 AttributeArgs# = synrust-analyzer cargo syn 2.0.119 crate/::punctuatedrust-analyzer cargo syn 2.0.119 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.119 punctuated/Punctuated#<synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#, synrust-analyzer cargo syn 2.0.119 crate/::Tokenrust-analyzer cargo syn 2.0.119 token/Token!![,]>;
21
22 /// Marks async entry-point function to be executed by Actix system.
23 // #[allow(clippy::needless_doctest_main)]
24 // #[cfg(not(test))] // Work around for rust-lang/rust#62127
25 #[proc_macro_attribute]
26 pub fn mainrust-analyzer cargo actix-macros 0.2.4 main:(args: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, item: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
27 let mut input = match synrust-analyzer cargo syn 2.0.119 crate/::parserust-analyzer cargo syn 2.0.119 parse().::<synrust-analyzer cargo syn 2.0.119 crate/::ItemFnrust-analyzer cargo syn 2.0.119 item/ItemFn#>(item.clonerust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro impl#[TokenStream][Clone]clone().()) {
28 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(input) => input,
29 // on parse err, make IDEs happy; see fn docs
30 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => return input_and_compile_errorrust-analyzer cargo actix-macros 0.2.4 input_and_compile_error().(item, err),
31 };
32
33 let parser = AttributeArgsrust-analyzer cargo actix-macros 0.2.4 AttributeArgs#::parse_terminatedrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]parse_terminated().;
34 let args = match parser.parserust-analyzer cargo syn 2.0.119 parse/Parser#parse().(args.clonerust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro impl#[TokenStream][Clone]clone().()) {
35 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(args) => args,
36 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => return input_and_compile_errorrust-analyzer cargo actix-macros 0.2.4 input_and_compile_error().(args, err),
37 };
38
39 let attrs = &input.attrsrust-analyzer cargo syn 2.0.119 item/ItemFn#attrs.;
40 let vis = &input.visrust-analyzer cargo syn 2.0.119 item/ItemFn#vis.;
41 let sig = &mut input.sigrust-analyzer cargo syn 2.0.119 item/ItemFn#sig.;
42 let body = &input.blockrust-analyzer cargo syn 2.0.119 item/ItemFn#block.;
43
44 if sig.asyncnessrust-analyzer cargo syn 2.0.119 item/Signature#asyncness..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
45 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(
46 sig.fn_tokenrust-analyzer cargo syn 2.0.119 item/Signature#fn_token.,
47 "the async keyword is missing from the function declaration",
48 )
49 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
50 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
51 }
52
53 let mut system = synrust-analyzer cargo syn 2.0.119 crate/::parse_strrust-analyzer cargo syn 2.0.119 parse_str().::<synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#>("::actix_rt::System").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
54
55 for arg in &args {
56 match arg {
57 synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#::NameValuerust-analyzer cargo syn 2.0.119 attr/Meta#NameValue#(synrust-analyzer cargo syn 2.0.119 crate/::MetaNameValuerust-analyzer cargo syn 2.0.119 attr/MetaNameValue# {
58 pathrust-analyzer cargo syn 2.0.119 attr/MetaNameValue#path.,
59 valuerust-analyzer cargo syn 2.0.119 attr/MetaNameValue#value.:
60 synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Litrust-analyzer cargo syn 2.0.119 expr/Expr#Lit#(synrust-analyzer cargo syn 2.0.119 crate/::ExprLitrust-analyzer cargo syn 2.0.119 expr/ExprLit# {
61 litrust-analyzer cargo syn 2.0.119 expr/ExprLit#lit.: synrust-analyzer cargo syn 2.0.119 crate/::Litrust-analyzer cargo syn 2.0.119 lit/Lit#::Strrust-analyzer cargo syn 2.0.119 lit/Lit#Str#(lit),
62 ..
63 }),
64 ..
65 }) => match path
66 .get_identrust-analyzer cargo syn 2.0.119 path/impl#[Path]get_ident().()
67 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|i| i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().to_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_lowercase().())
68 .as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
69 {
70 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("system") => match lit.parserust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]parse().() {
71 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) => system = path,
72 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
73 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(lit, "Expected path")
74 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
75 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
76 }
77 },
78 _ => {
79 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(arg, "Unknown attribute specified")
80 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
81 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
82 }
83 },
84
85 _ => {
86 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(arg, "Unknown attribute specified")
87 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
88 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
89 }
90 }
91 }
92
93 sig.asyncnessrust-analyzer cargo syn 2.0.119 item/Signature#asyncness. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
94
95 (quoterust-analyzer cargo quote 1.0.47 quote!! {
96 #(#attrs)*
97 #vis #sig {
98 <#system>::new().block_on(async move { #body })
99 }
100 })
101 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
102 }
103
104 /// Marks async test function to be executed in an Actix system.
105 #[proc_macro_attribute]
106 pub fn testrust-analyzer cargo actix-macros 0.2.4 test:(args: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, item: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
107 let mut input = match synrust-analyzer cargo syn 2.0.119 crate/::parserust-analyzer cargo syn 2.0.119 parse().::<synrust-analyzer cargo syn 2.0.119 crate/::ItemFnrust-analyzer cargo syn 2.0.119 item/ItemFn#>(item.clonerust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro impl#[TokenStream][Clone]clone().()) {
108 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(input) => input,
109 // on parse err, make IDEs happy; see fn docs
110 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => return input_and_compile_errorrust-analyzer cargo actix-macros 0.2.4 input_and_compile_error().(item, err),
111 };
112
113 let parser = AttributeArgsrust-analyzer cargo actix-macros 0.2.4 AttributeArgs#::parse_terminatedrust-analyzer cargo syn 2.0.119 punctuated/impl#[`Punctuated<T, P>`]parse_terminated().;
114 let args = match parser.parserust-analyzer cargo syn 2.0.119 parse/Parser#parse().(args.clonerust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro impl#[TokenStream][Clone]clone().()) {
115 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(args) => args,
116 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => return input_and_compile_errorrust-analyzer cargo actix-macros 0.2.4 input_and_compile_error().(args, err),
117 };
118
119 let attrs = &input.attrsrust-analyzer cargo syn 2.0.119 item/ItemFn#attrs.;
120 let vis = &input.visrust-analyzer cargo syn 2.0.119 item/ItemFn#vis.;
121 let sig = &mut input.sigrust-analyzer cargo syn 2.0.119 item/ItemFn#sig.;
122 let body = &input.blockrust-analyzer cargo syn 2.0.119 item/ItemFn#block.;
123 let mut has_test_attr = false;
124
125 for attr in attrs {
126 if attr.pathrust-analyzer cargo syn 2.0.119 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.119 path/impl#[Path]is_ident().("test") {
127 has_test_attr = true;
128 }
129 }
130
131 if sig.asyncnessrust-analyzer cargo syn 2.0.119 item/Signature#asyncness..is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
132 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(
133 input.sigrust-analyzer cargo syn 2.0.119 item/ItemFn#sig..fn_tokenrust-analyzer cargo syn 2.0.119 item/Signature#fn_token.,
134 "the async keyword is missing from the function declaration",
135 )
136 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
137 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
138 }
139
140 sig.asyncnessrust-analyzer cargo syn 2.0.119 item/Signature#asyncness. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
141
142 let missing_test_attr = if has_test_attr {
143 quoterust-analyzer cargo quote 1.0.47 quote!! {}
144 } else {
145 quoterust-analyzer cargo quote 1.0.47 quote!! { #[::core::prelude::v1::test] }
146 };
147
148 let mut system = synrust-analyzer cargo syn 2.0.119 crate/::parse_strrust-analyzer cargo syn 2.0.119 parse_str().::<synrust-analyzer cargo syn 2.0.119 crate/::Pathrust-analyzer cargo syn 2.0.119 path/Path#>("::actix_rt::System").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
149
150 for arg in &args {
151 match arg {
152 synrust-analyzer cargo syn 2.0.119 crate/::Metarust-analyzer cargo syn 2.0.119 attr/Meta#::NameValuerust-analyzer cargo syn 2.0.119 attr/Meta#NameValue#(synrust-analyzer cargo syn 2.0.119 crate/::MetaNameValuerust-analyzer cargo syn 2.0.119 attr/MetaNameValue# {
153 pathrust-analyzer cargo syn 2.0.119 attr/MetaNameValue#path.,
154 valuerust-analyzer cargo syn 2.0.119 attr/MetaNameValue#value.:
155 synrust-analyzer cargo syn 2.0.119 crate/::Exprrust-analyzer cargo syn 2.0.119 expr/Expr#::Litrust-analyzer cargo syn 2.0.119 expr/Expr#Lit#(synrust-analyzer cargo syn 2.0.119 crate/::ExprLitrust-analyzer cargo syn 2.0.119 expr/ExprLit# {
156 litrust-analyzer cargo syn 2.0.119 expr/ExprLit#lit.: synrust-analyzer cargo syn 2.0.119 crate/::Litrust-analyzer cargo syn 2.0.119 lit/Lit#::Strrust-analyzer cargo syn 2.0.119 lit/Lit#Str#(lit),
157 ..
158 }),
159 ..
160 }) => match path
161 .get_identrust-analyzer cargo syn 2.0.119 path/impl#[Path]get_ident().()
162 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|i| i.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().to_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_lowercase().())
163 .as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
164 {
165 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("system") => match lit.parserust-analyzer cargo syn 2.0.119 lit/impl#[LitStr]parse().() {
166 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path) => system = path,
167 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
168 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(lit, "Expected path")
169 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
170 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
171 }
172 },
173 _ => {
174 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(arg, "Unknown attribute specified")
175 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
176 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
177 }
178 },
179 _ => {
180 return synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#::new_spannedrust-analyzer cargo syn 2.0.119 error/impl#[Error]new_spanned().(arg, "Unknown attribute specified")
181 .to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().()
182 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
183 }
184 }
185 }
186
187 (quoterust-analyzer cargo quote 1.0.47 quote!! {
188 #missing_test_attr
189 #(#attrs)*
190 #vis #sig {
191 <#system>::new().block_on(async { #body })
192 }
193 })
194 .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
195 }
196
197 /// Converts the error to a token stream and appends it to the original input.
198 ///
199 /// Returning the original input in addition to the error is good for IDEs which can gracefully
200 /// recover and show more precise errors within the macro body.
201 ///
202 /// See <https://github.com/rust-analyzer/rust-analyzer/issues/10468> for more info.
203 fn input_and_compile_errorrust-analyzer cargo actix-macros 0.2.4 input_and_compile_error().(mut item: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#, err: synrust-analyzer cargo syn 2.0.119 crate/::Errorrust-analyzer cargo syn 2.0.119 error/Error#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
204 let compile_err = TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#::fromrust-analyzer cargo proc-macro2 1.0.107 impl#[`proc_macro::TokenStream`][`From<TokenStream>`]from().(err.to_compile_errorrust-analyzer cargo syn 2.0.119 error/impl#[Error]to_compile_error().());
205 item.extendrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro impl#[TokenStream][`Extend<TokenTree>`]extend().(compile_err);
206 item
207 }