Skip to content

Instantly share code, notes, and snippets.

@x7c1
Created November 8, 2019 10:58
Show Gist options
  • Select an option

  • Save x7c1/bab9e18aac847ff128568057e8c5790f to your computer and use it in GitHub Desktop.

Select an option

Save x7c1/bab9e18aac847ff128568057e8c5790f to your computer and use it in GitHub Desktop.
`Option<T> -> Result<Option<U>, E>`
trait Hoge<T> {
fn map_result<U, E, F>(self, f: F) -> Result<Option<U>, E>
where
F: FnOnce(T) -> Result<U, E>;
}
impl<T> Hoge<T> for Option<T> {
fn map_result<U, E, F>(self, f: F) -> Result<Option<U>, E>
where
F: FnOnce(T) -> Result<U, E>,
{
match self {
Some(x) => Ok(Some(f(x)?)),
None => Ok(None),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment