@@ -40,39 +40,86 @@ impl Idx for u32 {
40
40
41
41
#[ macro_export]
42
42
macro_rules! newtype_index {
43
- ( $name: ident) => (
44
- newtype_index!( $name, unsafe { :: std:: intrinsics:: type_name:: <$name>( ) } ) ;
45
- ) ;
43
+ // ---- private rules ----
46
44
47
- ( $name: ident, $debug_name: expr) => (
45
+ // Base case, user-defined constants (if any) have already been defined
46
+ ( @type [ $type: ident] @max[ $max: expr] @descr[ $descr: expr] ) => (
48
47
#[ derive( Copy , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ,
49
- RustcEncodable , RustcDecodable ) ]
50
- pub struct $name( u32 ) ;
51
-
52
- impl $name {
53
- // HACK use for constants
54
- #[ allow( unused) ]
55
- const fn const_new( x: u32 ) -> Self {
56
- $name( x)
57
- }
58
- }
48
+ RustcEncodable , RustcDecodable ) ]
49
+ pub struct $type( u32 ) ;
59
50
60
- impl Idx for $name {
51
+ impl Idx for $type {
61
52
fn new( value: usize ) -> Self {
62
- assert!( value < ( :: std :: u32 :: MAX ) as usize ) ;
63
- $name ( value as u32 )
53
+ assert!( value < ( $max ) as usize ) ;
54
+ $type ( value as u32 )
64
55
}
65
56
fn index( self ) -> usize {
66
57
self . 0 as usize
67
58
}
68
59
}
69
60
70
- impl :: std:: fmt:: Debug for $name {
61
+ impl :: std:: fmt:: Debug for $type {
71
62
fn fmt( & self , fmt: & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
72
- write!( fmt, "{}{}" , $debug_name , self . 0 )
63
+ write!( fmt, "{}{}" , $descr , self . 0 )
73
64
}
74
65
}
75
- )
66
+ ) ;
67
+
68
+ // Replace existing default for max (as final param)
69
+ ( @type [ $type: ident] @max[ $_max: expr] @descr[ $descr: expr] MAX = $max: expr) => (
70
+ newtype_index!( @type [ $type] @max[ $max] @descr[ $descr] ) ;
71
+ ) ;
72
+
73
+ // Replace existing default for max
74
+ ( @type [ $type: ident] @max[ $_max: expr] @descr[ $descr: expr] MAX = $max: expr, $( $idents: ident = $constants: expr) ,* ) => (
75
+ newtype_index!( @type [ $type] @max[ $max] @descr[ $descr] ) ;
76
+ ) ;
77
+
78
+ // Replace existing default for description (as final param)
79
+ ( @type [ $type: ident] @max[ $max: expr] @descr[ $_descr: expr] DESCRIPTION = $descr: expr) => (
80
+ newtype_index!( @type [ $type] @max[ $max] @descr[ $descr] ) ;
81
+ ) ;
82
+
83
+ // Replace existing default for description
84
+ ( @type [ $type: ident] @max[ $max: expr] @descr[ $_descr: expr] DESCRIPTION = $descr: expr, $( $idents: ident = $constants: expr) ,* ) => (
85
+ newtype_index!( @type [ $type] @max[ $max] @descr[ $descr] $( $idents = $constants) ,* ) ;
86
+ ) ;
87
+
88
+ // Assign a user-defined constant (as final param)
89
+ ( @type [ $type: ident] @max[ $max: expr] @descr[ $descr: expr] $name: ident = $constant: expr) => (
90
+ pub const $name: $type = $type( $constant) ;
91
+ newtype_index!( @type [ $type] @max[ $max] @descr[ $descr] ) ;
92
+ ) ;
93
+
94
+ // Assign a user-defined constant
95
+ ( @type [ $type: ident] @max[ $max: expr] @descr[ $descr: expr] $name: ident = $constant: expr, $( $idents: ident = $constants: expr) ,* ) => (
96
+ pub const $name: $type = $type( $constant) ;
97
+ newtype_index!( @type [ $type] @max[ $max] @descr[ $descr] $( $idents = $constants) ,* ) ;
98
+ ) ;
99
+
100
+ // ---- public rules ----
101
+
102
+ // Use default constants
103
+ ( $name: ident) => (
104
+ newtype_index!(
105
+ @type [ $name]
106
+ @max[ :: std:: u32 :: MAX ]
107
+ @descr[ unsafe { :: std:: intrinsics:: type_name:: <$name>( ) } ] ) ;
108
+ ) ;
109
+
110
+ // Define any constants
111
+ ( $name: ident, const { $( $idents: ident = $constants: expr, ) + } ) => (
112
+ newtype_index!(
113
+ @type [ $name]
114
+ @max[ :: std:: u32 :: MAX ]
115
+ @descr[ unsafe { :: std:: intrinsics:: type_name:: <$name>( ) } ]
116
+ $( $idents = $constants) ,+) ;
117
+ ) ;
118
+
119
+ // Rewrite missing trailing comma in const to version with trailing comma
120
+ ( $name: ident, const { $( $idents: ident = $constants: expr) ,+ } ) => (
121
+ newtype_index!( $name, const { $( $idents = $constants, ) + } ) ;
122
+ ) ;
76
123
}
77
124
78
125
#[ derive( Clone , PartialEq , Eq ) ]
0 commit comments