-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 Iterator::try_find #63177
Add Iterator::try_find #63177
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Sorry about force-pushes. The code was completely broken, but not it compiles at least, so I'll be adding commits instead of force pushing from now on. |
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 iterator trait is generally not built around iteration over Results, and I'm not sure that adding a variant of a single method makes sense when thinking about Iterator on the whole. Thoughts @rust-lang/libs? |
We already have |
Using |
I also had a feeling this |
@alexcrichton could you elaborate more? If I understand correctly - stabilizing means promoting this fn to stable, and I'd rather just keep unstable for a while - cause maybe there's a better way of providing this API. I fully agree that we might want to add other |
Sorry yeah what I mean to say is that I would want to pause before stabilization, but this is fine to land unstable at any time. When landing it unstable though it seem reasonable to try to use |
Right, I'm in progress with switching to |
fn try_find<F, E, T, FR, R>(&mut self, mut f: F) -> R where
Self: Sized,
FR: Try<Ok = bool, Error = E>,
F: FnMut(&Self::Item) -> FR,
R: Try<Ok = Option<Self::Item>, Error = E> { ... } This is what I'm trying atm. Is it ok if we use |
Returning the Any thoughts? |
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 |
I'm having troubles with running tests locally - UPD: for later: |
I really like this, having it return a Something else to think about: similar to |
I think, as a general rule, almost anything that takes With the result - I think I implement it with |
I've created this: https://github.com/MOZGIII/iter-try-rs Based on this experiment, I think it looks like we should only provide a single implementation of the I'd suggest to go with the |
☔ The latest upstream changes (presumably #67596) made this pull request unmergeable. Please resolve the merge conflicts. |
r? @Amanieu |
@MOZGIII if you can resolve the conflicts, we can proceed with this |
Rebased, ready to proceed |
Run |
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 |
Thx, done |
@bors r+ |
📌 Commit 5446cc9 has been approved by |
Add Iterator::try_find I found a need for this fn, and created this PR. Tracking issue: #63178 I did a fair amount of thinking about the function name, and settled on the current one. I don't see other anything else that's non-trivial here, but I'm open for debate. I just want this functionality to be there. It couples with the `collect` trick for collecting `Result<Vec<T>, E>` from `Iterator<Item = Result<T, E>>`. UPD: I've already looked at `fallible_iterator` crate, but I don't think it supports my use case. The main problem is that I can't construct a failable iterator. I have a regular iterator, and I just need to apply a predicate that can fail via `find` method. UPD: `fallible_iterator` would work, but it's not elegant cause I'd have to make a failable iterator by mapping iterator with `Result::Ok` first.
☀️ Test successful - checks-azure |
I found a need for this fn, and created this PR.
Tracking issue: #63178
I did a fair amount of thinking about the function name, and settled on the current one.
I don't see other anything else that's non-trivial here, but I'm open for debate. I just want this functionality to be there.
It couples with the
collect
trick for collectingResult<Vec<T>, E>
fromIterator<Item = Result<T, E>>
.UPD:
I've already looked at
fallible_iterator
crate, but I don't think it supports my use case.The main problem is that I can't construct a failable iterator. I have a regular iterator, and I just need to apply a predicate that can fail via
find
method.UPD:
fallible_iterator
would work, but it's not elegant cause I'd have to make a failable iterator by mapping iterator withResult::Ok
first.