rust-lang / regex

An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.
https://docs.rs/regex
Apache License 2.0
3.52k stars 440 forks source link

the Replacer trait should be parameterized over a lifetime #777

Open BurntSushi opened 3 years ago

BurntSushi commented 3 years ago

It turns out that the Replacer trait is a bit more restrictive than it should be. For the closure impl of Replacer, it requires that the closure work for all possible lifetimes. But it's far more useful to be able to provide a closure that works on a specific lifetime. This makes it possible to do things like return a Cow<str> from the closure to avoid an extra alloc.

This problem was originally brought up in #775 by @tpoliaw.

That lead to @sgrif submitting #776 that changes the Replacer trait to be parameterized over a lifetime.

Unfortunately, it's a breaking change, so this will have to wait for regex 2.0.0.

N.B. This is generally an ergonomic problem I think. It is always possible to implement Replacer oneself and do something more optimal. @tpoliaw also provided a work-around using a closure as well, although it's a bit clunky (but perhaps not as clunky as implementing Repalcer).

JanBeh commented 1 year ago

See also #1048, which I think could solve this issue.