Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a lifetime to Replacer #776

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ impl Regex {
/// assert_eq!(result, "$2 $last");
/// # }
/// ```
pub fn replace<'t, R: Replacer>(
pub fn replace<'t, R: Replacer<'t>>(
&self,
text: &'t str,
rep: R,
Expand All @@ -517,7 +517,7 @@ impl Regex {
///
/// See the documentation for `replace` for details on how to access
/// capturing group matches in the replacement string.
pub fn replace_all<'t, R: Replacer>(
pub fn replace_all<'t, R: Replacer<'t>>(
&self,
text: &'t str,
rep: R,
Expand All @@ -531,7 +531,7 @@ impl Regex {
///
/// See the documentation for `replace` for details on how to access
/// capturing group matches in the replacement string.
pub fn replacen<'t, R: Replacer>(
pub fn replacen<'t, R: Replacer<'t>>(
&self,
text: &'t str,
limit: usize,
Expand Down Expand Up @@ -1149,15 +1149,15 @@ impl<'r, 't> FusedIterator for Matches<'r, 't> {}
/// since implementations are already provided for `&str` along with other
/// variants of string types and `FnMut(&Captures) -> String` (or any
/// `FnMut(&Captures) -> T` where `T: AsRef<str>`), which covers most use cases.
pub trait Replacer {
pub trait Replacer<'a> {
/// Appends text to `dst` to replace the current match.
///
/// The current match is represented by `caps`, which is guaranteed to
/// have a match at capture group `0`.
///
/// For example, a no-op replacement would be
/// `dst.push_str(caps.get(0).unwrap().as_str())`.
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String);
fn replace_append(&mut self, caps: &Captures<'a>, dst: &mut String);

/// Return a fixed unchanging replacement string.
///
Expand Down Expand Up @@ -1202,16 +1202,16 @@ pub trait Replacer {
#[derive(Debug)]
pub struct ReplacerRef<'a, R: ?Sized>(&'a mut R);

impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R> {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
impl<'a, 'b, R: Replacer<'b> + ?Sized + 'a> Replacer<'b> for ReplacerRef<'a, R> {
fn replace_append(&mut self, caps: &Captures<'b>, dst: &mut String) {
self.0.replace_append(caps, dst)
}
fn no_expansion(&mut self) -> Option<Cow<'_, str>> {
self.0.no_expansion()
}
}

impl<'a> Replacer for &'a str {
impl<'a> Replacer<'_> for &'a str {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
caps.expand(*self, dst);
}
Expand All @@ -1221,7 +1221,7 @@ impl<'a> Replacer for &'a str {
}
}

impl<'a> Replacer for &'a String {
impl<'a> Replacer<'_> for &'a String {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_str().replace_append(caps, dst)
}
Expand All @@ -1231,7 +1231,7 @@ impl<'a> Replacer for &'a String {
}
}

impl Replacer for String {
impl Replacer<'_> for String {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_str().replace_append(caps, dst)
}
Expand All @@ -1241,7 +1241,7 @@ impl Replacer for String {
}
}

impl<'a> Replacer for Cow<'a, str> {
impl<'a> Replacer<'_> for Cow<'a, str> {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_ref().replace_append(caps, dst)
}
Expand All @@ -1251,7 +1251,7 @@ impl<'a> Replacer for Cow<'a, str> {
}
}

impl<'a> Replacer for &'a Cow<'a, str> {
impl<'a> Replacer<'_> for &'a Cow<'a, str> {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
self.as_ref().replace_append(caps, dst)
}
Expand All @@ -1269,12 +1269,12 @@ fn no_expansion<T: AsRef<str>>(t: &T) -> Option<Cow<'_, str>> {
}
}

impl<F, T> Replacer for F
impl<'a, F, T> Replacer<'a> for F
where
F: FnMut(&Captures<'_>) -> T,
F: FnMut(&Captures<'a>) -> T,
T: AsRef<str>,
{
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
fn replace_append(&mut self, caps: &Captures<'a>, dst: &mut String) {
dst.push_str((*self)(caps).as_ref());
}
}
Expand All @@ -1290,7 +1290,7 @@ where
#[derive(Clone, Debug)]
pub struct NoExpand<'t>(pub &'t str);

impl<'t> Replacer for NoExpand<'t> {
impl<'t> Replacer<'_> for NoExpand<'t> {
fn replace_append(&mut self, _: &Captures<'_>, dst: &mut String) {
dst.push_str(self.0);
}
Expand Down