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
+13-2Lines changed: 13 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -136,8 +136,6 @@ r[macro.proc.derive]
136
136
r[macro.proc.derive.intro]
137
137
The *`proc_macro_derive`[attribute][attributes]* defines a *derive macro* which defines an input for the [`derive` attribute]. These macros can create new [items] given the token stream of a [struct], [enum], or [union]. They can also define [derive macro helper attributes].
138
138
139
-
r[macro.proc.derive.def]
140
-
Custom derive macros are defined by a [public] [function] with the `proc_macro_derive` attribute and a signature of `(TokenStream) -> TokenStream`.
141
139
> [!EXAMPLE]
142
140
> The following is an example of a derive macro. Instead of doing anything useful with its input, it just appends a function `answer`.
143
141
>
@@ -173,13 +171,26 @@ The `proc_macro_derive` attribute defines the custom derive in the [macro namesp
173
171
174
172
r[macro.proc.derive.output]
175
173
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.
174
+
r[macro.proc.derive.syntax]
175
+
The `proc_macro_derive` attribute uses the following syntax:
The [DeriveMacroName] is the name of the derive macro. The optional `attributes` are described in [macro.proc.derive.attributes].
181
188
189
+
r[macro.proc.derive.allowed-positions]
190
+
The `proc_macro_derive` attribute may only be applied to a function with the signature of `pub fn(TokenStream) -> TokenStream` where [`TokenStream`] comes from the [`proc_macro` crate]. It must have the ["Rust" ABI][items.fn.extern]. No other function qualifiers are allowed.
182
191
192
+
r[macro.proc.derive.duplicates]
193
+
The `proc_macro_derive` attribute may only be specified once on a function.
0 commit comments