Skip to content

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

Closed

Conversation

birkenfeld
Copy link
Contributor

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:

  • is this a useful addition?
  • is the name ok?
  • is the feature name ok?

@rust-highfive
Copy link
Contributor

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.
@birkenfeld birkenfeld force-pushed the entry_or_insert_with_result branch from c566516 to 0058269 Compare April 21, 2016 07:19
@birkenfeld
Copy link
Contributor Author

birkenfeld commented Apr 21, 2016

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.)

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Apr 21, 2016
@aturon
Copy link
Member

aturon commented Apr 22, 2016

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 Result-based variant.

We'll discuss this in the next libs team meeting.

@bors
Copy link
Collaborator

bors commented Apr 30, 2016

☔ The latest upstream changes (presumably #33148) made this pull request unmergeable. Please resolve the merge conflicts.

@sgrif
Copy link
Contributor

sgrif commented Apr 30, 2016

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 Result. I do think it is more common with the entry API though, as a map is much more likely to be used for memoization, and you likely don't want to memoize failure.

This seems like a good addition, but I don't think it makes sense to change it to take the key, for consistency with or_insert_with (Though if we were pre-1.0 I would argue that or_insert should pass the key to the closure as well). This means we wouldn't be able to use it for the referenced case, but I don't think it's terribly common to have a case where:

  • The map is being used as a cache
  • Errors need to be returned outright but not cached
  • The key is used to calculate the value

Also my vote for a name would be or_insert_if_ok instead of or_insert_with_result

@aturon
Copy link
Member

aturon commented May 6, 2016

We didn't quite get to this at this week's libs meeting, but should next week.

@alexcrichton
Copy link
Member

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 or_insert_with method) would likely require HKT of some form. Today we basically have to decide if closure-taking APIs handle results (like this) or not (like it does today). We're not comfortable starting a convention just yet of having dual APIs for this (as Result isn't the only kind of error), so for now so long as the functionality is available via some other means we're gonna stick with the one-closure APIs we have today.

Thanks for the PR though @birkenfeld!

@birkenfeld birkenfeld deleted the entry_or_insert_with_result branch May 12, 2016 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants