@@ -47,16 +47,6 @@ macro_rules! newtype_index {
47
47
newtype_index!(
48
48
// Leave out derives marker so we can use its absence to ensure it comes first
49
49
@type [ $name]
50
- @pub [ pub ]
51
- @max [ :: std:: u32 :: MAX ]
52
- @debug_format [ "{}" ] ) ;
53
- ) ;
54
-
55
- ( $name: ident nopub) => (
56
- newtype_index!(
57
- // Leave out derives marker so we can use its absence to ensure it comes first
58
- @type [ $name]
59
- @pub [ ]
60
50
@max [ :: std:: u32 :: MAX ]
61
51
@debug_format [ "{}" ] ) ;
62
52
) ;
@@ -66,29 +56,17 @@ macro_rules! newtype_index {
66
56
newtype_index!(
67
57
// Leave out derives marker so we can use its absence to ensure it comes first
68
58
@type [ $name]
69
- @pub [ pub ]
70
59
@max [ :: std:: u32 :: MAX ]
71
60
@debug_format [ "{}" ]
72
61
$( $tokens) +) ;
73
62
) ;
74
63
75
- // Define any constants
76
- ( $name: ident nopub { $( $tokens: tt) + } ) => (
77
- newtype_index!(
78
- // Leave out derives marker so we can use its absence to ensure it comes first
79
- @type [ $name]
80
- @pub [ ]
81
- @max [ :: std:: u32 :: MAX ]
82
- @debug_format [ unsafe { :: std:: intrinsics:: type_name:: <$name>( ) } ]
83
- $( $tokens) +) ;
84
- ) ;
85
-
86
64
// ---- private rules ----
87
65
88
66
// Base case, user-defined constants (if any) have already been defined
89
67
( @derives [ $( $derives: ident, ) * ]
90
- @type [ $type: ident]
91
68
@pub [ $( $pub: tt) * ]
69
+ @type [ $type: ident]
92
70
@max [ $max: expr]
93
71
@debug_format [ $debug_format: expr] ) => (
94
72
#[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord , $( $derives) ,* ) ]
@@ -148,16 +126,43 @@ macro_rules! newtype_index {
148
126
@debug_format [ $debug_format] ) ;
149
127
) ;
150
128
151
- // Append comma to end of derives list if it's missing
129
+ // Handle the case where someone wants to make the internal field public
130
+ ( @type [ $type: ident]
131
+ @max [ $max: expr]
132
+ @debug_format [ $debug_format: expr]
133
+ pub idx
134
+ $( $tokens: tt) * ) => (
135
+ newtype_index!(
136
+ @pub [ pub ]
137
+ @type [ $type]
138
+ @max [ $max]
139
+ @debug_format [ $debug_format]
140
+ $( $tokens) * ) ;
141
+ ) ;
142
+
143
+ // The default case is that the internal field is private
152
144
( @type [ $type: ident]
153
- @pub [ $( $pub: tt) * ]
154
145
@max [ $max: expr]
155
146
@debug_format [ $debug_format: expr]
156
- derive [ $( $derives: ident) ,* ]
157
147
$( $tokens: tt) * ) => (
158
148
newtype_index!(
149
+ @pub [ ]
159
150
@type [ $type]
151
+ @max [ $max]
152
+ @debug_format [ $debug_format]
153
+ $( $tokens) * ) ;
154
+ ) ;
155
+
156
+ // Append comma to end of derives list if it's missing
157
+ ( @pub [ $( $pub: tt) * ]
158
+ @type [ $type: ident]
159
+ @max [ $max: expr]
160
+ @debug_format [ $debug_format: expr]
161
+ derive [ $( $derives: ident) ,* ]
162
+ $( $tokens: tt) * ) => (
163
+ newtype_index!(
160
164
@pub [ $( $pub) * ]
165
+ @type [ $type]
161
166
@max [ $max]
162
167
@debug_format [ $debug_format]
163
168
derive [ $( $derives, ) * ]
@@ -166,142 +171,142 @@ macro_rules! newtype_index {
166
171
167
172
// By not including the @derives marker in this list nor in the default args, we can force it
168
173
// to come first if it exists. When encodable is custom, just use the derives list as-is.
169
- ( @type [ $type : ident ]
170
- @pub [ $ ( $pub : tt ) * ]
174
+ ( @pub [ $ ( $pub : tt ) * ]
175
+ @type [ $type : ident ]
171
176
@max [ $max: expr]
172
177
@debug_format [ $debug_format: expr]
173
178
derive [ $( $derives: ident, ) +]
174
179
ENCODABLE = custom
175
180
$( $tokens: tt) * ) => (
176
181
newtype_index!(
177
182
@derives [ $( $derives, ) +]
178
- @type [ $type]
179
183
@pub [ $( $pub) * ]
184
+ @type [ $type]
180
185
@max [ $max]
181
186
@debug_format [ $debug_format]
182
187
$( $tokens) * ) ;
183
188
) ;
184
189
185
190
// By not including the @derives marker in this list nor in the default args, we can force it
186
191
// to come first if it exists. When encodable isn't custom, add serialization traits by default.
187
- ( @type [ $type : ident ]
188
- @pub [ $ ( $pub : tt ) * ]
192
+ ( @pub [ $ ( $pub : tt ) * ]
193
+ @type [ $type : ident ]
189
194
@max [ $max: expr]
190
195
@debug_format [ $debug_format: expr]
191
196
derive [ $( $derives: ident, ) +]
192
197
$( $tokens: tt) * ) => (
193
198
newtype_index!(
194
199
@derives [ $( $derives, ) + RustcDecodable , RustcEncodable , ]
195
- @type [ $type]
196
200
@pub [ $( $pub) * ]
201
+ @type [ $type]
197
202
@max [ $max]
198
203
@debug_format [ $debug_format]
199
204
$( $tokens) * ) ;
200
205
) ;
201
206
202
207
// The case where no derives are added, but encodable is overriden. Don't
203
208
// derive serialization traits
204
- ( @type [ $type : ident ]
205
- @pub [ $ ( $pub : tt ) * ]
209
+ ( @pub [ $ ( $pub : tt ) * ]
210
+ @type [ $type : ident ]
206
211
@max [ $max: expr]
207
212
@debug_format [ $debug_format: expr]
208
213
ENCODABLE = custom
209
214
$( $tokens: tt) * ) => (
210
215
newtype_index!(
211
216
@derives [ ]
212
- @type [ $type]
213
217
@pub [ $( $pub) * ]
218
+ @type [ $type]
214
219
@max [ $max]
215
220
@debug_format [ $debug_format]
216
221
$( $tokens) * ) ;
217
222
) ;
218
223
219
224
// The case where no derives are added, add serialization derives by default
220
- ( @type [ $type : ident ]
221
- @pub [ $ ( $pub : tt ) * ]
225
+ ( @pub [ $ ( $pub : tt ) * ]
226
+ @type [ $type : ident ]
222
227
@max [ $max: expr]
223
228
@debug_format [ $debug_format: expr]
224
229
$( $tokens: tt) * ) => (
225
230
newtype_index!(
226
231
@derives [ RustcDecodable , RustcEncodable , ]
227
- @type [ $type]
228
232
@pub [ $( $pub) * ]
233
+ @type [ $type]
229
234
@max [ $max]
230
235
@debug_format [ $debug_format]
231
236
$( $tokens) * ) ;
232
237
) ;
233
238
234
239
// Rewrite final without comma to one that includes comma
235
240
( @derives [ $( $derives: ident, ) * ]
236
- @type [ $type: ident]
237
241
@pub [ $( $pub: tt) * ]
242
+ @type [ $type: ident]
238
243
@max [ $max: expr]
239
244
@debug_format [ $debug_format: expr]
240
245
$name: ident = $constant: expr) => (
241
246
newtype_index!(
242
247
@derives [ $( $derives, ) * ]
243
- @type [ $type]
244
248
@pub [ $( $pub) * ]
249
+ @type [ $type]
245
250
@max [ $max]
246
251
@debug_format [ $debug_format]
247
252
$name = $constant, ) ;
248
253
) ;
249
254
250
255
// Rewrite final const without comma to one that includes comma
251
256
( @derives [ $( $derives: ident, ) * ]
252
- @type [ $type: ident]
253
257
@pub [ $( $pub: tt) * ]
258
+ @type [ $type: ident]
254
259
@max [ $_max: expr]
255
260
@debug_format [ $debug_format: expr]
256
261
$( #[ doc = $doc: expr] ) *
257
262
const $name: ident = $constant: expr) => (
258
263
newtype_index!(
259
264
@derives [ $( $derives, ) * ]
260
- @type [ $type]
261
265
@pub [ $( $pub) * ]
266
+ @type [ $type]
262
267
@max [ $max]
263
268
@debug_format [ $debug_format]
264
269
$( #[ doc = $doc] ) * const $name = $constant, ) ;
265
270
) ;
266
271
267
272
// Replace existing default for max
268
273
( @derives [ $( $derives: ident, ) * ]
269
- @type [ $type: ident]
270
274
@pub [ $( $pub: tt) * ]
275
+ @type [ $type: ident]
271
276
@max [ $_max: expr]
272
277
@debug_format [ $debug_format: expr]
273
278
MAX = $max: expr,
274
279
$( $tokens: tt) * ) => (
275
280
newtype_index!(
276
281
@derives [ $( $derives, ) * ]
277
- @type [ $type]
278
282
@pub [ $( $pub) * ]
283
+ @type [ $type]
279
284
@max [ $max]
280
285
@debug_format [ $debug_format]
281
286
$( $tokens) * ) ;
282
287
) ;
283
288
284
289
// Replace existing default for debug_format
285
290
( @derives [ $( $derives: ident, ) * ]
286
- @type [ $type: ident]
287
291
@pub [ $( $pub: tt) * ]
292
+ @type [ $type: ident]
288
293
@max [ $max: expr]
289
294
@debug_format [ $_debug_format: expr]
290
295
DEBUG_FORMAT = $debug_format: expr,
291
296
$( $tokens: tt) * ) => (
292
297
newtype_index!(
293
298
@derives [ $( $derives, ) * ]
294
- @type [ $type]
295
299
@pub [ $( $pub) * ]
300
+ @type [ $type]
296
301
@max [ $max]
297
302
@debug_format [ $debug_format]
298
303
$( $tokens) * ) ;
299
304
) ;
300
305
301
306
// Assign a user-defined constant
302
307
( @derives [ $( $derives: ident, ) * ]
303
- @type [ $type: ident]
304
308
@pub [ $( $pub: tt) * ]
309
+ @type [ $type: ident]
305
310
@max [ $max: expr]
306
311
@debug_format [ $debug_format: expr]
307
312
$( #[ doc = $doc: expr] ) *
@@ -311,8 +316,8 @@ macro_rules! newtype_index {
311
316
pub const $name: $type = $type( $constant) ;
312
317
newtype_index!(
313
318
@derives [ $( $derives, ) * ]
314
- @type [ $type]
315
319
@pub [ $( $pub) * ]
320
+ @type [ $type]
316
321
@max [ $max]
317
322
@debug_format [ $debug_format]
318
323
$( $tokens) * ) ;
0 commit comments