Skip to content

Commit 53385d7

Browse files
elidupreeBurntSushi
authored andcommitted
api: generalize Replacer impls
This causes the `Replacer` impls for closures to accept functions that return any AsRef<str>/AsRef<[u8]> instead of String/Vec<u8> specifically. PR #509
1 parent 18a71d0 commit 53385d7

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

src/re_bytes.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,8 @@ impl<'c, 't> Iterator for SubCaptureMatches<'c, 't> {
10651065
///
10661066
/// In general, users of this crate shouldn't need to implement this trait,
10671067
/// since implementations are already provided for `&[u8]` and
1068-
/// `FnMut(&Captures) -> Vec<u8>`, which covers most use cases.
1068+
/// `FnMut(&Captures) -> Vec<u8>` (or any `FnMut(&Captures) -> T`
1069+
/// where `T: AsRef<[u8]>`), which covers most use cases.
10691070
pub trait Replacer {
10701071
/// Appends text to `dst` to replace the current match.
10711072
///
@@ -1141,9 +1142,9 @@ impl<'a> Replacer for &'a [u8] {
11411142
}
11421143
}
11431144

1144-
impl<F> Replacer for F where F: FnMut(&Captures) -> Vec<u8> {
1145+
impl<F, T> Replacer for F where F: FnMut(&Captures) -> T, T: AsRef<[u8]> {
11451146
fn replace_append(&mut self, caps: &Captures, dst: &mut Vec<u8>) {
1146-
dst.extend_from_slice(&(*self)(caps));
1147+
dst.extend_from_slice((*self)(caps).as_ref());
11471148
}
11481149
}
11491150

src/re_unicode.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,8 @@ impl<'r, 't> Iterator for Matches<'r, 't> {
11071107
///
11081108
/// In general, users of this crate shouldn't need to implement this trait,
11091109
/// since implementations are already provided for `&str` and
1110-
/// `FnMut(&Captures) -> String`, which covers most use cases.
1110+
/// `FnMut(&Captures) -> String` (or any `FnMut(&Captures) -> T`
1111+
/// where `T: AsRef<str>`), which covers most use cases.
11111112
pub trait Replacer {
11121113
/// Appends text to `dst` to replace the current match.
11131114
///
@@ -1183,9 +1184,9 @@ impl<'a> Replacer for &'a str {
11831184
}
11841185
}
11851186

1186-
impl<F> Replacer for F where F: FnMut(&Captures) -> String {
1187+
impl<F, T> Replacer for F where F: FnMut(&Captures) -> T, T: AsRef<str> {
11871188
fn replace_append(&mut self, caps: &Captures, dst: &mut String) {
1188-
dst.push_str(&(*self)(caps));
1189+
dst.push_str((*self)(caps).as_ref());
11891190
}
11901191
}
11911192

tests/macros_bytes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
macro_rules! text { ($text:expr) => { $text.as_bytes() } }
33
macro_rules! t { ($re:expr) => { text!($re) } }
44
macro_rules! match_text { ($text:expr) => { $text.as_bytes() } }
5+
macro_rules! use_ { ($($path: tt)*) => { use regex::bytes::$($path)*; } }
56

67
macro_rules! bytes { ($text:expr) => { $text } }
78

tests/macros_str.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
macro_rules! text { ($text:expr) => { $text } }
33
macro_rules! t { ($text:expr) => { text!($text) } }
44
macro_rules! match_text { ($text:expr) => { $text.as_str() } }
5+
macro_rules! use_ { ($($path: tt)*) => { use regex::$($path)*; } }
56

67
macro_rules! no_expand {
78
($text:expr) => {{

tests/replace.rs

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ replace!(no_expand1, replace,
3333
r"(\S+)\s+(\S+)", "w1 w2", no_expand!("$2 $1"), "$2 $1");
3434
replace!(no_expand2, replace,
3535
r"(\S+)\s+(\S+)", "w1 w2", no_expand!("$$1"), "$$1");
36+
use_!(Captures);
37+
replace!(closure_returning_reference, replace, r"(\d+)", "age: 26",
38+
| captures: &Captures | &match_text!(captures.get(1).unwrap())[0..1], "age: 2");
39+
replace!(closure_returning_value, replace, r"\d+", "age: 26",
40+
| _captures: &Captures | t!("Z").to_owned(), "age: Z");
41+
3642

3743
// See https://github.com/rust-lang/regex/issues/314
3844
replace!(match_at_start_replace_with_empty, replace_all, r"foo", "foobar", t!(""), "bar");

0 commit comments

Comments
 (0)