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

Create a version of get_or_insert that doesn't move the key #199

Merged
merged 1 commit into from
Jul 31, 2024

Conversation

lufte
Copy link
Contributor

@lufte lufte commented Jul 13, 2024

get_or_insert is handy but expensive when K is a non-copy type, as it needs to take ownership of the key even when it's already in the cache. This version takes a reference to K and only if it doesn't exist, will it clone it (via ToOwned) and put it in the cache.

If this addition is acceptable, I will create the missing counterparts to get_or_insert_mut, try_get_or_insert, and try_get_or_insert_mut; as well as the documentation for every method.

I'm really not used to writing much "library code" (with lots of Trait requirements) in Rust, nor unsafe code, so I would really appreciate a careful review from you; although it's pretty much a copy of get_or_insert with just a couple of changes.

@lufte
Copy link
Contributor Author

lufte commented Jul 13, 2024

I also accept suggestions on better names for these methods.

@jeromefroe
Copy link
Owner

Changes look good! Mind just adding a test for the method as well?

@lufte
Copy link
Contributor Author

lufte commented Jul 21, 2024

Done. I added the missing methods, and documentation and tests for all of them.

@lufte lufte changed the title [WIP] Create a version of get_or_insert that doesn't move the key Create a version of get_or_insert that doesn't move the key Jul 22, 2024
@jeromefroe
Copy link
Owner

@lufte apologies for the delay getting back to this, mind addressing the issues from CI?

@lufte
Copy link
Contributor Author

lufte commented Jul 30, 2024

No worries. All done, it was just a matter of running cargo fmt.

@jeromefroe
Copy link
Owner

@lufte mind merging master into your branch? I just fixed the issue CI flagged in #200

Four new methods are added:
* get_or_insert_ref
* try_get_or_insert_ref
* get_or_insert_mut_ref
* try_get_or_insert_mut_ref

which are analog to their existing counterparts:
* get_or_insert
* try_get_or_insert
* get_or_insert_mut
* try_get_or_insert_mut

with the difference of accepting a reference to the key instead of an owned object. If the key
doesn't exist in the cache and needs to be moved, only then is it cloned using to_owned() so it can
be owned by the cache. This is useful when cloning the key is expensive.
@lufte
Copy link
Contributor Author

lufte commented Jul 31, 2024

Oops, missed that one. I squashed everything to avoid having some many little commits related to linting and test fixing.

@jeromefroe jeromefroe merged commit 5f69ef1 into jeromefroe:master Jul 31, 2024
4 checks passed
@jeromefroe
Copy link
Owner

Looks great, thanks for the contribution @lufte!

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.

2 participants