@@ -150,8 +150,6 @@ extern crate proptest;
150
150
#[ cfg( test) ]
151
151
extern crate region;
152
152
153
- use std:: marker:: PhantomData ;
154
-
155
153
include ! ( concat!( env!( "OUT_DIR" ) , "/src/macros.rs" ) ) ;
156
154
157
155
#[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
@@ -163,49 +161,18 @@ mod fallback;
163
161
#[ cfg( feature = "pattern" ) ]
164
162
mod pattern;
165
163
166
- macro_rules! dispatch {
167
- ( simd: $simd: expr, fallback: $fallback: expr, ) => {
168
- // If we can tell at compile time that we have support,
169
- // call the optimized code directly.
170
- #[ cfg( jetscii_sse4_2 = "yes" ) ]
171
- {
172
- $simd
173
- }
174
-
175
- // If we can tell at compile time that we will *never* have
176
- // support, call the fallback directly.
177
- #[ cfg( jetscii_sse4_2 = "no" ) ]
178
- {
179
- $fallback
180
- }
181
-
182
- // Otherwise, we will be run on a machine with or without
183
- // support, so we perform runtime detection.
184
- #[ cfg( jetscii_sse4_2 = "maybe" ) ]
185
- {
186
- if is_x86_feature_detected!( "sse4.2" ) {
187
- $simd
188
- } else {
189
- $fallback
190
- }
191
- }
192
- } ;
193
- }
194
-
195
164
/// Searches a slice for a set of bytes. Up to 16 bytes may be used.
196
- pub struct Bytes < F >
165
+ pub enum Bytes < F >
197
166
where
198
167
F : Fn ( u8 ) -> bool ,
199
168
{
200
169
#[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
201
- simd : simd:: Bytes ,
170
+ // Since we might not use the fallback implementation, we add
171
+ // PhantomData to avoid an unused type parameter
172
+ SIMD ( simd:: Bytes , core:: marker:: PhantomData < F > ) ,
202
173
203
174
#[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
204
- fallback : fallback:: Bytes < F > ,
205
-
206
- // Since we might not use the fallback implementation, we add this
207
- // to avoid unused type parameters.
208
- _fallback : PhantomData < F > ,
175
+ Fallback ( fallback:: Bytes < F > ) ,
209
176
}
210
177
211
178
impl < F > Bytes < F >
@@ -220,23 +187,34 @@ where
220
187
/// the same bytes as in the array.
221
188
#[ allow( unused_variables) ]
222
189
pub /* const */ fn new ( bytes : [ u8 ; 16 ] , len : i32 , fallback : F ) -> Self {
223
- Bytes {
224
- #[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
225
- simd : simd:: Bytes :: new ( bytes, len) ,
190
+ #[ cfg( jetscii_sse4_2 = "yes" ) ]
191
+ {
192
+ Self :: SIMD ( simd:: Bytes :: new ( bytes, len) , Default :: default ( ) )
193
+ }
226
194
227
- #[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
228
- fallback : fallback:: Bytes :: new ( fallback) ,
195
+ #[ cfg( jetscii_sse4_2 = "no" ) ]
196
+ {
197
+ Self :: Fallback ( fallback:: Bytes :: new ( fallback) )
198
+ }
229
199
230
- _fallback : PhantomData ,
200
+ #[ cfg( jetscii_sse4_2 = "maybe" ) ]
201
+ {
202
+ if is_x86_feature_detected ! ( "sse4.2" ) {
203
+ Self :: SIMD ( simd:: Bytes :: new ( bytes, len) , Default :: default ( ) )
204
+ } else {
205
+ Self :: Fallback ( fallback:: Bytes :: new ( fallback) )
206
+ }
231
207
}
232
208
}
233
209
234
210
/// Searches the slice for the first matching byte in the set.
235
211
#[ inline]
236
212
pub fn find ( & self , haystack : & [ u8 ] ) -> Option < usize > {
237
- dispatch ! {
238
- simd: unsafe { self . simd. find( haystack) } ,
239
- fallback: self . fallback. find( haystack) ,
213
+ match self {
214
+ #[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
215
+ Self :: SIMD ( needle, _) => unsafe { needle. find ( haystack) } ,
216
+ #[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
217
+ Self :: Fallback ( needle) => needle. find ( haystack) ,
240
218
}
241
219
}
242
220
}
@@ -282,42 +260,55 @@ where
282
260
pub type AsciiCharsConst = AsciiChars < fn ( u8 ) -> bool > ;
283
261
284
262
/// Searches a slice for the first occurence of the subslice.
285
- pub struct ByteSubstring < T > {
263
+ pub enum ByteSubstring < T > {
286
264
#[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
287
- simd : simd :: ByteSubstring < ' a > ,
265
+ SIMD ( simd:: ByteSubstring < T > ) ,
288
266
289
267
#[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
290
- fallback : fallback :: ByteSubstring < T > ,
268
+ Fallback ( fallback:: ByteSubstring < T > ) ,
291
269
}
292
270
293
271
impl < T > ByteSubstring < T >
294
272
where
295
273
T : AsRef < [ u8 ] > ,
296
274
{
297
275
pub /* const */ fn new ( needle : T ) -> Self {
298
- ByteSubstring {
299
- #[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
300
- simd : simd:: ByteSubstring :: new ( needle) ,
276
+ #[ cfg( jetscii_sse4_2 = "yes" ) ]
277
+ {
278
+ Self :: SIMD ( simd:: ByteSubstring :: new ( needle) )
279
+ }
301
280
302
- #[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
303
- fallback : fallback:: ByteSubstring :: new ( needle) ,
281
+ #[ cfg( jetscii_sse4_2 = "no" ) ]
282
+ {
283
+ Self :: Fallback ( fallback:: ByteSubstring :: new ( needle) )
284
+ }
285
+
286
+ #[ cfg( jetscii_sse4_2 = "maybe" ) ]
287
+ if is_x86_feature_detected ! ( "sse4.2" ) {
288
+ Self :: SIMD ( simd:: ByteSubstring :: new ( needle) )
289
+ } else {
290
+ Self :: Fallback ( fallback:: ByteSubstring :: new ( needle) )
304
291
}
305
292
}
306
293
307
294
#[ cfg( feature = "pattern" ) ]
308
295
fn needle_len ( & self ) -> usize {
309
- dispatch ! {
310
- simd: self . simd. needle_len( ) ,
311
- fallback: self . fallback. needle_len( ) ,
296
+ match self {
297
+ #[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
298
+ Self :: SIMD ( needle) => needle. needle_len ( ) ,
299
+ #[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
300
+ Self :: Fallback ( needle) => needle. needle_len ( ) ,
312
301
}
313
302
}
314
303
315
304
/// Searches the slice for the first occurence of the subslice.
316
305
#[ inline]
317
306
pub fn find ( & self , haystack : & [ u8 ] ) -> Option < usize > {
318
- dispatch ! {
319
- simd: unsafe { self . simd. find( haystack) } ,
320
- fallback: self . fallback. find( haystack) ,
307
+ match self {
308
+ #[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
309
+ Self :: SIMD ( needle) => unsafe { needle. find ( haystack) } ,
310
+ #[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
311
+ Self :: Fallback ( needle) => needle. find ( haystack) ,
321
312
}
322
313
}
323
314
}
0 commit comments