Create a version of get_or_insert that doesn't move the key #199
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 toK
and only if it doesn't exist, will it clone it (viaToOwned
) 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
, andtry_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.