-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Move the <dyn Error>::chain() method to a trait #69163
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
Conversation
r? @cramertj (rust_highfive has picked a reviewer for you, use r? to override) |
r? @withoutboats |
Don't you have to import the trait then? Wouldn't just a free standing |
Hmm... works, but not so ergonomic. E.g. for |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Because `chain()` was only implemented for `dyn Error`, users had to cast errors to a `&(dyn Error)` to use the `chain()` method. ```rust let mut iter = (&my_error as &(dyn Error)).chain(); // or let mut iter = <dyn Error>::chain(&my_error); // or let mut iter = Error::chain(&my_error); ``` What I would liked to have is ```rust pub trait Error: Debug + Display { fn source(&self) -> Option<&(dyn Error + 'static)> { None } fn chain(&self) -> Chain<'_> where Self: Sized + 'static { Chain { current: Some(self), } } } impl dyn Error + 'static { fn chain(&self) -> Chain<'_> { Chain { current: Some(self), } } } ``` This doesn't work, because of: rust-lang#69161 This patch introduces an ErrorChain trait, which accomplishes the job and is implemented for `Error` and `dyn Error + 'static`.
I think it's very important to have chain as a method method call. Otherwise it will not be very useful to add more functionality to the Chain, because creating a Chain will require too much effort. So I fully support the idea of this PR. I would very much prefer #69161 to be fixed though, since I think it's much nicer not to have to import a separate trait to get access to this method. |
@haraldh I figured out a way to get this to work, without requiring the ErrorChain trait to be added to the scope: It does trigger the warning added in #51443, but I guess it might be fine in this case. If not it seems reasonable to use this in nightly until #69161 is fixed. |
cool, but I don't want everybody using |
I would expect this can be done by adding |
If it doesn't work in the playground, I doubt it works without global disable. |
r? @dtolnay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I am interested in having a chain method that works better, but I would prefer not to add a new standard library trait just for this. I would instead like to see work on #69161 and then implement chain in the way you say you want in the PR description. A trait like this would only hamstring us once the method resolution issue is fixed.
Because
chain()
was only implemented fordyn Error
, users hadto cast errors to a
&(dyn Error)
to use thechain()
method.What I would liked to have is
This doesn't work, because of:
#69161
This patch introduces an ErrorChain trait, which accomplishes the
job and is implemented for
Error
anddyn Error + 'static
.