//! Extensions to core types

pub trait OptionExt<T> {
    fn get_or_try_insert_with<E, F: FnOnce() -> Result<T, E>>(
        &mut self,
        init: F,
    ) -> Result<&mut T, E>;
}

impl<T> OptionExt<T> for Option<T> {
    fn get_or_try_insert_with<E, F: FnOnce() -> Result<T, E>>(
        &mut self,
        init: F,
    ) -> Result<&mut T, E> {
        match self {
            Self::None => Ok(self.insert(init()?)),
            Self::Some(value) => Ok(value),
        }
    }
}
