@@ -82,7 +82,9 @@ pub trait Haystack: Writable + fmt::Debug + PartialEq + Eq + Sync {
8282 fn max_len ( & self ) -> usize ;
8383}
8484
85+ /// Something that can be written to to a given [io::Write].
8586pub trait Writable {
87+ /// Writes `self` to the given [io::Write], returning the number of bytes written.
8688 fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
8789 where
8890 W : io:: Write ;
@@ -109,6 +111,69 @@ impl Writable for str {
109111 }
110112}
111113
114+ impl Haystack for String {
115+ type Slice = str ;
116+
117+ fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice {
118+ & self [ from..to]
119+ }
120+
121+ fn max_len ( & self ) -> usize {
122+ self . len ( )
123+ }
124+ }
125+
126+ impl Writable for String {
127+ fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
128+ where
129+ W : io:: Write ,
130+ {
131+ w. write_all ( self . as_bytes ( ) ) . map ( |_| self . len ( ) )
132+ }
133+ }
134+
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 >
149+ 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 ] ;
158+
159+ fn slice ( & self , from : usize , to : usize ) -> & Self :: Slice {
160+ & self [ from..to]
161+ }
162+
163+ fn max_len ( & self ) -> usize {
164+ self . len ( )
165+ }
166+ }
167+
168+ impl Writable for Vec < u8 > {
169+ fn write_to < W > ( & self , w : & mut W ) -> io:: Result < usize >
170+ where
171+ W : io:: Write ,
172+ {
173+ w. write_all ( self ) . map ( |_| self . len ( ) )
174+ }
175+ }
176+
112177/// Represents the capture group positions for a single [Re] match only in terms of byte offsets
113178/// into the original haystack.
114179///
0 commit comments