1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Similar to Option<T>.get_or_insert_with, for a function that returns a result.
pub trait OptionInsertWithResult<T> {
    fn get_or_insert_with_result<F, E>(&mut self, f: F) -> Result<&mut T, E>
    where
        F: FnOnce() -> Result<T, E>;
}

impl<T> OptionInsertWithResult<T> for Option<T> {
    fn get_or_insert_with_result<F, E>(&mut self, f: F) -> Result<&mut T, E>
    where
        F: FnOnce() -> Result<T, E>,
    {
        if self.is_none() {
            *self = Some(f()?);
        }
        Ok(self.as_mut().unwrap())
    }
}