Skip to content

Rust nightly removed the lifetime from Pattern #1219

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

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub struct RegexSearcher<'r, 't> {
next_match: Option<(usize, usize)>,
}

impl<'r, 't> Pattern<'t> for &'r Regex {
type Searcher = RegexSearcher<'r, 't>;
impl<'r> Pattern for &'r Regex {
type Searcher<'t> = RegexSearcher<'r, 't>;

fn into_searcher(self, haystack: &'t str) -> RegexSearcher<'r, 't> {
fn into_searcher(self, haystack: &str) -> RegexSearcher<'r, '_> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be?

fn into_searcher(self, haystack: &'t str) -> RegexSearcher<'r, 't> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lifetime setup is also used by the various Pattern impls in the standard library:
rust-lang/rust@772315d

Your version doesn't compile directly, the first suggestion below is exactly what I have implemented now (but with 't being elided) and the second suggestion is what was used before my PR and now fails to compile.

error[E0261]: use of undeclared lifetime name `'t`
  --> src/pattern.rs:16:68
   |
16 |     fn into_searcher(self, haystack: &'t str) -> RegexSearcher<'r, 't> {
   |                                                                    ^^ undeclared lifetime
   |
help: consider introducing lifetime `'t` here
   |
16 |     fn into_searcher<'t>(self, haystack: &'t str) -> RegexSearcher<'r, 't> {
   |                     ++++
help: consider introducing lifetime `'t` here
   |
13 | impl<'t, 'r> Pattern for &'r Regex {
   |      +++

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the problem I have here is how does the RegexSearcher connect its lifetime to the haystack? I must be missing something.

Copy link
Contributor Author

@mischnic mischnic Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I wrote here is equivalent to this, which is how the &str lifetime flows into the RegexSearcher lifetime (I can commit that instead if you want)

fn into_searcher<'h>(self, haystack: &'h str) -> RegexSearcher<'r, 'h>

Is that what you meant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooo, yes, I get it now. Yes, please do write that. I'm usually fine with lifetime elision, but I think it actually helps a lot here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right right. This all makes sense now. Got it. I was confused myself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect 😄
I've added back that explicit lifetime

RegexSearcher {
haystack,
it: self.find_iter(haystack),
Expand Down