@@ -98,12 +98,18 @@ macro_rules! newtype_index {
98
98
@max [ $max: expr]
99
99
@vis [ $v: vis]
100
100
@debug_format [ $debug_format: tt] ) => (
101
- #[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord , $( $derives) ,* ) ]
101
+ #[ derive( Copy , PartialEq , Eq , Hash , PartialOrd , Ord , $( $derives) ,* ) ]
102
102
#[ rustc_layout_scalar_valid_range_end( $max) ]
103
103
$v struct $type {
104
104
private: u32
105
105
}
106
106
107
+ impl Clone for $type {
108
+ fn clone( & self ) -> Self {
109
+ * self
110
+ }
111
+ }
112
+
107
113
impl $type {
108
114
$v const MAX_AS_U32 : u32 = $max;
109
115
@@ -145,7 +151,7 @@ macro_rules! newtype_index {
145
151
146
152
#[ inline]
147
153
$v const unsafe fn from_u32_unchecked( value: u32 ) -> Self {
148
- $type { private: value }
154
+ unsafe { $type { private: value } }
149
155
}
150
156
151
157
/// Extract value of this index as an integer.
@@ -328,12 +334,13 @@ macro_rules! newtype_index {
328
334
derive [ $( $derives: ident, ) +]
329
335
$( $tokens: tt) * ) => (
330
336
newtype_index!(
331
- @derives [ $( $derives, ) + RustcDecodable , RustcEncodable , ]
337
+ @derives [ $( $derives, ) + RustcEncodable , ]
332
338
@type [ $type]
333
339
@max [ $max]
334
340
@vis [ $v]
335
341
@debug_format [ $debug_format]
336
342
$( $tokens) * ) ;
343
+ newtype_index!( @decodable $type) ;
337
344
) ;
338
345
339
346
// The case where no derives are added, but encodable is overridden. Don't
@@ -360,12 +367,29 @@ macro_rules! newtype_index {
360
367
@debug_format [ $debug_format: tt]
361
368
$( $tokens: tt) * ) => (
362
369
newtype_index!(
363
- @derives [ RustcDecodable , RustcEncodable , ]
370
+ @derives [ RustcEncodable , ]
364
371
@type [ $type]
365
372
@max [ $max]
366
373
@vis [ $v]
367
374
@debug_format [ $debug_format]
368
375
$( $tokens) * ) ;
376
+ newtype_index!( @decodable $type) ;
377
+ ) ;
378
+
379
+ ( @decodable $type: ident) => (
380
+ impl $type {
381
+ fn __decodable__impl__hack( ) {
382
+ mod __more_hacks_because__self_doesnt_work_in_functions {
383
+ extern crate serialize;
384
+ use self :: serialize:: { Decodable , Decoder } ;
385
+ impl Decodable for super :: $type {
386
+ fn decode<D : Decoder >( d: & mut D ) -> Result <Self , D :: Error > {
387
+ d. read_u32( ) . map( Self :: from)
388
+ }
389
+ }
390
+ }
391
+ }
392
+ }
369
393
) ;
370
394
371
395
// Rewrite final without comma to one that includes comma
0 commit comments