Skip to content
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

Bind lifetimes of &str returned from Key by the lifetime of 'k rather than the lifetime of the Key struct #648

Merged
merged 2 commits into from
Dec 7, 2024

Conversation

gbbosak
Copy link
Contributor

@gbbosak gbbosak commented Dec 6, 2024

This allows things inside a Visitor to persist these strings for the lifetime of 'k without having to copy them.

src/kv/key.rs Outdated
@@ -57,7 +57,7 @@ impl<'k> fmt::Display for Key<'k> {
}

impl<'k> AsRef<str> for Key<'k> {
fn as_ref(&self) -> &str {
fn as_ref(&self) -> &'k str {
Copy link
Member

Choose a reason for hiding this comment

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

This has no effect unless you add #[refine] (https://rust-lang.github.io/rfcs/3245-refined-impls.html).

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, it almost seems weird this compiles at all 👀 I hadn't seen #[refine] before. Thanks for pointing it out!

@Thomasdezeeuw Thomasdezeeuw merged commit 134d252 into rust-lang:master Dec 7, 2024
14 checks passed
@Thomasdezeeuw
Copy link
Collaborator

Thanks @gbbosak

@KodrAus
Copy link
Contributor

KodrAus commented Dec 8, 2024

I should've left a comment on the type suggesting why it was that way, but we intentionally hadn't used 'k here so we were free to change the internals of Key to use something like Cow<'k, str> to support key-value sources that need to buffer their keys. I'm ok with tightening it up and not doing that if we decide it's not that important, but worth calling out that's what we lose here.

@Thomasdezeeuw
Copy link
Collaborator

@KodrAus do we want to revert the pr to keep that option open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants