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
+91-70Lines changed: 91 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -131,94 +131,101 @@ expressions], [item] positions, including items in [`extern` blocks], inherent
131
131
and trait [implementations], and [trait definitions].
132
132
133
133
r[macro.proc.derive]
134
-
## Derive macros
134
+
## The `proc_macro_derive` attribute
135
135
136
136
r[macro.proc.derive.intro]
137
-
*Derive macros* define new inputs for the [`derive` attribute]. These macros
138
-
can create new [items] given the token stream of a [struct], [enum], or [union].
139
-
They can also define [derive macro helper attributes].
137
+
Applying the *`proc_macro_derive`[attribute]* to a function defines a *derive macro* that can be invoked by the [`derive` attribute]. These macros are given the token stream of a [struct], [enum], or [union] definition and can emit new [items] after it. They can also declare and use [derive macro helper attributes].
140
138
141
-
r[macro.proc.derive.def]
142
-
Custom derive macros are defined by a [public] [function] with the
143
-
`proc_macro_derive` attribute and a signature of `(TokenStream) -> TokenStream`.
144
-
145
-
r[macro.proc.derive.namespace]
146
-
The `proc_macro_derive` attribute defines the custom derive in the [macro namespace] in the root of the crate.
139
+
> [!EXAMPLE]
140
+
> This derive macro ignores its input and appends tokens that define a function.
141
+
>
142
+
> <!-- ignore: test doesn't support proc-macro -->
The name of the derive macro is given by [DeriveMacroName]. The optional `attributes` argument is described in [macro.proc.derive.attributes].
170
183
171
-
<!-- ignore: requires external crates -->
172
-
```rust,ignore
173
-
extern crate proc_macro_examples;
174
-
use proc_macro_examples::AnswerFn;
184
+
r[macro.proc.derive.allowed-positions]
185
+
The `proc_macro_derive` attribute may only be applied to a `pub` function with the [Rust ABI][items.fn.extern] defined in the root of the crate with a type of `fn(TokenStream) -> TokenStream` where [`TokenStream`] comes from the [`proc_macro` crate]. The function may be `const` and may use `extern` to explicitly specify the Rust ABI, but it may not use any other [qualifiers][FunctionQualifiers] (e.g. it may not be `async` or `unsafe`).
175
186
176
-
#[derive(AnswerFn)]
177
-
struct Struct;
187
+
r[macro.proc.derive.duplicates]
188
+
The `proc_macro_derive` attribute may be specified only once on a function.
178
189
179
-
fn main() {
180
-
assert_eq!(42, answer());
181
-
}
182
-
```
190
+
r[macro.proc.derive.namespace]
191
+
The `proc_macro_derive` attribute publicly defines the derive macro in the [macro namespace] in the root of the crate.
192
+
193
+
r[macro.proc.derive.output]
194
+
The input [`TokenStream`] is the token stream of the item to which the `derive` attribute is applied. The output [`TokenStream`] must be a (possibly empty) set of items. These items are appended following the input item within the same [module] or [block].
183
195
184
196
r[macro.proc.derive.attributes]
185
197
### Derive macro helper attributes
186
198
187
199
r[macro.proc.derive.attributes.intro]
188
-
Derive macros can add additional [attributes] into the scope of the [item]
189
-
they are on. Said attributes are called *derive macro helper attributes*. These
190
-
attributes are [inert], and their only purpose is to be fed into the derive
191
-
macro that defined them. That said, they can be seen by all macros.
192
-
193
-
r[macro.proc.derive.attributes.def]
194
-
The way to define helper attributes is to put an `attributes` key in the
195
-
`proc_macro_derive` macro with a comma separated list of identifiers that are
196
-
the names of the helper attributes.
197
-
198
-
For example, the following derive macro defines a helper attribute
199
-
`helper`, but ultimately doesn't do anything with it.
200
-
201
-
<!-- ignore: test doesn't support proc-macro -->
202
-
```rust,ignore
203
-
# #![crate_type="proc-macro"]
204
-
# extern crate proc_macro;
205
-
# use proc_macro::TokenStream;
200
+
Derive macros can declare *derive macro helper attributes* to be used within the scope of the [item] to which the derive macro is applied. These [attributes] are [inert]. While their purpose is to be used by the macro that declared them, they can be seen by any macro.
0 commit comments