Use From::from
fn pointer to convert to boxed errors
#1906
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR changes the recommended way to convert to a boxed error
Box<dyn Error>
for types that implError
. It changes themap_err(|e| e.into())
tomap_err(From::from)
which I believe is much more concise and idiomatic (and satisfying!).Motivation
Like a lot of people I bashed my head on this problem of getting the compiler to accept the mapped error as a
Box<dyn Error>
and not aBox<CustomError>
. I eventually founde.into()
and then I realized that means I can useFrom::from
.I have done some searching on the StackOverflow and the Rust user forums to see how everyone else is solving this problem and I have yet to find an example of someone recommending
From::from
fn pointer. Everyone just suggests|e| e.into()
even though the former does the same thing more concisely.I think it is just an oversight and it doesn't occur to the person helping that if
e.into
works, that meansFrom::from(e)
necessarily works as well. So I figured I would share this solution in the official example book if it is indeed an improvement over the current recommended method.Any way, let know what you guys think!