Skip to content

Commit f45633d

Browse files
ehusstraviscross
authored andcommitted
Move proc_macro_attribute example to an example block
1 parent c6d588b commit f45633d

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

src/procedural-macros.md

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -248,71 +248,78 @@ r[macro.proc.attribute.intro]
248248
249249
r[macro.proc.attribute.def]
250250
Attribute macros are defined by a [public] [function] with the `proc_macro_attribute` [attribute] that has a signature of `(TokenStream, TokenStream) -> TokenStream`. The first [`TokenStream`] 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 attribute [`TokenStream`] is empty. The second [`TokenStream`] is the rest of the [item] including other [attributes] on the [item]. The returned [`TokenStream`] replaces the [item] with an arbitrary number of [items].
251+
> [!EXAMPLE]
252+
> The following attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
253+
>
254+
> <!-- ignore: test doesn't support proc-macro -->
255+
> ```rust,ignore
256+
> # #![crate_type = "proc-macro"]
257+
> # extern crate proc_macro;
258+
> # use proc_macro::TokenStream;
259+
>
260+
> #[proc_macro_attribute]
261+
> pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
262+
> item
263+
> }
264+
> ```
251265
252266
r[macro.proc.attribute.namespace]
253267
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate.
268+
> [!EXAMPLE]
269+
> 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:".
270+
>
271+
> <!-- ignore: test doesn't support proc-macro -->
272+
> ```rust,ignore
273+
> // my-macro/src/lib.rs
274+
> # extern crate proc_macro;
275+
> # use proc_macro::TokenStream;
276+
>
277+
> #[proc_macro_attribute]
278+
> pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream {
279+
> println!("attr: \"{attr}\"");
280+
> println!("item: \"{item}\"");
281+
> item
282+
> }
283+
> ```
284+
>
285+
> <!-- ignore: requires external crates -->
286+
> ```rust,ignore
287+
> // src/lib.rs
288+
> extern crate my_macro;
289+
>
290+
> use my_macro::show_streams;
291+
>
292+
> // Example: Basic function
293+
> #[show_streams]
294+
> fn invoke1() {}
295+
> // out: attr: ""
296+
> // out: item: "fn invoke1() {}"
297+
>
298+
> // Example: Attribute with input
299+
> #[show_streams(bar)]
300+
> fn invoke2() {}
301+
> // out: attr: "bar"
302+
> // out: item: "fn invoke2() {}"
303+
>
304+
> // Example: Multiple tokens in the input
305+
> #[show_streams(multiple => tokens)]
306+
> fn invoke3() {}
307+
> // out: attr: "multiple => tokens"
308+
> // out: item: "fn invoke3() {}"
309+
>
310+
> // Example:
311+
> #[show_streams { delimiters }]
312+
> fn invoke4() {}
313+
> // out: attr: "delimiters"
314+
> // out: item: "fn invoke4() {}"
315+
> ```
254316
255-
For example, this attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
256317
257-
<!-- ignore: test doesn't support proc-macro -->
258-
```rust,ignore
259-
# #![crate_type = "proc-macro"]
260-
# extern crate proc_macro;
261-
# use proc_macro::TokenStream;
262318
263-
#[proc_macro_attribute]
264-
pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
265-
item
266-
}
267-
```
268319
269-
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:".
270320
271-
<!-- ignore: test doesn't support proc-macro -->
272-
```rust,ignore
273-
// my-macro/src/lib.rs
274-
# extern crate proc_macro;
275-
# use proc_macro::TokenStream;
276321
277-
#[proc_macro_attribute]
278-
pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream {
279-
println!("attr: \"{attr}\"");
280-
println!("item: \"{item}\"");
281-
item
282-
}
283-
```
284322
285-
<!-- ignore: requires external crates -->
286-
```rust,ignore
287-
// src/lib.rs
288-
extern crate my_macro;
289-
290-
use my_macro::show_streams;
291-
292-
// Example: Basic function
293-
#[show_streams]
294-
fn invoke1() {}
295-
// out: attr: ""
296-
// out: item: "fn invoke1() {}"
297-
298-
// Example: Attribute with input
299-
#[show_streams(bar)]
300-
fn invoke2() {}
301-
// out: attr: "bar"
302-
// out: item: "fn invoke2() {}"
303-
304-
// Example: Multiple tokens in the input
305-
#[show_streams(multiple => tokens)]
306-
fn invoke3() {}
307-
// out: attr: "multiple => tokens"
308-
// out: item: "fn invoke3() {}"
309-
310-
// Example:
311-
#[show_streams { delimiters }]
312-
fn invoke4() {}
313-
// out: attr: "delimiters"
314-
// out: item: "fn invoke4() {}"
315-
```
316323
317324
r[macro.proc.token]
318325
## Declarative macro tokens and procedural macro tokens

0 commit comments

Comments
 (0)