5555}
5656
5757/// Something that supports extracting a contiguous sub-section between two bytes offsets.
58- pub trait Sliceable : Writable + Copy {
58+ pub trait Sliceable : Writable {
5959 /// The output of the [slice][Sliceable::slice] method.
6060 type Slice < ' h > : Writable
6161 where
@@ -75,43 +75,50 @@ pub trait Sliceable: Writable + Copy {
7575 fn max_len ( & self ) -> usize ;
7676}
7777
78- /// Something that can be written to to a given [io::Write].
79- pub trait Writable {
80- /// Writes `self` to the given [io::Write], returning the number of bytes written.
81- fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
82- where
83- W : io:: Write ;
84- }
85-
86- impl Sliceable for & str {
78+ impl < T > Sliceable for T
79+ where
80+ T : AsRef < str > + ?Sized ,
81+ {
8782 type Slice < ' h >
8883 = & ' h str
8984 where
9085 Self : ' h ;
9186
9287 fn char_at ( & self , byte_offset : usize ) -> Option < char > {
93- if byte_offset >= self . len ( ) {
88+ if byte_offset >= self . as_ref ( ) . len ( ) {
9489 return None ;
9590 }
9691
97- self [ byte_offset..] . chars ( ) . next ( )
92+ self . as_ref ( ) [ byte_offset..] . chars ( ) . next ( )
9893 }
9994
10095 fn slice ( & self , range : Range < usize > ) -> & str {
101- & self [ range]
96+ & self . as_ref ( ) [ range]
10297 }
10398
10499 fn max_len ( & self ) -> usize {
105- self . len ( )
100+ self . as_ref ( ) . len ( )
106101 }
107102}
108103
109- impl Writable for & str {
104+ /// Something that can be written to to a given [io::Write].
105+ pub trait Writable {
106+ /// Writes `self` to the given [io::Write], returning the number of bytes written.
107+ fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
108+ where
109+ W : io:: Write ;
110+ }
111+
112+ impl < T > Writable for T
113+ where
114+ T : AsRef < str > + ?Sized ,
115+ {
110116 fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
111117 where
112118 W : io:: Write ,
113119 {
114- w. write_all ( self . as_bytes ( ) ) . map ( |_| self . len ( ) )
120+ w. write_all ( self . as_ref ( ) . as_bytes ( ) )
121+ . map ( |_| self . as_ref ( ) . len ( ) )
115122 }
116123}
117124
@@ -154,19 +161,19 @@ impl RawCaptures {
154161/// Represents the capture group positions for a single [RegexEngine] match in terms of byte
155162/// offsets into the original haystack that the match was run against.
156163#[ derive( Debug , PartialEq , Eq ) ]
157- pub struct Captures < H >
164+ pub struct Captures < ' h , H >
158165where
159- H : Sliceable ,
166+ H : Sliceable + ? Sized ,
160167{
161- haystack : H ,
168+ haystack : & ' h H ,
162169 caps : Vec < Option < ( usize , usize ) > > ,
163170}
164171
165- impl < H > Captures < H >
172+ impl < ' h , H > Captures < ' h , H >
166173where
167- H : Sliceable ,
174+ H : Sliceable + ? Sized ,
168175{
169- pub ( crate ) fn new ( haystack : H , caps : Vec < Option < ( usize , usize ) > > ) -> Self {
176+ pub ( crate ) fn new ( haystack : & ' h H , caps : Vec < Option < ( usize , usize ) > > ) -> Self {
170177 Self { haystack, caps }
171178 }
172179
@@ -241,7 +248,7 @@ mod impl_for_regex {
241248 }
242249 }
243250
244- impl Haystack < Regex > for & str {
251+ impl Haystack < Regex > for str {
245252 fn is_match_between ( & self , re : & Regex , from : usize , to : usize ) -> bool {
246253 re. is_match ( & self [ from..to] )
247254 }
0 commit comments