@@ -17,11 +17,7 @@ pub trait Re: Sized {
1717 /// The haystack that can be searched by this [Re] using the
1818 /// [is_match_between][Re::is_match_between] and [captures_between][Re::captures_between]
1919 /// methods.
20- type Haystack : Haystack < Slice = Self :: Slice > + ?Sized ;
21-
22- /// The slice type of the associated [Haystack] that is returned by [Captures] methods when
23- /// extracting matches and submatches.
24- type Slice : Writable + ?Sized ;
20+ type Haystack < ' h > : Haystack + ' h ;
2521
2622 /// Attempt to compile the given regular expression for use inside of a [Structex][crate::Structex].
2723 ///
@@ -43,7 +39,7 @@ pub trait Re: Sized {
4339 /// This does not need to search for the leftmost-longest match and where possible should be
4440 /// faster to run that [Re::captures_between] which needs to extract the position of the match
4541 /// itself and all submatches.
46- fn is_match_between ( & self , haystack : & Self :: Haystack , from : usize , to : usize ) -> bool ;
42+ fn is_match_between ( & self , haystack : Self :: Haystack < ' _ > , from : usize , to : usize ) -> bool ;
4743
4844 /// Searches for the first match of this regex between the given byte offsets in the given
4945 /// haystack, returning the overall match along with the matches of each capture group in the
@@ -52,27 +48,29 @@ pub trait Re: Sized {
5248 /// See [RawCaptures::new] for requirements around constructing the return type.
5349 fn captures_between (
5450 & self ,
55- haystack : & Self :: Haystack ,
51+ haystack : Self :: Haystack < ' _ > ,
5652 from : usize ,
5753 to : usize ,
5854 ) -> Option < RawCaptures > ;
5955}
6056
6157/// A haystack is an associated type on [Re] that the regular expression engine can be run against.
6258///
63- /// Typically this is a [str] but some engines may support richer types in order to provide
59+ /// Typically this is a [& str] but some engines may support richer types in order to provide
6460/// searching of streams or discontiguous inputs.
65- pub trait Haystack : Writable + fmt:: Debug + PartialEq + Eq + Sync {
61+ pub trait Haystack : Writable + fmt:: Debug + PartialEq + Eq + Copy {
6662 /// The output of the [slice][Haystack::slice] method.
6763 ///
6864 /// Typically the same type as the haystack itself but not required to be so.
69- type Slice : Writable + Sync + ?Sized ;
65+ type Slice < ' h > : Writable
66+ where
67+ Self : ' h ;
7068
7169 /// A contiguous sub-section of the haystack between the given bytes offsets.
7270 ///
7371 /// The given byte offsets from a half-open interval, inclusive of `from` but omitting `to`.
7472 /// This is the same semantics as a normal Rust range `from..to`.
75- fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice ;
73+ fn slice < ' h > ( & ' h self , from : usize , to : usize ) -> Self :: Slice < ' h > ;
7674
7775 /// The maximum length of the full haystack in bytes.
7876 ///
@@ -90,31 +88,13 @@ pub trait Writable {
9088 W : io:: Write ;
9189}
9290
93- impl Haystack for str {
94- type Slice = str ;
95-
96- fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice {
97- & self [ from..to]
98- }
99-
100- fn max_len ( & self ) -> usize {
101- self . len ( )
102- }
103- }
104-
105- impl Writable for str {
106- fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
91+ impl Haystack for & str {
92+ type Slice < ' h >
93+ = & ' h str
10794 where
108- W : io:: Write ,
109- {
110- w. write_all ( self . as_bytes ( ) ) . map ( |_| self . len ( ) )
111- }
112- }
95+ Self : ' h ;
11396
114- impl Haystack for String {
115- type Slice = str ;
116-
117- fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice {
97+ fn slice ( & self , from : usize , to : usize ) -> & str {
11898 & self [ from..to]
11999 }
120100
@@ -123,7 +103,7 @@ impl Haystack for String {
123103 }
124104}
125105
126- impl Writable for String {
106+ impl Writable for & str {
127107 fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
128108 where
129109 W : io:: Write ,
@@ -132,31 +112,13 @@ impl Writable for String {
132112 }
133113}
134114
135- impl Haystack for [ u8 ] {
136- type Slice = [ u8 ] ;
137-
138- fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice {
139- & self [ from..to]
140- }
141-
142- fn max_len ( & self ) -> usize {
143- self . len ( )
144- }
145- }
146-
147- impl Writable for [ u8 ] {
148- fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
115+ impl Haystack for & [ u8 ] {
116+ type Slice < ' h >
117+ = & ' h [ u8 ]
149118 where
150- W : io:: Write ,
151- {
152- w. write_all ( self ) . map ( |_| self . len ( ) )
153- }
154- }
155-
156- impl Haystack for Vec < u8 > {
157- type Slice = [ u8 ] ;
119+ Self : ' h ;
158120
159- fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice {
121+ fn slice ( & self , from : usize , to : usize ) -> & [ u8 ] {
160122 & self [ from..to]
161123 }
162124
@@ -165,7 +127,7 @@ impl Haystack for Vec<u8> {
165127 }
166128}
167129
168- impl Writable for Vec < u8 > {
130+ impl Writable for & [ u8 ] {
169131 fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
170132 where
171133 W : io:: Write ,
@@ -212,19 +174,19 @@ impl RawCaptures {
212174/// Represents the capture group positions for a single [Re] match in terms of byte offsets into
213175/// the original haystack that the match was run against.
214176#[ derive( Debug , PartialEq , Eq ) ]
215- pub struct Captures < ' h , R >
177+ pub struct Captures < H >
216178where
217- R : Re ,
179+ H : Haystack ,
218180{
219- haystack : & ' h R :: Haystack ,
181+ haystack : H ,
220182 caps : Vec < Option < ( usize , usize ) > > ,
221183}
222184
223- impl < ' h , R > Captures < ' h , R >
185+ impl < H > Captures < H >
224186where
225- R : Re ,
187+ H : Haystack ,
226188{
227- pub ( crate ) fn new ( haystack : & ' h R :: Haystack , caps : Vec < Option < ( usize , usize ) > > ) -> Self {
189+ pub ( crate ) fn new ( haystack : H , caps : Vec < Option < ( usize , usize ) > > ) -> Self {
228190 Self { haystack, caps }
229191 }
230192
@@ -265,21 +227,21 @@ where
265227 }
266228
267229 /// The full text of the match in the original haystack.
268- pub fn match_text ( & self ) -> & ' h R :: Slice {
230+ pub fn match_text ( & self ) -> H :: Slice < ' _ > {
269231 let ( from, to) = self . get_match ( ) ;
270232
271233 self . haystack . slice ( from, to)
272234 }
273235
274236 /// The full text of the submatch, if present, in the original haystack.
275- pub fn submatch_text ( & self , n : usize ) -> Option < & ' h R :: Slice > {
237+ pub fn submatch_text ( & self , n : usize ) -> Option < H :: Slice < ' _ > > {
276238 let ( from, to) = self . get ( n) ?;
277239
278240 Some ( self . haystack . slice ( from, to) )
279241 }
280242
281243 /// Iterate over all submatches starting with the full match.
282- pub fn iter_submatches ( & ' h self ) -> impl Iterator < Item = Option < & ' h R :: Slice > > {
244+ pub fn iter_submatches ( & self ) -> impl Iterator < Item = Option < H :: Slice < ' _ > > > {
283245 self . caps
284246 . iter ( )
285247 . map ( |cap| cap. map ( |( from, to) | self . haystack . slice ( from, to) ) )
@@ -289,20 +251,19 @@ where
289251#[ cfg( feature = "regex" ) ]
290252impl Re for regex:: Regex {
291253 type CompileError = regex:: Error ;
292- type Haystack = str ;
293- type Slice = str ;
254+ type Haystack < ' h > = & ' h str ;
294255
295256 fn compile ( re : & str ) -> Result < Self , Self :: CompileError > {
296257 regex:: RegexBuilder :: new ( re) . multi_line ( true ) . build ( )
297258 }
298259
299- fn is_match_between ( & self , haystack : & Self :: Haystack , from : usize , to : usize ) -> bool {
260+ fn is_match_between ( & self , haystack : Self :: Haystack < ' _ > , from : usize , to : usize ) -> bool {
300261 self . is_match ( & haystack[ from..to] )
301262 }
302263
303264 fn captures_between (
304265 & self ,
305- haystack : & Self :: Haystack ,
266+ haystack : Self :: Haystack < ' _ > ,
306267 from : usize ,
307268 to : usize ,
308269 ) -> Option < RawCaptures > {
0 commit comments