-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add impl
to collect an iterator of Result<T,E>
into (Collection<T>, Collection<E>)
#87047
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1626,6 +1626,25 @@ impl<A, E, V: FromIterator<A>> FromIterator<Result<A, E>> for Result<V, E> { | |
} | ||
} | ||
|
||
//FIXME: Figure out how to mark this as unstable without x.py refusing to run any tests | ||
#[stable(feature = "tuple_from_result_iter", since = "1.53.0")] | ||
impl<T, E, U, V> FromIterator<Result<T, E>> for (U, V) | ||
where | ||
U: Default + Extend<T>, | ||
V: Default + Extend<E>, | ||
{ | ||
fn from_iter<I: IntoIterator<Item = Result<T, E>>>(iter: I) -> (U, V) { | ||
let (mut oks, mut errs) = (U::default(), V::default()); | ||
for result in iter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering, why did this got a negative feedback? Internal iteration should always be either as fast as or faster than external iteration, so you should always try to use it if you can, hence why I would have used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I disagree with this. for_each introduces a lot more syntactic noise and does the same thing as a normal for loop. If this happens to be super slow for some reason it seems fine to use for_each, but I don't think it should be the default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, you didn't mention at first that this was for performance reasons, so I thought it was just personal preference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's already pretty slow for iterators like
But isn't it already the default in pretty much all the stdlib?
Yeah my bad for that one, I was assuming that the difference between |
||
match result { | ||
Ok(ok) => oks.extend_one(ok), | ||
Err(err) => errs.extend_one(err), | ||
} | ||
} | ||
(oks, errs) | ||
} | ||
} | ||
|
||
#[unstable(feature = "try_trait_v2", issue = "84277")] | ||
impl<T, E> ops::TryV2 for Result<T, E> { | ||
type Output = T; | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
This obviously should be marked as unstable, but every time I did so, it would cause
x.py
to refuse any attempt I made to run the tests, instead exiting with the message:Thus I temporarily marked it as stable as a kludge to get the tests to run. I realize there is probably just a mistake I'm making as a result of this being my first contribution here, so I'd appreciate some help resolving this small detail.
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.
Impls can't be unstable, see the linked issue. They're always insta-stable.
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.
I see. I read the issue before but was confused because there is an
impl
just a few lines below where I inserted my code (line 1649,impl<T, E> ops::TryV2 for Result<T, E>
) which is marked as unstable, but upon re-reading the issue I realize the difference is that there the trait itself is unstable.