Skip to content

Commit 37f8e90

Browse files
committed
Move proc_macro_attribute example to an example block
1 parent 8c1250f commit 37f8e90

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
@@ -228,71 +228,78 @@ r[macro.proc.attribute.intro]
228228

229229
r[macro.proc.attribute.def]
230230
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].
231+
> [!EXAMPLE]
232+
> The following attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
233+
>
234+
> <!-- ignore: test doesn't support proc-macro -->
235+
> ```rust,ignore
236+
> # #![crate_type = "proc-macro"]
237+
> # extern crate proc_macro;
238+
> # use proc_macro::TokenStream;
239+
>
240+
> #[proc_macro_attribute]
241+
> pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
242+
> item
243+
> }
244+
> ```
231245
232246
r[macro.proc.attribute.namespace]
233247
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate.
248+
> [!EXAMPLE]
249+
> 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:".
250+
>
251+
> <!-- ignore: test doesn't support proc-macro -->
252+
> ```rust,ignore
253+
> // my-macro/src/lib.rs
254+
> # extern crate proc_macro;
255+
> # use proc_macro::TokenStream;
256+
>
257+
> #[proc_macro_attribute]
258+
> pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream {
259+
> println!("attr: \"{attr}\"");
260+
> println!("item: \"{item}\"");
261+
> item
262+
> }
263+
> ```
264+
>
265+
> <!-- ignore: requires external crates -->
266+
> ```rust,ignore
267+
> // src/lib.rs
268+
> extern crate my_macro;
269+
>
270+
> use my_macro::show_streams;
271+
>
272+
> // Example: Basic function
273+
> #[show_streams]
274+
> fn invoke1() {}
275+
> // out: attr: ""
276+
> // out: item: "fn invoke1() {}"
277+
>
278+
> // Example: Attribute with input
279+
> #[show_streams(bar)]
280+
> fn invoke2() {}
281+
> // out: attr: "bar"
282+
> // out: item: "fn invoke2() {}"
283+
>
284+
> // Example: Multiple tokens in the input
285+
> #[show_streams(multiple => tokens)]
286+
> fn invoke3() {}
287+
> // out: attr: "multiple => tokens"
288+
> // out: item: "fn invoke3() {}"
289+
>
290+
> // Example:
291+
> #[show_streams { delimiters }]
292+
> fn invoke4() {}
293+
> // out: attr: "delimiters"
294+
> // out: item: "fn invoke4() {}"
295+
> ```
234296
235-
For example, this attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
236297
237-
<!-- ignore: test doesn't support proc-macro -->
238-
```rust,ignore
239-
# #![crate_type = "proc-macro"]
240-
# extern crate proc_macro;
241-
# use proc_macro::TokenStream;
242298
243-
#[proc_macro_attribute]
244-
pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
245-
item
246-
}
247-
```
248299
249-
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:".
250300
251-
<!-- ignore: test doesn't support proc-macro -->
252-
```rust,ignore
253-
// my-macro/src/lib.rs
254-
# extern crate proc_macro;
255-
# use proc_macro::TokenStream;
256301
257-
#[proc_macro_attribute]
258-
pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream {
259-
println!("attr: \"{attr}\"");
260-
println!("item: \"{item}\"");
261-
item
262-
}
263-
```
264302
265-
<!-- ignore: requires external crates -->
266-
```rust,ignore
267-
// src/lib.rs
268-
extern crate my_macro;
269-
270-
use my_macro::show_streams;
271-
272-
// Example: Basic function
273-
#[show_streams]
274-
fn invoke1() {}
275-
// out: attr: ""
276-
// out: item: "fn invoke1() {}"
277-
278-
// Example: Attribute with input
279-
#[show_streams(bar)]
280-
fn invoke2() {}
281-
// out: attr: "bar"
282-
// out: item: "fn invoke2() {}"
283-
284-
// Example: Multiple tokens in the input
285-
#[show_streams(multiple => tokens)]
286-
fn invoke3() {}
287-
// out: attr: "multiple => tokens"
288-
// out: item: "fn invoke3() {}"
289-
290-
// Example:
291-
#[show_streams { delimiters }]
292-
fn invoke4() {}
293-
// out: attr: "delimiters"
294-
// out: item: "fn invoke4() {}"
295-
```
296303
297304
r[macro.proc.token]
298305
## Declarative macro tokens and procedural macro tokens

0 commit comments

Comments
 (0)