-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Proposal
Problem statement
It is often desirable to call Option::get_or_insert_with
with a fallible closure.
Motivating examples or use cases
Simple memoization/caching of fallible operations where failure of the option would be fatal, and thus reserving space to cache the whole Result
would be a waste.
An example would be an expensive directory traversal of a dir that is expected to maintain integrity.
This is useful for a lazy query pattern similar to that used in rustc.
Solution sketch
impl<T> Option<T> {
pub fn get_or_insert_with<F, E>(&mut self, f: F) -> Result<&mut T, E> where F: FnOnce() -> Result<T, E>;
Alternatives
- Use unwinding
- Some other sort of nonlocal control flow for closures.
- Putting this in some sort of extension trait crate analogous to itertools
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.