@@ -47,16 +47,6 @@ macro_rules! newtype_index {
4747 newtype_index!(
4848 // Leave out derives marker so we can use its absence to ensure it comes first
4949 @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 [ ]
6050 @max [ :: std:: u32 :: MAX ]
6151 @debug_format [ "{}" ] ) ;
6252 ) ;
@@ -66,29 +56,17 @@ macro_rules! newtype_index {
6656 newtype_index!(
6757 // Leave out derives marker so we can use its absence to ensure it comes first
6858 @type [ $name]
69- @pub [ pub ]
7059 @max [ :: std:: u32 :: MAX ]
7160 @debug_format [ "{}" ]
7261 $( $tokens) +) ;
7362 ) ;
7463
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-
8664 // ---- private rules ----
8765
8866 // Base case, user-defined constants (if any) have already been defined
8967 ( @derives [ $( $derives: ident, ) * ]
90- @type [ $type: ident]
9168 @pub [ $( $pub: tt) * ]
69+ @type [ $type: ident]
9270 @max [ $max: expr]
9371 @debug_format [ $debug_format: expr] ) => (
9472 #[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord , $( $derives) ,* ) ]
@@ -148,16 +126,43 @@ macro_rules! newtype_index {
148126 @debug_format [ $debug_format] ) ;
149127 ) ;
150128
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
152144 ( @type [ $type: ident]
153- @pub [ $( $pub: tt) * ]
154145 @max [ $max: expr]
155146 @debug_format [ $debug_format: expr]
156- derive [ $( $derives: ident) ,* ]
157147 $( $tokens: tt) * ) => (
158148 newtype_index!(
149+ @pub [ ]
159150 @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!(
160164 @pub [ $( $pub) * ]
165+ @type [ $type]
161166 @max [ $max]
162167 @debug_format [ $debug_format]
163168 derive [ $( $derives, ) * ]
@@ -166,142 +171,142 @@ macro_rules! newtype_index {
166171
167172 // By not including the @derives marker in this list nor in the default args, we can force it
168173 // 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 ]
171176 @max [ $max: expr]
172177 @debug_format [ $debug_format: expr]
173178 derive [ $( $derives: ident, ) +]
174179 ENCODABLE = custom
175180 $( $tokens: tt) * ) => (
176181 newtype_index!(
177182 @derives [ $( $derives, ) +]
178- @type [ $type]
179183 @pub [ $( $pub) * ]
184+ @type [ $type]
180185 @max [ $max]
181186 @debug_format [ $debug_format]
182187 $( $tokens) * ) ;
183188 ) ;
184189
185190 // By not including the @derives marker in this list nor in the default args, we can force it
186191 // 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 ]
189194 @max [ $max: expr]
190195 @debug_format [ $debug_format: expr]
191196 derive [ $( $derives: ident, ) +]
192197 $( $tokens: tt) * ) => (
193198 newtype_index!(
194199 @derives [ $( $derives, ) + RustcDecodable , RustcEncodable , ]
195- @type [ $type]
196200 @pub [ $( $pub) * ]
201+ @type [ $type]
197202 @max [ $max]
198203 @debug_format [ $debug_format]
199204 $( $tokens) * ) ;
200205 ) ;
201206
202207 // The case where no derives are added, but encodable is overriden. Don't
203208 // derive serialization traits
204- ( @type [ $type : ident ]
205- @pub [ $ ( $pub : tt ) * ]
209+ ( @pub [ $ ( $pub : tt ) * ]
210+ @type [ $type : ident ]
206211 @max [ $max: expr]
207212 @debug_format [ $debug_format: expr]
208213 ENCODABLE = custom
209214 $( $tokens: tt) * ) => (
210215 newtype_index!(
211216 @derives [ ]
212- @type [ $type]
213217 @pub [ $( $pub) * ]
218+ @type [ $type]
214219 @max [ $max]
215220 @debug_format [ $debug_format]
216221 $( $tokens) * ) ;
217222 ) ;
218223
219224 // 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 ]
222227 @max [ $max: expr]
223228 @debug_format [ $debug_format: expr]
224229 $( $tokens: tt) * ) => (
225230 newtype_index!(
226231 @derives [ RustcDecodable , RustcEncodable , ]
227- @type [ $type]
228232 @pub [ $( $pub) * ]
233+ @type [ $type]
229234 @max [ $max]
230235 @debug_format [ $debug_format]
231236 $( $tokens) * ) ;
232237 ) ;
233238
234239 // Rewrite final without comma to one that includes comma
235240 ( @derives [ $( $derives: ident, ) * ]
236- @type [ $type: ident]
237241 @pub [ $( $pub: tt) * ]
242+ @type [ $type: ident]
238243 @max [ $max: expr]
239244 @debug_format [ $debug_format: expr]
240245 $name: ident = $constant: expr) => (
241246 newtype_index!(
242247 @derives [ $( $derives, ) * ]
243- @type [ $type]
244248 @pub [ $( $pub) * ]
249+ @type [ $type]
245250 @max [ $max]
246251 @debug_format [ $debug_format]
247252 $name = $constant, ) ;
248253 ) ;
249254
250255 // Rewrite final const without comma to one that includes comma
251256 ( @derives [ $( $derives: ident, ) * ]
252- @type [ $type: ident]
253257 @pub [ $( $pub: tt) * ]
258+ @type [ $type: ident]
254259 @max [ $_max: expr]
255260 @debug_format [ $debug_format: expr]
256261 $( #[ doc = $doc: expr] ) *
257262 const $name: ident = $constant: expr) => (
258263 newtype_index!(
259264 @derives [ $( $derives, ) * ]
260- @type [ $type]
261265 @pub [ $( $pub) * ]
266+ @type [ $type]
262267 @max [ $max]
263268 @debug_format [ $debug_format]
264269 $( #[ doc = $doc] ) * const $name = $constant, ) ;
265270 ) ;
266271
267272 // Replace existing default for max
268273 ( @derives [ $( $derives: ident, ) * ]
269- @type [ $type: ident]
270274 @pub [ $( $pub: tt) * ]
275+ @type [ $type: ident]
271276 @max [ $_max: expr]
272277 @debug_format [ $debug_format: expr]
273278 MAX = $max: expr,
274279 $( $tokens: tt) * ) => (
275280 newtype_index!(
276281 @derives [ $( $derives, ) * ]
277- @type [ $type]
278282 @pub [ $( $pub) * ]
283+ @type [ $type]
279284 @max [ $max]
280285 @debug_format [ $debug_format]
281286 $( $tokens) * ) ;
282287 ) ;
283288
284289 // Replace existing default for debug_format
285290 ( @derives [ $( $derives: ident, ) * ]
286- @type [ $type: ident]
287291 @pub [ $( $pub: tt) * ]
292+ @type [ $type: ident]
288293 @max [ $max: expr]
289294 @debug_format [ $_debug_format: expr]
290295 DEBUG_FORMAT = $debug_format: expr,
291296 $( $tokens: tt) * ) => (
292297 newtype_index!(
293298 @derives [ $( $derives, ) * ]
294- @type [ $type]
295299 @pub [ $( $pub) * ]
300+ @type [ $type]
296301 @max [ $max]
297302 @debug_format [ $debug_format]
298303 $( $tokens) * ) ;
299304 ) ;
300305
301306 // Assign a user-defined constant
302307 ( @derives [ $( $derives: ident, ) * ]
303- @type [ $type: ident]
304308 @pub [ $( $pub: tt) * ]
309+ @type [ $type: ident]
305310 @max [ $max: expr]
306311 @debug_format [ $debug_format: expr]
307312 $( #[ doc = $doc: expr] ) *
@@ -311,8 +316,8 @@ macro_rules! newtype_index {
311316 pub const $name: $type = $type( $constant) ;
312317 newtype_index!(
313318 @derives [ $( $derives, ) * ]
314- @type [ $type]
315319 @pub [ $( $pub) * ]
320+ @type [ $type]
316321 @max [ $max]
317322 @debug_format [ $debug_format]
318323 $( $tokens) * ) ;
0 commit comments