-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Map entry API: add Entry::or_insert_with_result() #33126
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
Map entry API: add Entry::or_insert_with_result() #33126
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
Takes a default function that returns a Result, and bails out on Err cases. Use case: caches where producing the value to cache can fail.
c566516
to
0058269
Compare
Hm, another question: since we're introducing a new type of closure here anyway, should it receive a borrow of the key? (Thinking about cases like @sgrif's https://gist.github.com/sgrif/68cbba9a87084da3a2e8c42a12e7d6cf here.) |
cc @rust-lang/libs I do think these kinds of additions can be useful, but we don't currently have a great convention for them, and of course in the long run it would be sad if every closure-taking function ends up having a We'll discuss this in the next libs team meeting. |
☔ The latest upstream changes (presumably #33148) made this pull request unmergeable. Please resolve the merge conflicts. |
Just to chime in since some code from Diesel got referenced in the use case. This is what the code would look like with/without this feature (assuming the closure did in fact take the key as an argument). https://gist.github.com/sgrif/95bdbd302124010a0e4dbc39862313f2 I agree that we don't want every closure-taking function to have a special case for This seems like a good addition, but I don't think it makes sense to change it to take the key, for consistency with
Also my vote for a name would be |
We didn't quite get to this at this week's libs meeting, but should next week. |
The libs team discussed this PR at triage the other day and the conclusion was that we probably don't want to merge this at this time. Handling this in the "most clean" fashion (e.g. with the current Thanks for the PR though @birkenfeld! |
Takes a default function that returns a Result, and bails out
on Err cases. Use case: caches where producing the value
to cache can fail.
To clarify: