@@ -69,14 +69,15 @@ other functions (like `__internal_foo` instead of `foo`).
69
69
70
70
# ## Function-like procedural macros
71
71
72
- Function-like procedural macros define new invokable macros.
72
+ *Function-like procedural macros* are procedural macros that are invoked using
73
+ the macro invocation operator (`!`).
73
74
74
- These macros are defined by a [public] [function] with the `proc_macro`
75
+ These macros are defined by a [public]  [function] with the `proc_macro`
75
76
[attribute ] and a signature of `(TokenStream) -> TokenStream`. The input
76
77
[`TokenStream` ] is what is inside the delimiters of the macro invocation and the
77
78
output [`TokenStream`] replaces the entire macro invocation. It may contain an
78
- arbitrary number of [items]. The returned [`TokenStream`] cannot include any
79
- [ macro ] definitions .
79
+ arbitrary number of [items]. These macros cannot expand to syntax that defines
80
+ new `macro_rule` style macros .
80
81
81
82
For example, the following macro definition ignores its input and outputs a
82
83
function `answer` into its scope.
@@ -105,20 +106,18 @@ fn main() {
105
106
```
106
107
107
108
These macros are only invokable in [ modules] . They cannot even be invoked to
108
- make [ item declaration statements] . Furthermore, they must either be invoked
109
+ create [ item declaration statements] . Furthermore, they must either be invoked
109
110
with curly braces and no semicolon or a different delimiter followed by a
110
111
semicolon. For example, ` make_answer ` from the previous example can be invoked
111
112
as ` make_answer!{} ` , ` make_answer!(); ` or ` make_answer![]; ` .
112
113
113
- These macros cannot expand to syntax that defines new ` macro_rule ` style macros.
114
-
115
114
### Derive mode macros
116
115
117
- * Derive mode macros* define new modes for the ` derive ` attribute. These macros
118
- define new items given the token stream of a [ struct] , [ enum] , or [ union] . They
119
- also define derive mode helper attributes.
116
+ * Derive mode macros* define new modes for the ` derive ` [ attribute] . These macros
117
+ define new [ items] given the token stream of a [ struct] , [ enum] , or [ union] .
118
+ They also define [ derive mode helper attributes] .
120
119
121
- Custom deriver modes are defined by a [ public] [ function] with the
120
+ Custom deriver modes are defined by a [ public] &# 32 ; [ function] with the
122
121
` proc_macro_derive ` attribute and a signature of ` (TokenStream) -> TokenStream ` .
123
122
124
123
The input [ ` TokenStream ` ] is the token stream of the item that has the ` derive `
@@ -155,9 +154,9 @@ fn main() {
155
154
156
155
#### Derive mode helper attributes
157
156
158
- Derive mode macros can add additional [ attributes] into the scope of the item
157
+ Derive mode macros can add additional [ attributes] into the scope of the [ item]
159
158
they are on. Said attributes are called * derive mode helper attributes* . These
160
- attributes are inert, and their only purpose is to be fed into the derive
159
+ attributes are [ inert] , and their only purpose is to be fed into the derive
161
160
mode macro that defined them. That said, they can be seen by all macros.
162
161
163
162
The way to define helper attributes is to put an ` attributes ` key in the
@@ -167,7 +166,8 @@ the names of the helper attributes.
167
166
For example, the following derive mode macro defines a helper attribute
168
167
` helper ` , but ultimately doesn't do anything with it.
169
168
170
- ``` rust, ignore
169
+ ``` rust,ignore
170
+ # #[crate_type="proc-macro"]
171
171
# extern crate proc_macro;
172
172
# use proc_macro::TokenStream;
173
173
@@ -179,7 +179,8 @@ pub fn derive_helper_attr(_item: TokenStream) -> TokenStream {
179
179
180
180
And then usage on the derive mode on a struct:
181
181
182
- ```
182
+ ``` rust,ignore
183
+ # #![crate_type="proc-macro"]
183
184
# extern crate proc_macro_examples;
184
185
# use proc_macro_examples::HelperAttr;
185
186
@@ -193,24 +194,23 @@ struct Struct {
193
194
194
195
* Attribute macros* define new [ attributes] which can be attached to [ items] .
195
196
196
- Attribute macros are defined by a [ public] [ function] with the
197
- ` proc_macro_attribute ` attribute that a signature of `(TokenStream, TokenStream)
198
- -> TokenStream` . The first [ ` TokenStream`] is the attribute's metaitems, not
199
- including the delimiters. If the attribute is written without a metaitem, the
200
- attribute [ ` TokenStream ` ] is empty. The second [ ` TokenStream ` ] is of the rest of
201
- the item including other attributes on the item. The returned [ ` TokenStream ` ]
202
- replaces the item. It may contain an arbitrary number of items. The returned
203
- [ ` TokenStream ` ] cannot include any [ macro] definitions.
197
+ Attribute macros are defined by a [ public]   ; [ function] with the
198
+ ` proc_macro_attribute ` [ attribute] that a signature of
199
+ ` (TokenStream, TokenStream) -> TokenStream ` . The first [ ` TokenStream ` ] is the
200
+ attribute's metaitems, not including the delimiters. If the attribute is written
201
+ without a metaitem, the attribute [ ` TokenStream ` ] is empty. The second
202
+ [ ` TokenStream ` ] is of the rest of the [ item] including other [ attributes] on the
203
+ [ item] . The returned [ ` TokenStream ` ] replaces the [ item] with an arbitrary
204
+ number of [ items] . These macros cannot expand to syntax that defines new
205
+ ` macro_rule ` style macros.
204
206
205
207
For example, this attribute macro takes the input stream and returns it as is,
206
208
effectively being the no-op of attributes.
207
209
208
- ``` rust
209
- #![crate_type = " proc_macro" ]
210
-
211
- extern crate proc_macro;
212
-
213
- use proc_macro :: TokenStream ;
210
+ ``` rust,ignore
211
+ # #![crate_type = "proc-macro"]
212
+ # extern crate proc_macro;
213
+ # use proc_macro::TokenStream;
214
214
215
215
#[proc_macro_attribute]
216
216
pub fn return_as_is(_attr: TokenStream, item: TokenStream) -> TokenStream {
@@ -275,14 +275,21 @@ fn invoke4() {}
275
275
[ Derive mode macros ] : #derive-mode-macros
276
276
[ Attribute macros ] : #attribute-macros
277
277
[ Function-like macros ] : #function-like-procedural-macros
278
+ [ attribute ] : attributes.html
278
279
[ attributes ] : attributes.html
279
280
[ custom attributes ] : attributes.html
280
281
[ crate type ] : linkage.html
282
+ [ derive mode helper attributes ] : #derive-mode-helper-attributes
283
+ [ enum ] : items/enumerations.html
284
+ [ inert ] : attributes.html#active-and-inert-attributes
281
285
[ item ] : items.html
282
286
[ item declaration statements ] : statements.html#item-declarations
287
+ [ items ] : items.html
283
288
[ function ] : items/functions.html
284
289
[ macro ] : macros.html
285
290
[ module ] : items/modules.html
286
291
[ modules ] : items/modules.html
287
292
[ procedural macro tutorial ] : ../book/2018-edition/appendix-04-macros.html#procedural-macros-for-custom-derive
288
293
[ public ] : visibility-and-privacy.html
294
+ [ struct ] : items/structs.html
295
+ [ unions ] : items/unions.html
0 commit comments