-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
impl iter() for dyn Error #58289
impl iter() for dyn Error #58289
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
1831ef6
to
5a9c457
Compare
Thanks for the PR! I think this kind of functionality is definitely useful, but I think we'll want to tweak the API a bit. In particular, I think a method name like |
agreed with |
I'd return |
Which would make |
Yes, this is intended. Otherwise you have to box it to store it. |
Ah yeah, we should return a concrete iterator type. |
Ok, |
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 |
Examples: ```rust let next_error_type_a = err .iter_chain() .filter_map(Error::downcast_ref::<ErrorTypeA>) .next(); ``` ```rust let source_root_error = err.iter_chain().last(); ``` Credit for the ErrorIter goes to Tim Diekmann https://www.reddit.com/r/rust/comments/aj3lpg/is_an_iterator_impl_over_errorsource_possible/
More comments? |
@bors r+ rollup |
📌 Commit f06af1f has been approved by |
impl iter() for dyn Error Examples: ```rust let next_error_type_a = err .iter() .filter_map(Error::downcast_ref::<ErrorTypeA>) .next(); ``` ```rust let source_root_error = err.iter().last(); ``` Credit for the ErrorIter goes to reddit user /u/tdiekmann (Tim Diekmann) https://www.reddit.com/r/rust/comments/aj3lpg/is_an_iterator_impl_over_errorsource_possible/
impl iter() for dyn Error Examples: ```rust let next_error_type_a = err .iter() .filter_map(Error::downcast_ref::<ErrorTypeA>) .next(); ``` ```rust let source_root_error = err.iter().last(); ``` Credit for the ErrorIter goes to reddit user /u/tdiekmann (Tim Diekmann) https://www.reddit.com/r/rust/comments/aj3lpg/is_an_iterator_impl_over_errorsource_possible/
Rollup of 13 pull requests Successful merges: - #57693 (Doc rewording) - #57815 (Speed up the fast path for assert_eq! and assert_ne!) - #58034 (Stabilize the time_checked_add feature) - #58057 (Stabilize linker-plugin based LTO (aka cross-language LTO)) - #58137 (Cleanup: rename node_id_to_type(_opt)) - #58166 (allow shorthand syntax for deprecation reason) - #58196 (Add specific feature gate error for const-unstable features) - #58200 (fix str mutating through a ptr derived from &self) - #58273 (Rename rustc_errors dependency in rust 2018 crates) - #58289 (impl iter() for dyn Error) - #58387 (Disallow `auto` trait alias syntax) - #58404 (use Ubuntu keyserver for CloudABI ports) - #58405 (Remove some dead code from libcore) Failed merges: r? @ghost
impl iter() for dyn Error Examples: ```rust let next_error_type_a = err .iter() .filter_map(Error::downcast_ref::<ErrorTypeA>) .next(); ``` ```rust let source_root_error = err.iter().last(); ``` Credit for the ErrorIter goes to reddit user /u/tdiekmann (Tim Diekmann) https://www.reddit.com/r/rust/comments/aj3lpg/is_an_iterator_impl_over_errorsource_possible/
Rollup of 12 pull requests Successful merges: - #57693 (Doc rewording) - #57815 (Speed up the fast path for assert_eq! and assert_ne!) - #58034 (Stabilize the time_checked_add feature) - #58057 (Stabilize linker-plugin based LTO (aka cross-language LTO)) - #58137 (Cleanup: rename node_id_to_type(_opt)) - #58166 (allow shorthand syntax for deprecation reason) - #58200 (fix str mutating through a ptr derived from &self) - #58273 (Rename rustc_errors dependency in rust 2018 crates) - #58289 (impl iter() for dyn Error) - #58387 (Disallow `auto` trait alias syntax) - #58404 (use Ubuntu keyserver for CloudABI ports) - #58405 (Remove some dead code from libcore) Failed merges: r? @ghost
@sfackler : What's the procedure for this to make it into stable? |
The usability is not very ergonomic though: let mut iter = (&b as &(dyn Error)).iter_chain();
// or
let b = Box::<Error>::from(b); let mut iter = b.iter_chain();
// or
let mut iter = <dyn Error>::iter_chain(&b);
// or
let mut iter = Error::iter_chain(&b);
// but not
let mut iter = b.iter_chain(); Where we already have a |
It may be better to implement it as a provided trait method. |
pub trait Error: Debug + Display {
…
fn iter_chain(&self) -> ErrorIter
where
Self: Sized + 'static,
{
<dyn Error>::iter_chain(self)
}
…
} will give for: let mut iter = b.iter_chain();
How can I call the |
I meant, don't implement the method in |
if the pub trait Error: Debug + Display {
…
fn iter_chain(&self) -> ErrorIter
where
Self: Sized + 'static,
{
ErrorIter {
current: Some(self),
}
}
…
} will give for:
|
Ah, I see ... |
And with both in place returning ErrorIter: let err: Box<Error> = b.into();
let mut iter = err.iter_chain();
Where #2 cannot even be used 👎 |
Anyway, I could probably live with: let mut iter = Error::iter_chain(&b); which works as of now |
Here is a playground to play with. |
This needs a proper tracking issue to track stabilization and let people report feedback (the issue provided is to this pull request). I also think that |
Oh right, whoops. Tracking issue fix: #58521 |
Examples:
Credit for the ErrorIter goes to reddit user /u/tdiekmann (Tim Diekmann)
https://www.reddit.com/r/rust/comments/aj3lpg/is_an_iterator_impl_over_errorsource_possible/