@@ -98,12 +98,18 @@ macro_rules! newtype_index {
9898 @max [ $max: expr]
9999 @vis [ $v: vis]
100100 @debug_format [ $debug_format: tt] ) => (
101- #[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord , $( $derives) ,* ) ]
101+ #[ derive( Copy , PartialEq , Eq , Hash , PartialOrd , Ord , $( $derives) ,* ) ]
102102 #[ rustc_layout_scalar_valid_range_end( $max) ]
103103 $v struct $type {
104104 private: u32
105105 }
106106
107+ impl Clone for $type {
108+ fn clone( & self ) -> Self {
109+ * self
110+ }
111+ }
112+
107113 impl $type {
108114 $v const MAX_AS_U32 : u32 = $max;
109115
@@ -145,7 +151,7 @@ macro_rules! newtype_index {
145151
146152 #[ inline]
147153 $v const unsafe fn from_u32_unchecked( value: u32 ) -> Self {
148- $type { private: value }
154+ unsafe { $type { private: value } }
149155 }
150156
151157 /// Extract value of this index as an integer.
@@ -328,12 +334,13 @@ macro_rules! newtype_index {
328334 derive [ $( $derives: ident, ) +]
329335 $( $tokens: tt) * ) => (
330336 newtype_index!(
331- @derives [ $( $derives, ) + RustcDecodable , RustcEncodable , ]
337+ @derives [ $( $derives, ) + RustcEncodable , ]
332338 @type [ $type]
333339 @max [ $max]
334340 @vis [ $v]
335341 @debug_format [ $debug_format]
336342 $( $tokens) * ) ;
343+ newtype_index!( @decodable $type) ;
337344 ) ;
338345
339346 // The case where no derives are added, but encodable is overridden. Don't
@@ -360,12 +367,29 @@ macro_rules! newtype_index {
360367 @debug_format [ $debug_format: tt]
361368 $( $tokens: tt) * ) => (
362369 newtype_index!(
363- @derives [ RustcDecodable , RustcEncodable , ]
370+ @derives [ RustcEncodable , ]
364371 @type [ $type]
365372 @max [ $max]
366373 @vis [ $v]
367374 @debug_format [ $debug_format]
368375 $( $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+ }
369393 ) ;
370394
371395 // Rewrite final without comma to one that includes comma
0 commit comments