Skip to content

Add get_or_insert_with_key#229

Merged
jeromefroe merged 2 commits intojeromefroe:masterfrom
pikatos:get_or_insert_with_key
Apr 13, 2026
Merged

Add get_or_insert_with_key#229
jeromefroe merged 2 commits intojeromefroe:masterfrom
pikatos:get_or_insert_with_key

Conversation

@pikatos
Copy link
Copy Markdown

@pikatos pikatos commented Apr 13, 2026

Add get_or_insert_with_key variant. If the key is needed to create a new value to populate the cache, using only get_or_insert implies the use of a cloneable key to be able to capture the key in the provided closure. Wrapping the key with Rc and using get_or_insert_ref just for that seems overkill.

let key = Rc::new("mydomain.com".to_owned());
cache.get_or_insert_ref(&key, domain_cert_generator(key.clone()));

becomes:

cache.get_or_insert_with_key("mydomain.com".to_owned(), generate_domain_cert);

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the LruCache API with *_or_insert_with_key variants that pass &K into the value-construction closure, reducing the need for cloning keys solely to capture them in FnOnce.

Changes:

  • Added get_or_insert_with_key, try_get_or_insert_with_key, get_or_insert_mut_with_key, and try_get_or_insert_mut_with_key.
  • Refactored existing get_or_insert* / try_get_or_insert* methods to delegate to the new *_with_key variants.
  • Added unit tests covering the new APIs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib.rs Outdated
Comment thread src/lib.rs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@jeromefroe jeromefroe merged commit 16e161d into jeromefroe:master Apr 13, 2026
4 checks passed
@jeromefroe
Copy link
Copy Markdown
Owner

Thank you for the contribution @pikatos

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.

3 participants