@@ -150,8 +150,6 @@ extern crate proptest;
150150#[ cfg( test) ]
151151extern crate region;
152152
153- use std:: marker:: PhantomData ;
154-
155153include ! ( concat!( env!( "OUT_DIR" ) , "/src/macros.rs" ) ) ;
156154
157155#[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
@@ -163,49 +161,18 @@ mod fallback;
163161#[ cfg( feature = "pattern" ) ]
164162mod pattern;
165163
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-
195164/// 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 >
197166where
198167 F : Fn ( u8 ) -> bool ,
199168{
200169 #[ 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 > ) ,
202173
203174 #[ 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 > ) ,
209176}
210177
211178impl < F > Bytes < F >
@@ -220,23 +187,34 @@ where
220187 /// the same bytes as in the array.
221188 #[ allow( unused_variables) ]
222189 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+ }
226194
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+ }
229199
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+ }
231207 }
232208 }
233209
234210 /// Searches the slice for the first matching byte in the set.
235211 #[ inline]
236212 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) ,
240218 }
241219 }
242220}
@@ -282,42 +260,55 @@ where
282260pub type AsciiCharsConst = AsciiChars < fn ( u8 ) -> bool > ;
283261
284262/// Searches a slice for the first occurence of the subslice.
285- pub struct ByteSubstring < T > {
263+ pub enum ByteSubstring < T > {
286264 #[ cfg( any( jetscii_sse4_2 = "yes" , jetscii_sse4_2 = "maybe" ) ) ]
287- simd : simd :: ByteSubstring < ' a > ,
265+ SIMD ( simd:: ByteSubstring < T > ) ,
288266
289267 #[ cfg( any( jetscii_sse4_2 = "maybe" , jetscii_sse4_2 = "no" ) ) ]
290- fallback : fallback :: ByteSubstring < T > ,
268+ Fallback ( fallback:: ByteSubstring < T > ) ,
291269}
292270
293271impl < T > ByteSubstring < T >
294272where
295273 T : AsRef < [ u8 ] > ,
296274{
297275 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+ }
301280
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) )
304291 }
305292 }
306293
307294 #[ cfg( feature = "pattern" ) ]
308295 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 ( ) ,
312301 }
313302 }
314303
315304 /// Searches the slice for the first occurence of the subslice.
316305 #[ inline]
317306 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) ,
321312 }
322313 }
323314}
0 commit comments