-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add new API try_downcast_inner
to std::io::Error
#57
Comments
try_downgrade_inner
to std::io::Error
try_downcast_inner
to std::io::Error
This is incredibly unfortunate and seems like quite a strong justification for this proposed API. Seconded @rustbot label +initial-comment-period My only concern is to make sure that whatever name we pick is as consistent and intuitive as possible. I'm looking at https://doc.rust-lang.org/stable/std/error/trait.Error.html#method.downcast and can't help but think that this is effectively the same API on We can still disambiguate between these two cases with an extra call to |
How about
I think it would be too hard to use and it doesn't really yield any benefit. |
I fully agree, but I brought that up because I'm questioning the value of the |
Because it can only downcast if there is an inner error. Although I am ok with |
Yea, but we're not differentiating between failures that are due to mismatched type vs failures due to a lack of an inner error, that was the point of my original comment on the |
@yaahc Yeah I think you are right here. I am OK to change the proposal and update the PR. BTW, do w need more feedbacks from people before changing the name? |
Nope, since this is a nightly only ACP only one libs-api team member is needed to approve or give feedback on the API. Other people can of course give feedback and we will wait the 10 days since I seconded the proposal before approving the PR to give people a chance to participate if they want, but you should be good to update the PR whenever. |
Thanks for the explanation, I will update the PR later today. |
@yaahc I've updated the PR. |
…owngrade_inner, r=yaahc Add new unstable API `downcast` to `std::io::Error` rust-lang/libs-team#57 Signed-off-by: Jiahao XU <[email protected]>
…owngrade_inner, r=yaahc Add new unstable API `downcast` to `std::io::Error` rust-lang/libs-team#57 Signed-off-by: Jiahao XU <[email protected]>
…inner, r=yaahc Add new unstable API `downcast` to `std::io::Error` rust-lang/libs-team#57 Signed-off-by: Jiahao XU <[email protected]>
Proposal
Add new API
try_downcast_inner
tostd::io::Error
:Problem statement
Existing APIs requires two separate calls to obtain the raw os error or the inner error and they both return
Option
.There's no way to avoid
unwrap
/expect
if we want to cover every possible corner case here, e.g., both of them somehow returnsNone
.This is impossible without
expect
/unwrap
becausestd::io::Error::into_inner
takes the error by value and returnsOption<Box<dyn Error + Send + Sync>>
instead ofResult<Box<dyn Error + Send + Sync>, Self>
.Currently, we would have to do this to downcast the inner error:
(Adapted from cargo-bins/cargo-binstall#180)
This has only 1
expect
/unwrap
, but it usesstd::io::Error::new
, which usesBox::new
internally.Another way to implement it would be:
This saves the call to
std::io::Error::new
, but adds anotherunwrap
/expect
.Thus, I propose to add another API to simplify the downcasting while avoiding
unwrap
/expect
, which isstd::io::Error::try_downcast_inner
.It returns
Result<Box<E>, Self>
, and when the downcast fails, it simply return the originalstd::io::Error
.Motivation, use-cases
This makes downcasting much easier:
Solution sketches
The first solution I came to mind with is:
Then I realized that this is too complex and exposes too much details of
std::io::Error
.The second solution:
The problem with this solution is that it creates confusion by introducing a new function with name very similar to existing one.
The text was updated successfully, but these errors were encountered: