Add out of the box support for mapping custom error types that implement std::error::Error' to
Box<dyn Error>`
#520
Labels
std::error::Error' to
Box<dyn Error>`
#520
Proposal
Problem statement
Often you need to map one
Result::Err
to another. This is usually straight forward but when you want to return the result as aResult<_, Box<dyn Error>>
it can become cumbersome. One issue is that the compiler makes you go in circles figuring out how to do it (map_err(???)
). And then when you do figure out the way the compiler likes it becomes unwieldy to constantly write.map_err(|e| e.into())
everywhere.Motivating examples or use cases
Often you have to use code that returns a
Result<_, ConcreteError>
whereConcreteError
implementsstd::error::Error
. If you are in a method that returns aResult<_, Box<dyn Error>>
and want to propagate these errors up the stack asBox<dyn Error>
every call to the fallible function requires the error to be mapped to aBox<dyn Error>
.This can be improved for 2 reasons.
Many people who have to do this end up fighting the compiler because the naive
map_err(Box::new)?
does not work. So they go in circles trying to convince the compiler to convert aBox<ConcreteError>
to aBox<dyn Error>
.It is cumbersome boilerplate that often exists solely to satisfy the compiler. Constantly calling
map_err(|e| e.into())
can feel unidiomatic. You feel like there must be a better way. But there isn't; this is the idiomatic way. There isFrom::from
which many don't appear to know works but even if you do know about it, it still requires constantly typingmap_err(From::from)
.Solution sketch
My solution is simply adding a method to the
core::result::Result
type that looks something like this:Alternatives
There are not too many alternatives I can think of other than continuing to require users to write
map_err(...)?
.One possible alternative worth considering is to do something like:
This would allow users to just call
into
on the result instead of mapping the error manually. Perhaps this is a better idea because it is even more concise that the solution I suggested above.Links and related work
This is a link to the Rust by Example book. Note that they are suggesting
map_err(|e| e.into())
instead of the more idiomaticmap_err(From::from)
, which goes to show that it this is something that the community could really use.This is a link to the Rust users forum asking how to map to a boxed error. It is from 2019! We have been fighting the compiler about this for a while!
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):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: