Skip to content

Commit

Permalink
fixup! More generic impl of Replacer for closures
Browse files Browse the repository at this point in the history
Refactored code to be able to use two different lifetimes `'a` and `'b`
for the `&'a Captures<'b>` argument while allowing the return type to
depend on either `'a` or `'b`.
  • Loading branch information
JanBeh committed Jul 22, 2023
1 parent 664a0f2 commit d89b31a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
26 changes: 13 additions & 13 deletions src/regex/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2374,18 +2374,18 @@ impl<'c, 'h> core::iter::FusedIterator for SubCaptureMatches<'c, 'h> {}
/// Contains helper trait for blanket implementation for [`Replacer`].
mod replacer_closure {
use super::*;
/// If a closure implements this for all `'a`, then it also implements
/// [`Replacer`].
pub trait ReplacerClosure<'a>
/// If a closure implements this for all `&'a Captures<'b>`, then it also
/// implements [`Replacer`].
pub trait ReplacerClosure<Arg>
where
Self: FnMut(&'a Captures<'a>) -> <Self as ReplacerClosure<'a>>::Output,
Self: FnMut(Arg) -> <Self as ReplacerClosure<Arg>>::Output,
{
/// Return type of the closure (may depend on lifetime `'a`).
/// Return type of the closure (may depend on lifetime `'a` or `'b`).
type Output: AsRef<str>;
}
impl<'a, F: ?Sized, O> ReplacerClosure<'a> for F
impl<'a, 'b, F, O> ReplacerClosure<&'a Captures<'b>> for F
where
F: FnMut(&'a Captures<'a>) -> O,
F: ?Sized + FnMut(&'a Captures<'b>) -> O,
O: AsRef<str>,
{
type Output = O;
Expand Down Expand Up @@ -2428,8 +2428,8 @@ use replacer_closure::*;
///
/// # Implementation by closures
///
/// Closures that take an argument of type `&'a Captures<'b>` for any `'a` and
/// `'b: 'a` and which return a type `T: AsRef<str>` (that may depend on `'a`
/// Closures that take an argument of type `&'a Captures<'b>` (for any `'a`
/// and `'b`) and which return a type `T: AsRef<str>` (that may depend on `'a`
/// or `'b`) implement the `Replacer` trait through a [blanket implementation].
///
/// [blanket implementation]: Self#impl-Replacer-for-F
Expand Down Expand Up @@ -2575,18 +2575,18 @@ impl<'a> Replacer for &'a Cow<'a, str> {
/// Blanket implementation of `Replacer` for closures.
///
/// This implementation is basically the following, except that the return type
/// `T` may optionally depend on lifetime `'a`.
/// `T` may optionally depend on the lifetimes `'a` and `'b`.
///
/// ```ignore
/// impl<F, T> Replacer for F
/// where
/// F: for<'a> FnMut(&'a Captures<'a>) -> T,
/// T: AsRef<str>, // `T` may also depend on `'a`, which cannot be expressed easily
/// F: for<'a, 'b> FnMut(&'a Captures<'b>) -> T,
/// T: AsRef<str>, // `T` may depend on `'a` or `'b`, which can't be expressed easily
/// {
/// /* … */
/// }
/// ```
impl<F: for<'a> ReplacerClosure<'a>> Replacer for F {
impl<F: for<'a, 'b> ReplacerClosure<&'a Captures<'b>>> Replacer for F {
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
dst.push_str((*self)(caps).as_ref());
}
Expand Down
16 changes: 0 additions & 16 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,4 @@ mod replacer_closure_lifetimes {
);
assert_eq!(s, "x");
}
// Additionally demand that its sufficient if the closure accepts a single
// lifetime `'u` which is used both for the reference to and the lifetime
// argument of the `Captures` argument. Note that `Captures<'u>` is
// covariant over `'u`.
#[test]
fn unified_lifetime() {
fn coerce<F: for<'u> FnMut(&'u Captures<'u>) -> Cow<'u, str>>(
f: F,
) -> F {
f
}
let s = Regex::new("x")
.unwrap()
.replace_all("x", coerce(|caps| Cow::Borrowed(&caps[0])));
assert_eq!(s, "x");
}
}

0 comments on commit d89b31a

Please sign in to comment.