Skip to content

Commit 092b770

Browse files
committed
fixup! More generic impl of Replacer for closures
Less generic implementation, which results in a better documented API.
1 parent b6a612d commit 092b770

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/regex/string.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,21 +2371,22 @@ impl<'c, 'h> ExactSizeIterator for SubCaptureMatches<'c, 'h> {}
23712371

23722372
impl<'c, 'h> core::iter::FusedIterator for SubCaptureMatches<'c, 'h> {}
23732373

2374-
/// Trait alias for `FnMut` with one argument, which allows adding a bound
2375-
/// without specifying the closure's return type.
2376-
pub trait GenericFnMut1Arg<Arg>
2374+
/// If a closure implements this for all `'a`, then it also implements
2375+
/// [`Replacer`].
2376+
pub trait ReplacerClosure<'a>
23772377
where
2378-
Self: FnMut(Arg) -> <Self as GenericFnMut1Arg<Arg>>::Output,
2378+
Self: FnMut(&'a Captures<'a>) -> <Self as ReplacerClosure>::Output,
23792379
{
2380-
/// Return type of the closure.
2381-
type Output;
2380+
/// Return type of the closure (may depend on lifetime `'a`).
2381+
type Output: AsRef<str>;
23822382
}
23832383

2384-
impl<T: ?Sized, Arg, Ret> GenericFnMut1Arg<Arg> for T
2384+
impl<'a, F: ?Sized, O> ReplacerClosure<'a> for F
23852385
where
2386-
T: FnMut(Arg) -> Ret,
2386+
F: FnMut(&'a Captures<'a>) -> O,
2387+
O: AsRef<str>,
23872388
{
2388-
type Output = Ret;
2389+
type Output = O;
23892390
}
23902391

23912392
/// A trait for types that can be used to replace matches in a haystack.
@@ -2518,11 +2519,7 @@ impl<'a> Replacer for &'a Cow<'a, str> {
25182519
}
25192520
}
25202521

2521-
impl<F> Replacer for F
2522-
where
2523-
F: for<'a> GenericFnMut1Arg<&'a Captures<'a>>,
2524-
for<'a> <F as GenericFnMut1Arg<&'a Captures<'a>>>::Output: AsRef<str>,
2525-
{
2522+
impl<F: for<'a> ReplacerClosure<'a>> Replacer for F {
25262523
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
25272524
dst.push_str((*self)(caps).as_ref());
25282525
}

0 commit comments

Comments
 (0)