@@ -1147,9 +1147,9 @@ impl<'r, 't> FusedIterator for Matches<'r, 't> {}
11471147/// Replacer describes types that can be used to replace matches in a string.
11481148///
11491149/// In general, users of this crate shouldn't need to implement this trait,
1150- /// since implementations are already provided for `&str` and
1151- /// `FnMut(&Captures) -> String` (or any `FnMut(&Captures) -> T`
1152- /// where `T: AsRef<str>`), which covers most use cases.
1150+ /// since implementations are already provided for `&str` along with other
1151+ /// variants of string types and `FnMut(&Captures) -> String` (or any
1152+ /// `FnMut(&Captures) -> T` where `T: AsRef<str>`), which covers most use cases.
11531153pub trait Replacer {
11541154 /// Appends text to `dst` to replace the current match.
11551155 ///
@@ -1218,10 +1218,55 @@ impl<'a> Replacer for &'a str {
12181218 }
12191219
12201220 fn no_expansion ( & mut self ) -> Option < Cow < str > > {
1221- match find_byte ( b'$' , self . as_bytes ( ) ) {
1222- Some ( _) => None ,
1223- None => Some ( Cow :: Borrowed ( * self ) ) ,
1224- }
1221+ no_expansion ( self )
1222+ }
1223+ }
1224+
1225+ impl < ' a > Replacer for & ' a String {
1226+ fn replace_append ( & mut self , caps : & Captures , dst : & mut String ) {
1227+ self . as_str ( ) . replace_append ( caps, dst)
1228+ }
1229+
1230+ fn no_expansion ( & mut self ) -> Option < Cow < str > > {
1231+ no_expansion ( self )
1232+ }
1233+ }
1234+
1235+ impl Replacer for String {
1236+ fn replace_append ( & mut self , caps : & Captures , dst : & mut String ) {
1237+ self . as_str ( ) . replace_append ( caps, dst)
1238+ }
1239+
1240+ fn no_expansion ( & mut self ) -> Option < Cow < str > > {
1241+ no_expansion ( self )
1242+ }
1243+ }
1244+
1245+ impl < ' a > Replacer for Cow < ' a , str > {
1246+ fn replace_append ( & mut self , caps : & Captures , dst : & mut String ) {
1247+ self . as_ref ( ) . replace_append ( caps, dst)
1248+ }
1249+
1250+ fn no_expansion ( & mut self ) -> Option < Cow < str > > {
1251+ no_expansion ( self )
1252+ }
1253+ }
1254+
1255+ impl < ' a > Replacer for & ' a Cow < ' a , str > {
1256+ fn replace_append ( & mut self , caps : & Captures , dst : & mut String ) {
1257+ self . as_ref ( ) . replace_append ( caps, dst)
1258+ }
1259+
1260+ fn no_expansion ( & mut self ) -> Option < Cow < str > > {
1261+ no_expansion ( self )
1262+ }
1263+ }
1264+
1265+ fn no_expansion < T : AsRef < str > > ( t : & T ) -> Option < Cow < str > > {
1266+ let s = t. as_ref ( ) ;
1267+ match find_byte ( b'$' , s. as_bytes ( ) ) {
1268+ Some ( _) => None ,
1269+ None => Some ( Cow :: Borrowed ( s) ) ,
12251270 }
12261271}
12271272
0 commit comments