1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
macro_rules! impl_range {
    ($id:ident, $max:expr, $($a:ident = $b:expr,) *) => (        
        #[derive(PartialEq, Eq, Clone, Copy)]
        #[repr(usize)]
        pub enum $id {
            $($a = $b, )*
        }
        impl From<u8> for $id {
            #[inline]
            fn from(other: u8) -> Self {
                assert!(other < $max);
                unsafe { transmute(other as usize) }
            }
        }

        impl From<$id> for u8 {
            #[inline]
            fn from(other: $id) -> u8 {
                other as u8
            }
        }

        impl PartialEq<u8> for $id {
            #[inline]
            fn eq(&self, other: &u8) -> bool {
                *self as usize == *other as usize
            }
        }

        impl From<u16> for $id {
            #[inline]
            fn from(other: u16) -> Self {
                assert!(other < $max);
                unsafe { transmute(other as usize) }
            }
        }

        impl From<$id> for u16 {
            #[inline]
            fn from(other: $id) -> u16 {
                other as u16
            }
        }

        impl PartialEq<u16> for $id {
            #[inline]
            fn eq(&self, other: &u16) -> bool {
                *self as usize == *other as usize
            }
        }

        impl From<u32> for $id {
            #[inline]
            fn from(other: u32) -> Self {
                assert!(other < $max);
                unsafe { transmute(other as usize) }
            }
        }

        impl From<$id> for u32 {
            #[inline]
            fn from(other: $id) -> u32 {
                other as u32
            }
        }

        impl PartialEq<u32> for $id {
            #[inline]
            fn eq(&self, other: &u32) -> bool {
                *self as usize == *other as usize
            }
        }

        impl From<usize> for $id {
            #[inline]
            fn from(other: usize) -> Self {
                assert!(other < $max);
                unsafe { transmute(other as usize) }
            }
        }

        impl From<$id> for usize {
            #[inline]
            fn from(other: $id) -> usize {
                other as usize
            }
        }

        impl PartialEq<usize> for $id {
            #[inline]
            fn eq(&self, other: &usize) -> bool {
                *self as usize == *other as usize
            }
        }

        impl From<i32> for $id {
            #[inline]
            fn from(other: i32) -> Self {
                assert!(other >= 0);
                assert!(other < $max);
                unsafe { transmute(other as usize) }
            }
        }

        impl From<$id> for i32 {
            #[inline]
            fn from(other: $id) -> i32 {
                other as i32
            }
        }

        impl PartialEq<i32> for $id {
            #[inline]
            fn eq(&self, other: &i32) -> bool {
                *self as usize == *other as usize
            }
        }

        impl $id {
            #[inline]
            /// Returns the primitive representation of the value.            
            pub fn value(&self) -> usize {
                *self as usize
            }

            #[inline]
            /// Constructs the value without a range check.
            pub unsafe fn from_u8_unchecked(other: u8) -> Self {
                transmute(other as usize)
            }

            #[inline]
            /// Constructs the value without a range check.
            pub unsafe fn from_u16_unchecked(other: u16) -> Self {
                transmute(other as usize)
            }

            #[inline]
            /// Constructs the value without a range check.
            pub unsafe fn from_u32_unchecked(other: u32) -> Self {
                transmute(other as usize)
            }

            #[inline]
            /// Constructs the value without a range check.
            pub unsafe fn from_usize_unchecked(other: usize) -> Self {
                transmute(other as usize)
            }
            
            #[inline]
            /// Returns the value as an u8.
            pub fn into_u8(self) -> u8 {
                self as u8
            }

            #[inline]
            /// Returns the value as an u16.
            pub fn into_u16(self) -> u16 {
                self as u16
            }

            #[inline]
            /// Returns the value as an u32.            
            pub fn into_u32(self) -> u32 {
                self as u32
            }

            #[inline]
            /// Returns the value as an usize.            
            pub fn into_usize(self) -> usize {
                self as usize
            }

            #[inline]
            /// Returns the value as an i32.
            pub fn into_i32(self) -> i32 {
                self as i32
            }            
        }

        impl fmt::Debug for $id {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                write!(f, "0x{:x}", *self as u8)
            }
        }

        impl fmt::Display for $id {
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            (*self as u8).fmt(f)
            }
        }

        impl fmt::LowerHex for $id {
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            (*self as u8).fmt(f)
            }
        }

    )
}

macro_rules! impl_range_from {
    ($id:ident $(, $other:ident)* ) => (
        $(
        impl From<$other> for $id {
            #[inline]
            fn from(other: $other) -> Self {
                unsafe { transmute(other as usize) }
            }
        }
        )*
    )
}