@@ -17,16 +17,16 @@ use super::CharEq;
1717
1818pub trait Pattern < ' a > : Sized {
1919 type Searcher : Searcher < ' a > ;
20- fn into_matcher ( self , haystack : & ' a str ) -> Self :: Searcher ;
20+ fn into_searcher ( self , haystack : & ' a str ) -> Self :: Searcher ;
2121
2222 #[ inline]
2323 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 ( )
2525 }
2626
2727 #[ inline]
2828 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) ;
3030 loop {
3131 match matcher. next ( ) {
3232 SearchStep :: Match ( i, _) if i == idx => return true ,
@@ -42,7 +42,7 @@ pub trait Pattern<'a>: Sized {
4242 #[ inline]
4343 fn match_ends_at ( self , haystack : & ' a str , idx : usize ) -> bool
4444 where Self :: Searcher : ReverseSearcher < ' a > {
45- let mut matcher = self . into_matcher ( haystack) ;
45+ let mut matcher = self . into_searcher ( haystack) ;
4646 loop {
4747 match matcher. next_back ( ) {
4848 SearchStep :: Match ( _, j) if idx == j => return true ,
@@ -115,25 +115,27 @@ pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
115115
116116pub trait DoubleEndedSearcher < ' a > : ReverseSearcher < ' a > { }
117117
118- // Impl for CharEq
118+ // Impl for a CharEq wrapper
119119
120- pub struct CharEqSearcher < ' a , C > {
120+ struct CharEqPattern < C : CharEq > ( C ) ;
121+
122+ pub struct CharEqSearcher < ' a , C : CharEq > {
121123 char_eq : C ,
122124 haystack : & ' a str ,
123125 char_indices : super :: CharIndices < ' a > ,
124126 #[ allow( dead_code) ]
125127 ascii_only : bool ,
126128}
127129
128- impl < ' a , C : CharEq > Pattern < ' a > for C {
130+ impl < ' a , C : CharEq > Pattern < ' a > for CharEqPattern < C > {
129131 type Searcher = CharEqSearcher < ' a , C > ;
130132
131133 #[ inline]
132- fn into_matcher ( self , haystack : & ' a str ) -> CharEqSearcher < ' a , C > {
134+ fn into_searcher ( self , haystack : & ' a str ) -> CharEqSearcher < ' a , C > {
133135 CharEqSearcher {
134- ascii_only : self . only_ascii ( ) ,
136+ ascii_only : self . 0 . only_ascii ( ) ,
135137 haystack : haystack,
136- char_eq : self ,
138+ char_eq : self . 0 ,
137139 char_indices : haystack. char_indices ( ) ,
138140 }
139141 }
@@ -203,7 +205,7 @@ impl<'a, 'b> Pattern<'a> for &'b str {
203205 type Searcher = StrSearcher < ' a , ' b > ;
204206
205207 #[ inline]
206- fn into_matcher ( self , haystack : & ' a str ) -> StrSearcher < ' a , ' b > {
208+ fn into_searcher ( self , haystack : & ' a str ) -> StrSearcher < ' a , ' b > {
207209 StrSearcher {
208210 haystack : haystack,
209211 needle : self ,
@@ -293,3 +295,65 @@ where F: FnOnce(&mut StrSearcher) -> SearchStep,
293295 SearchStep :: Done
294296 }
295297}
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