@@ -120,62 +120,44 @@ macro_rules! newtype_index {
120
120
impl $type {
121
121
$v const MAX_AS_U32 : u32 = $max;
122
122
123
- $v const MAX : Self = Self :: from_u32_const ( $max) ;
123
+ $v const MAX : Self = Self :: from_u32 ( $max) ;
124
124
125
125
#[ inline]
126
- $v fn from_usize( value: usize ) -> Self {
126
+ $v const fn from_usize( value: usize ) -> Self {
127
127
assert!( value <= ( $max as usize ) ) ;
128
128
unsafe {
129
129
Self :: from_u32_unchecked( value as u32 )
130
130
}
131
131
}
132
132
133
133
#[ inline]
134
- $v fn from_u32( value: u32 ) -> Self {
134
+ $v const fn from_u32( value: u32 ) -> Self {
135
135
assert!( value <= $max) ;
136
136
unsafe {
137
137
Self :: from_u32_unchecked( value)
138
138
}
139
139
}
140
140
141
- /// Hacky variant of `from_u32` for use in constants.
142
- /// This version checks the "max" constraint by using an
143
- /// invalid array dereference.
144
- #[ inline]
145
- $v const fn from_u32_const( value: u32 ) -> Self {
146
- // This will fail at const eval time unless `value <=
147
- // max` is true (in which case we get the index 0).
148
- // It will also fail at runtime, of course, but in a
149
- // kind of wacky way.
150
- let _ = [ "out of range value used" ] [
151
- !( value <= $max) as usize
152
- ] ;
153
-
154
- unsafe {
155
- Self { private: value }
156
- }
157
- }
158
-
159
141
#[ inline]
160
142
$v const unsafe fn from_u32_unchecked( value: u32 ) -> Self {
161
143
Self { private: value }
162
144
}
163
145
164
146
/// Extracts the value of this index as an integer.
165
147
#[ inline]
166
- $v fn index( self ) -> usize {
148
+ $v const fn index( self ) -> usize {
167
149
self . as_usize( )
168
150
}
169
151
170
152
/// Extracts the value of this index as a `u32`.
171
153
#[ inline]
172
- $v fn as_u32( self ) -> u32 {
154
+ $v const fn as_u32( self ) -> u32 {
173
155
self . private
174
156
}
175
157
176
158
/// Extracts the value of this index as a `usize`.
177
159
#[ inline]
178
- $v fn as_usize( self ) -> usize {
160
+ $v const fn as_usize( self ) -> usize {
179
161
self . as_u32( ) as usize
180
162
}
181
163
}
0 commit comments