Skip to content

Commit b38d9e3

Browse files
ehusstraviscross
authored andcommitted
Move proc_macro_attribute example to an example block
1 parent be918f0 commit b38d9e3

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

src/procedural-macros.md

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -236,71 +236,78 @@ r[macro.proc.attribute.intro]
236236
237237
r[macro.proc.attribute.def]
238238
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].
239+
> [!EXAMPLE]
240+
> The following attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
241+
>
242+
> <!-- ignore: test doesn't support proc-macro -->
243+
> ```rust,ignore
244+
> # #![crate_type = "proc-macro"]
245+
> # extern crate proc_macro;
246+
> # use proc_macro::TokenStream;
247+
>
248+
> #[proc_macro_attribute]
249+
> pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
250+
> item
251+
> }
252+
> ```
239253
240254
r[macro.proc.attribute.namespace]
241255
The `proc_macro_attribute` attribute defines the attribute in the [macro namespace] in the root of the crate.
256+
> [!EXAMPLE]
257+
> 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:".
258+
>
259+
> <!-- ignore: test doesn't support proc-macro -->
260+
> ```rust,ignore
261+
> // my-macro/src/lib.rs
262+
> # extern crate proc_macro;
263+
> # use proc_macro::TokenStream;
264+
>
265+
> #[proc_macro_attribute]
266+
> pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream {
267+
> println!("attr: \"{attr}\"");
268+
> println!("item: \"{item}\"");
269+
> item
270+
> }
271+
> ```
272+
>
273+
> <!-- ignore: requires external crates -->
274+
> ```rust,ignore
275+
> // src/lib.rs
276+
> extern crate my_macro;
277+
>
278+
> use my_macro::show_streams;
279+
>
280+
> // Example: Basic function
281+
> #[show_streams]
282+
> fn invoke1() {}
283+
> // out: attr: ""
284+
> // out: item: "fn invoke1() {}"
285+
>
286+
> // Example: Attribute with input
287+
> #[show_streams(bar)]
288+
> fn invoke2() {}
289+
> // out: attr: "bar"
290+
> // out: item: "fn invoke2() {}"
291+
>
292+
> // Example: Multiple tokens in the input
293+
> #[show_streams(multiple => tokens)]
294+
> fn invoke3() {}
295+
> // out: attr: "multiple => tokens"
296+
> // out: item: "fn invoke3() {}"
297+
>
298+
> // Example:
299+
> #[show_streams { delimiters }]
300+
> fn invoke4() {}
301+
> // out: attr: "delimiters"
302+
> // out: item: "fn invoke4() {}"
303+
> ```
304+
242305
243-
For example, this attribute macro takes the input stream and returns it as is, effectively being the no-op of attributes.
244306
245-
<!-- ignore: test doesn't support proc-macro -->
246-
```rust,ignore
247-
# #![crate_type = "proc-macro"]
248-
# extern crate proc_macro;
249-
# use proc_macro::TokenStream;
250307
251-
#[proc_macro_attribute]
252-
pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
253-
item
254-
}
255-
```
256308
257-
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:".
258309
259-
<!-- ignore: test doesn't support proc-macro -->
260-
```rust,ignore
261-
// my-macro/src/lib.rs
262-
# extern crate proc_macro;
263-
# use proc_macro::TokenStream;
264-
265-
#[proc_macro_attribute]
266-
pub fn show_streams(attr: TokenStream, item: TokenStream) -> TokenStream {
267-
println!("attr: \"{attr}\"");
268-
println!("item: \"{item}\"");
269-
item
270-
}
271-
```
272310
273-
<!-- ignore: requires external crates -->
274-
```rust,ignore
275-
// src/lib.rs
276-
extern crate my_macro;
277-
278-
use my_macro::show_streams;
279-
280-
// Example: Basic function
281-
#[show_streams]
282-
fn invoke1() {}
283-
// out: attr: ""
284-
// out: item: "fn invoke1() {}"
285-
286-
// Example: Attribute with input
287-
#[show_streams(bar)]
288-
fn invoke2() {}
289-
// out: attr: "bar"
290-
// out: item: "fn invoke2() {}"
291-
292-
// Example: Multiple tokens in the input
293-
#[show_streams(multiple => tokens)]
294-
fn invoke3() {}
295-
// out: attr: "multiple => tokens"
296-
// out: item: "fn invoke3() {}"
297-
298-
// Example:
299-
#[show_streams { delimiters }]
300-
fn invoke4() {}
301-
// out: attr: "delimiters"
302-
// out: item: "fn invoke4() {}"
303-
```
304311
305312
r[macro.proc.token]
306313
## Declarative macro tokens and procedural macro tokens

0 commit comments

Comments
 (0)