Skip to content

Commit eb579f6

Browse files
ehusstraviscross
authored andcommitted
Move examples into example blocks
1 parent 0163ef7 commit eb579f6

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

src/procedural-macros.md

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -138,41 +138,49 @@ The *`proc_macro_derive` [attribute][attributes]* defines a *derive macro* which
138138

139139
r[macro.proc.derive.def]
140140
Custom derive macros are defined by a [public] [function] with the `proc_macro_derive` attribute and a signature of `(TokenStream) -> TokenStream`.
141+
> [!EXAMPLE]
142+
> The following is an example of a derive macro. Instead of doing anything useful with its input, it just appends a function `answer`.
143+
>
144+
> <!-- ignore: test doesn't support proc-macro -->
145+
> ```rust,ignore
146+
> # #![crate_type = "proc-macro"]
147+
> extern crate proc_macro;
148+
> use proc_macro::TokenStream;
149+
>
150+
> #[proc_macro_derive(AnswerFn)]
151+
> pub fn derive_answer_fn(_item: TokenStream) -> TokenStream {
152+
> "fn answer() -> u32 { 42 }".parse().unwrap()
153+
> }
154+
> ```
155+
>
156+
> And then using said derive macro:
157+
>
158+
> <!-- ignore: requires external crates -->
159+
> ```rust,ignore
160+
> extern crate proc_macro_examples;
161+
> use proc_macro_examples::AnswerFn;
162+
>
163+
> #[derive(AnswerFn)]
164+
> struct Struct;
165+
>
166+
> fn main() {
167+
> assert_eq!(42, answer());
168+
> }
169+
> ```
141170
142171
r[macro.proc.derive.namespace]
143172
The `proc_macro_derive` attribute defines the custom derive in the [macro namespace] in the root of the crate.
144173
145174
r[macro.proc.derive.output]
146175
The input [`TokenStream`] is the token stream of the item that has the `derive` attribute on it. The output [`TokenStream`] must be a set of items that are then appended to the [module] or [block] that the item from the input [`TokenStream`] is in.
147176
148-
The following is an example of a derive macro. Instead of doing anything useful with its input, it just appends a function `answer`.
149177
150-
<!-- ignore: test doesn't support proc-macro -->
151-
```rust,ignore
152-
# #![crate_type = "proc-macro"]
153-
extern crate proc_macro;
154-
use proc_macro::TokenStream;
155178
156-
#[proc_macro_derive(AnswerFn)]
157-
pub fn derive_answer_fn(_item: TokenStream) -> TokenStream {
158-
"fn answer() -> u32 { 42 }".parse().unwrap()
159-
}
160179
```
161180
162-
And then using said derive macro:
163181

164-
<!-- ignore: requires external crates -->
165-
```rust,ignore
166-
extern crate proc_macro_examples;
167-
use proc_macro_examples::AnswerFn;
168182

169-
#[derive(AnswerFn)]
170-
struct Struct;
171183

172-
fn main() {
173-
assert_eq!(42, answer());
174-
}
175-
```
176184

177185
r[macro.proc.derive.attributes]
178186
### Derive macro helper attributes
@@ -183,29 +191,30 @@ Derive macros can add additional [attributes] into the scope of the [item] they
183191
r[macro.proc.derive.attributes.def]
184192
The way to define helper attributes is to put an `attributes` key in the `proc_macro_derive` macro with a comma separated list of identifiers that are the names of the helper attributes.
185193

186-
For example, the following derive macro defines a helper attribute `helper`, but ultimately doesn't do anything with it.
187-
188-
<!-- ignore: test doesn't support proc-macro -->
189-
```rust,ignore
190-
# #![crate_type="proc-macro"]
191-
# extern crate proc_macro;
192-
# use proc_macro::TokenStream;
193-
194-
#[proc_macro_derive(HelperAttr, attributes(helper))]
195-
pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
196-
TokenStream::new()
197-
}
198-
```
199-
200-
And then usage on the derive macro on a struct:
201-
202-
<!-- ignore: requires external crates -->
203-
```rust,ignore
204-
#[derive(HelperAttr)]
205-
struct Struct {
206-
#[helper] field: ()
207-
}
208-
```
194+
> [!EXAMPLE]
195+
> The following derive macro defines a helper attribute `helper`, but ultimately doesn't do anything with it.
196+
>
197+
> <!-- ignore: test doesn't support proc-macro -->
198+
> ```rust,ignore
199+
> # #![crate_type="proc-macro"]
200+
> # extern crate proc_macro;
201+
> # use proc_macro::TokenStream;
202+
>
203+
> #[proc_macro_derive(HelperAttr, attributes(helper))]
204+
> pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
205+
> TokenStream::new()
206+
> }
207+
> ```
208+
>
209+
> And then usage on the derive macro on a struct:
210+
>
211+
> <!-- ignore: requires external crates -->
212+
> ```rust,ignore
213+
> #[derive(HelperAttr)]
214+
> struct Struct {
215+
> #[helper] field: ()
216+
> }
217+
> ```
209218
210219
r[macro.proc.attribute]
211220
## Attribute macros

0 commit comments

Comments
 (0)