@@ -17,16 +17,16 @@ use super::CharEq;
17
17
18
18
pub trait Pattern < ' a > : Sized {
19
19
type Searcher : Searcher < ' a > ;
20
- fn into_matcher ( self , haystack : & ' a str ) -> Self :: Searcher ;
20
+ fn into_searcher ( self , haystack : & ' a str ) -> Self :: Searcher ;
21
21
22
22
#[ inline]
23
23
fn is_contained_in ( self , haystack : & ' a str ) -> bool {
24
- self . into_matcher ( haystack) . next_match ( ) . is_some ( )
24
+ self . into_searcher ( haystack) . next_match ( ) . is_some ( )
25
25
}
26
26
27
27
#[ inline]
28
28
fn match_starts_at ( self , haystack : & ' a str , idx : usize ) -> bool {
29
- let mut matcher = self . into_matcher ( haystack) ;
29
+ let mut matcher = self . into_searcher ( haystack) ;
30
30
loop {
31
31
match matcher. next ( ) {
32
32
SearchStep :: Match ( i, _) if i == idx => return true ,
@@ -42,7 +42,7 @@ pub trait Pattern<'a>: Sized {
42
42
#[ inline]
43
43
fn match_ends_at ( self , haystack : & ' a str , idx : usize ) -> bool
44
44
where Self :: Searcher : ReverseSearcher < ' a > {
45
- let mut matcher = self . into_matcher ( haystack) ;
45
+ let mut matcher = self . into_searcher ( haystack) ;
46
46
loop {
47
47
match matcher. next_back ( ) {
48
48
SearchStep :: Match ( _, j) if idx == j => return true ,
@@ -115,25 +115,27 @@ pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
115
115
116
116
pub trait DoubleEndedSearcher < ' a > : ReverseSearcher < ' a > { }
117
117
118
- // Impl for CharEq
118
+ // Impl for a CharEq wrapper
119
119
120
- pub struct CharEqSearcher < ' a , C > {
120
+ struct CharEqPattern < C : CharEq > ( C ) ;
121
+
122
+ pub struct CharEqSearcher < ' a , C : CharEq > {
121
123
char_eq : C ,
122
124
haystack : & ' a str ,
123
125
char_indices : super :: CharIndices < ' a > ,
124
126
#[ allow( dead_code) ]
125
127
ascii_only : bool ,
126
128
}
127
129
128
- impl < ' a , C : CharEq > Pattern < ' a > for C {
130
+ impl < ' a , C : CharEq > Pattern < ' a > for CharEqPattern < C > {
129
131
type Searcher = CharEqSearcher < ' a , C > ;
130
132
131
133
#[ inline]
132
- fn into_matcher ( self , haystack : & ' a str ) -> CharEqSearcher < ' a , C > {
134
+ fn into_searcher ( self , haystack : & ' a str ) -> CharEqSearcher < ' a , C > {
133
135
CharEqSearcher {
134
- ascii_only : self . only_ascii ( ) ,
136
+ ascii_only : self . 0 . only_ascii ( ) ,
135
137
haystack : haystack,
136
- char_eq : self ,
138
+ char_eq : self . 0 ,
137
139
char_indices : haystack. char_indices ( ) ,
138
140
}
139
141
}
@@ -203,7 +205,7 @@ impl<'a, 'b> Pattern<'a> for &'b str {
203
205
type Searcher = StrSearcher < ' a , ' b > ;
204
206
205
207
#[ inline]
206
- fn into_matcher ( self , haystack : & ' a str ) -> StrSearcher < ' a , ' b > {
208
+ fn into_searcher ( self , haystack : & ' a str ) -> StrSearcher < ' a , ' b > {
207
209
StrSearcher {
208
210
haystack : haystack,
209
211
needle : self ,
@@ -293,3 +295,65 @@ where F: FnOnce(&mut StrSearcher) -> SearchStep,
293
295
SearchStep :: Done
294
296
}
295
297
}
298
+
299
+ macro_rules! associated_items {
300
+ ( $t: ty, $s: ident, $e: expr) => {
301
+ // FIXME: #22463
302
+ //type Searcher = $t;
303
+
304
+ fn into_searcher( self , haystack: & ' a str ) -> $t {
305
+ let $s = self ;
306
+ $e. into_searcher( haystack)
307
+ }
308
+
309
+ #[ inline]
310
+ fn is_contained_in( self , haystack: & ' a str ) -> bool {
311
+ let $s = self ;
312
+ $e. is_contained_in( haystack)
313
+ }
314
+
315
+ #[ inline]
316
+ fn match_starts_at( self , haystack: & ' a str , idx: usize ) -> bool {
317
+ let $s = self ;
318
+ $e. match_starts_at( haystack, idx)
319
+ }
320
+
321
+ // FIXME: #21750
322
+ /*#[inline]
323
+ fn match_ends_at(self, haystack: &'a str, idx: usize) -> bool
324
+ where $t: ReverseSearcher<'a> {
325
+ let $s = self;
326
+ $e.match_ends_at(haystack, idx)
327
+ }*/
328
+ }
329
+ }
330
+
331
+ // CharEq delegation impls
332
+
333
+ impl < ' a , ' b > Pattern < ' a > for & ' b [ char ] {
334
+ type Searcher = <CharEqPattern < Self > as Pattern < ' a > >:: Searcher ;
335
+ associated_items ! ( <CharEqPattern <Self > as Pattern <' a>>:: Searcher ,
336
+ s, CharEqPattern ( s) ) ;
337
+ }
338
+
339
+ impl < ' a > Pattern < ' a > for char {
340
+ type Searcher = <CharEqPattern < Self > as Pattern < ' a > >:: Searcher ;
341
+ associated_items ! ( <CharEqPattern <Self > as Pattern <' a>>:: Searcher ,
342
+ s, CharEqPattern ( s) ) ;
343
+ }
344
+
345
+ impl < ' a , F > Pattern < ' a > for F where F : FnMut ( char ) -> bool {
346
+ type Searcher = <CharEqPattern < Self > as Pattern < ' a > >:: Searcher ;
347
+ associated_items ! ( <CharEqPattern <Self > as Pattern <' a>>:: Searcher ,
348
+ s, CharEqPattern ( s) ) ;
349
+ }
350
+
351
+ // Deref-forward impl
352
+
353
+ use ops:: Deref ;
354
+
355
+ impl < ' a , ' b , P : ' b + ?Sized , T : Deref < Target = P > + ?Sized > Pattern < ' a > for & ' b T where & ' b P : Pattern < ' a > {
356
+ type Searcher = <& ' b P as Pattern < ' a > >:: Searcher ;
357
+ associated_items ! ( <& ' b P as Pattern <' a>>:: Searcher ,
358
+ s, ( & * * s) ) ;
359
+ }
0 commit comments