Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use From::from fn pointer to convert to boxed errors #1906

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dcechano
Copy link

This PR changes the recommended way to convert to a boxed error Box<dyn Error> for types that impl Error. It changes the map_err(|e| e.into()) to map_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 a Box<CustomError>. I eventually found e.into() and then I realized that means I can use From::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 means From::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!

@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @marioidival (or someone else) some time within the next two weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants