Result has an map_err function that allows you to map Result<T, E> to a Result<T, F> by applying a function to the error value.
For types that implement FromError, the most natural function to apply would just be FromError::from_error, leading to something like the following:
foo() .map_err(FromError::from_error) .and_then(|val| { val.do_something().map_err(FromError::from_err) }) It seems that for this kind of use case, there should be a less cumbersome method defined on Result<T, E> where E: Error that calls FromError::from_error less verbosely, something like the following but with a better name:
foo() .wrap_err() .and_then(|val| { val.do_something().wrap_err() }) This would just be equivalent to .map_err(FromError::from_error), simply shorter and more convenient for when you're doing this kind of method chaining.
Is there something like this defined anywhere? I could not find it, though I'm not sure if I was looking in all of the right places in the documentation.
This question was inspired my my answer to this one, in which I described both FromError for use with try! but realized it didn't help with the method-chaining style being used there.
?postfix operator: github.com/rust-lang/rust/pull/23040try!, which interrupts control flow to return if you see an error value, while thewrap_errmethod that I describe propagates the error through the chained method calls. That may be what is more useful in a wider variety of cases, but there are cases where getting aResultfrom chained method calls without interrupting your control flow may also be useful.