Skip to content

Commit b70c647

Browse files
ehusstraviscross
authored andcommitted
Add proc_macro_derive attribute template rules
1 parent eb579f6 commit b70c647

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/procedural-macros.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ r[macro.proc.derive]
136136
r[macro.proc.derive.intro]
137137
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].
138138

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`.
141139
> [!EXAMPLE]
142140
> The following is an example of a derive macro. Instead of doing anything useful with its input, it just appends a function `answer`.
143141
>
@@ -173,13 +171,26 @@ The `proc_macro_derive` attribute defines the custom derive in the [macro namesp
173171
174172
r[macro.proc.derive.output]
175173
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:
176176
177+
```grammar,attributes
178+
@root ProcMacroDeriveAttribute ->
179+
`proc_macro_derive` `(` DeriveMacroName ( `,` DeriveMacroAttributes )? `,`? `)`
177180
181+
DeriveMacroName -> SimplePathSegment
178182
183+
DeriveMacroAttributes ->
184+
`attributes` `(` ( SimplePathSegment (`,` SimplePathSegment)* `,`?)? `)`
179185
```
180186
187+
The [DeriveMacroName] is the name of the derive macro. The optional `attributes` are described in [macro.proc.derive.attributes].
181188

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.
182191

192+
r[macro.proc.derive.duplicates]
193+
The `proc_macro_derive` attribute may only be specified once on a function.
183194

184195

185196
r[macro.proc.derive.attributes]

0 commit comments

Comments
 (0)