You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/procedural-macros.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,14 +240,15 @@ A helper attribute for a derive macro is declared by adding its identifier to th
240
240
> }
241
241
> ```
242
242
243
+
<!-- template:attributes -->
243
244
r[macro.proc.attribute]
244
245
## The `proc_macro_attribute` attribute
245
246
246
247
r[macro.proc.attribute.intro]
247
248
The *`proc_macro_attribute` [attribute][attributes]* defines an *attribute macro* which can be used as an [outer attribute][attributes].
248
249
249
250
> [!EXAMPLE]
250
-
> The following attribute macro takes the input stream and returns it asis, effectively being the no-op of attributes.
251
+
> This attribute macro takes the input stream and emits it as-is, effectively being a no-op attribute.
251
252
>
252
253
> <!-- ignore: test doesn't support proc-macro -->
253
254
> ```rust,ignore
@@ -262,14 +263,13 @@ The *`proc_macro_attribute` [attribute][attributes]* defines an *attribute macro
262
263
> ```
263
264
264
265
> [!EXAMPLE]
265
-
> This following example shows the stringified [`TokenStream`s] that the attribute macros see. The output will show in the output of the compiler. The output is shown in the comments after the function prefixed with "out:".
266
+
> This shows, in the output of the compiler, the stringified [`TokenStream`s] that attribute macros see.
266
267
>
267
268
> <!-- ignore: test doesn't support proc-macro -->
@@ -285,36 +285,36 @@ The *`proc_macro_attribute` [attribute][attributes]* defines an *attribute macro
285
285
>
286
286
> use my_macro::show_streams;
287
287
>
288
-
> // Example: Basic function
288
+
> // Example: Basic function.
289
289
> #[show_streams]
290
290
> fn invoke1() {}
291
291
> // out: attr: ""
292
292
> // out: item: "fn invoke1() {}"
293
293
>
294
-
> // Example: Attribute with input
294
+
> // Example: Attribute with input.
295
295
> #[show_streams(bar)]
296
296
> fn invoke2() {}
297
297
> // out: attr: "bar"
298
298
> // out: item: "fn invoke2() {}"
299
299
>
300
-
> // Example: Multiple tokens in the input
300
+
> // Example: Multiple tokens in the input.
301
301
> #[show_streams(multiple => tokens)]
302
302
> fn invoke3() {}
303
303
> // out: attr: "multiple => tokens"
304
304
> // out: item: "fn invoke3() {}"
305
305
>
306
-
> // Example:
306
+
> // Example: Delimiters in the input.
307
307
> #[show_streams { delimiters }]
308
308
> fn invoke4() {}
309
309
> // out: attr: "delimiters"
310
310
> // out: item: "fn invoke4() {}"
311
311
> ```
312
312
313
313
r[macro.proc.attribute.syntax]
314
-
The `proc_macro_attribute` attribute uses the [MetaWord] syntax and thus does not take any inputs.
314
+
The `proc_macro_attribute` attribute uses the [MetaWord] syntax.
315
315
316
316
r[macro.proc.attribute.allowed-positions]
317
-
The `proc_macro_attribute` attribute may only be applied to a function with the signature of `pub fn(TokenStream, TokenStream) -> TokenStream` where [`TokenStream`] comes from the [`proc_macro` crate]. It must have the ["Rust" ABI][items.fn.extern]. No other function qualifiers are allowed.
317
+
The `proc_macro_attribute` attribute may only be applied to a `pub` function of type `fn(TokenStream, TokenStream) -> TokenStream` where [`TokenStream`] comes from the [`proc_macro` crate]. It must have the ["Rust" ABI][items.fn.extern]. No other function qualifiers are allowed. It must be located in the root of the crate.
318
318
319
319
r[macro.proc.attribute.duplicates]
320
320
The `proc_macro_attribute` attribute may only be specified once on a function.
@@ -325,17 +325,17 @@ The `proc_macro_attribute` attribute defines the attribute in the [macro namespa
325
325
r[macro.proc.attribute.use-positions]
326
326
Attribute macros can only be used on:
327
327
328
-
- [items]
329
-
- items in [`extern` blocks]
330
-
- inherent and trait [implementations]
331
-
- [trait definitions]
328
+
- [Items]
329
+
- Items in [`extern` blocks]
330
+
- Inherent and trait [implementations]
331
+
- [Trait definitions]
332
332
333
333
r[macro.proc.attribute.behavior]
334
-
The first [`TokenStream`] parameter is the delimited token tree following the attribute's name, not including the outer delimiters. If the attribute is written as a bare attribute name, the [`TokenStream`] is empty.
334
+
The first [`TokenStream`] parameter is the delimited token tree following the attribute's name but not including the outer delimiters. If the applied attribute contains only the attribute name or the attribute name followed by empty delimiters, the [`TokenStream`] is empty.
335
335
336
-
The second [`TokenStream`] is the rest of the [item] including other [attributes] on the [item].
336
+
The second [`TokenStream`] is the rest of the [item], including other [attributes] on the [item].
337
337
338
-
The returned [`TokenStream`] replaces the [item] with an arbitrary number of [items].
338
+
The item to which the attribute is applied is replaced by the zero or more items in the returned [`TokenStream`].
339
339
340
340
r[macro.proc.token]
341
341
## Declarative macro tokens and procedural macro tokens
0 commit comments