Skip to content

Commit 43222f2

Browse files
committed
fixup! More generic impl of Replacer for closures
Do not use closure lifetime binder but helper function for coercion in documentation test.
1 parent 4451505 commit 43222f2

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

src/regex/string.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,47 +2444,31 @@ use replacer_closure::*;
24442444
/// assert_eq!(result, "[number with 4 digits],[number with 5 digits]");
24452445
/// ```
24462446
///
2447-
/// Note that the return type of the closure may depend on the lifetime of the
2448-
/// reference that is passed as an argument to the closure. This requires the
2449-
/// closure to be a function, unless [closure lifetime binders] are being used:
2447+
/// The return type of the closure may depend on the lifetime of the reference
2448+
/// that is passed as an argument to the closure. Unless [closure lifetime
2449+
/// binders] are being used, the correct type of the closure must be known to
2450+
/// the compiler, e.g. by coercing it through a helper function:
24502451
///
24512452
/// [closure lifetime binders]: https://rust-lang.github.io/rfcs/3216-closure-lifetime-binder.html
2452-
/// [`Cow`]: std::borrow::Cow
24532453
///
24542454
/// ```
24552455
/// use regex::{Captures, Regex, Replacer};
24562456
/// use std::borrow::Cow;
24572457
///
2458-
/// let re = Regex::new(r"[0-9]+").unwrap();
2459-
/// fn prepend_odd<'a>(caps: &'a Captures<'_>) -> Cow<'a, str> {
2460-
/// if caps[0].len() % 2 == 1 {
2461-
/// Cow::Owned(format!("0{}", &caps[0]))
2462-
/// } else {
2463-
/// Cow::Borrowed(&caps[0])
2464-
/// }
2458+
/// fn coerce<F: for<'a> FnMut(&'a Captures<'_>) -> Cow<'a, str>>(f: F) -> F {
2459+
/// f
24652460
/// }
2466-
/// let result = re.replace_all("1234,12345", prepend_odd);
2467-
/// assert_eq!(result, "1234,012345");
2468-
/// ```
2469-
///
2470-
/// The same example using closure lifetime binders:
2471-
///
2472-
/// ```ignore
2473-
/// #![feature(closure_lifetime_binder)]
2474-
///
2475-
/// use regex::{Captures, Regex, Replacer};
2476-
/// use std::borrow::Cow;
24772461
///
24782462
/// let re = Regex::new(r"[0-9]+").unwrap();
24792463
/// let result = re.replace_all(
24802464
/// "1234,12345",
2481-
/// for<'a, 'b> |caps: &'a Captures<'b>| -> Cow<'a, str> {
2465+
/// coerce(|caps| {
24822466
/// if caps[0].len() % 2 == 1 {
24832467
/// Cow::Owned(format!("0{}", &caps[0]))
24842468
/// } else {
24852469
/// Cow::Borrowed(&caps[0])
24862470
/// }
2487-
/// },
2471+
/// }),
24882472
/// );
24892473
/// assert_eq!(result, "1234,012345");
24902474
/// ```

0 commit comments

Comments
 (0)