Skip to content
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

Merged
merged 1 commit into from
Jan 2, 2020
Merged

Add Iterator::try_find #63177

merged 1 commit into from
Jan 2, 2020

Conversation

MOZGIII
Copy link
Contributor

@MOZGIII MOZGIII commented Jul 31, 2019

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.

@rust-highfive
Copy link
Collaborator

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 31, 2019
@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

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.

@MOZGIII MOZGIII changed the title Add find_result at Iterator Add Iterator::find_result Aug 1, 2019
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-08-01T00:08:29.1243892Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-08-01T00:08:29.1423352Z ##[command]git config gc.auto 0
2019-08-01T00:08:29.1473539Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-08-01T00:08:29.1517023Z ##[command]git config --get-all http.proxy
2019-08-01T00:08:29.1641956Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/63177/merge:refs/remotes/pull/63177/merge
---
2019-08-01T00:09:03.9396430Z do so (now or later) by using -b with the checkout command again. Example:
2019-08-01T00:09:03.9396456Z 
2019-08-01T00:09:03.9396620Z   git checkout -b <new-branch-name>
2019-08-01T00:09:03.9396644Z 
2019-08-01T00:09:03.9396695Z HEAD is now at 91e130838 Merge f851f29352aaa9d33fdd08397a297fcfb6c418d8 into 8a58268b5ad9c4a240be349a633069d48991eb0c
2019-08-01T00:09:03.9593590Z ##[section]Starting: Collect CPU-usage statistics in the background
2019-08-01T00:09:03.9595940Z ==============================================================================
2019-08-01T00:09:03.9595986Z Task         : Bash
2019-08-01T00:09:03.9596037Z Description  : Run a Bash script on macOS, Linux, or Windows
---
2019-08-01T01:08:36.9698737Z .................................................................................................... 1400/8819
2019-08-01T01:08:42.8103162Z .................................................................................................... 1500/8819
2019-08-01T01:08:55.5173414Z .................................................................i...............i.................. 1600/8819
2019-08-01T01:09:02.8500228Z .................................................................................................... 1700/8819
2019-08-01T01:09:17.3263892Z ...................................................iiiii............................................ 1800/8819
2019-08-01T01:09:28.3308992Z .................................................................................................... 2000/8819
2019-08-01T01:09:30.7032436Z .................................................................................................... 2100/8819
2019-08-01T01:09:34.2948831Z .................................................................................................... 2200/8819
2019-08-01T01:09:41.1424125Z .................................................................................................... 2300/8819
---
2019-08-01T01:13:34.7135879Z .................................................................................................... 5300/8819
2019-08-01T01:13:42.1100280Z ..............i..................................................................................... 5400/8819
2019-08-01T01:13:47.7255558Z .................................................................................................... 5500/8819
2019-08-01T01:14:00.2290628Z .................................................................................................... 5600/8819
2019-08-01T01:14:13.6168452Z ........ii...i..ii...........i...................................................................... 5700/8819
2019-08-01T01:14:31.0965989Z .................................................................................................... 5900/8819
2019-08-01T01:14:36.0580848Z .................................................................................................... 6000/8819
2019-08-01T01:14:36.0580848Z .................................................................................................... 6000/8819
2019-08-01T01:14:50.1004519Z ........i..ii....................................................................................... 6100/8819
2019-08-01T01:15:09.4352515Z ...................................................i................................................ 6300/8819
2019-08-01T01:15:11.5098524Z .................................................................................................... 6400/8819
2019-08-01T01:15:13.9784184Z .....................i.............................................................................. 6500/8819
2019-08-01T01:15:18.5059526Z .................................................................................................... 6600/8819
---
2019-08-01T01:19:55.0634585Z  finished in 20.485
2019-08-01T01:19:55.0807889Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:19:55.2364015Z 
2019-08-01T01:19:55.2364818Z running 146 tests
2019-08-01T01:19:58.4538831Z i....iii......iii..iiii....i............................i..i................i....i.........ii.i.i..i 100/146
2019-08-01T01:20:00.2778030Z iii..............i.........iii.i......ii......
2019-08-01T01:20:00.2781739Z 
2019-08-01T01:20:00.2783118Z  finished in 5.197
2019-08-01T01:20:00.2956021Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:20:00.4517342Z 
---
2019-08-01T01:20:02.4759891Z  finished in 2.180
2019-08-01T01:20:02.4935453Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:20:02.6419283Z 
2019-08-01T01:20:02.6419869Z running 9 tests
2019-08-01T01:20:02.6421064Z iiiiiiiii
2019-08-01T01:20:02.6421456Z 
2019-08-01T01:20:02.6421502Z  finished in 0.148
2019-08-01T01:20:02.6598460Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:20:02.8148703Z 
---
2019-08-01T01:20:20.7166270Z  finished in 18.056
2019-08-01T01:20:20.7357841Z Check compiletest suite=debuginfo mode=debuginfo-gdb+lldb (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:20:20.8999427Z 
2019-08-01T01:20:20.8999773Z running 122 tests
2019-08-01T01:20:43.6670459Z .iiiii...i.....i..i...i..i.i.i..i.ii..i.i.....i..i....i..........iiii..........i...ii...i.......ii.i 100/122
2019-08-01T01:20:48.1862250Z .i.i......iii.i.....ii
2019-08-01T01:20:48.1865961Z 
2019-08-01T01:20:48.1870288Z  finished in 27.451
2019-08-01T01:20:48.1877922Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:20:48.1878347Z Copying stage2 rustc from stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
---
2019-08-01T01:33:15.5752170Z 
2019-08-01T01:33:15.5876215Z  finished in 161.372
2019-08-01T01:33:15.5891763Z Testing core stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-01T01:33:15.7765974Z    Compiling core v0.0.0 (/checkout/src/libcore)
2019-08-01T01:33:21.0310751Z error[E0658]: use of unstable library feature 'find_result': new API
2019-08-01T01:33:21.0312405Z     --> src/libcore/../libcore/tests/iter.rs:1264:26
2019-08-01T01:33:21.0312985Z      |
2019-08-01T01:33:21.0313573Z 1264 |     assert_eq!(xs.iter().find_result(testfn), Ok(None));
2019-08-01T01:33:21.0314964Z      |
2019-08-01T01:33:21.0314964Z      |
2019-08-01T01:33:21.0315738Z      = note: for more information, see ***/issues/63178
2019-08-01T01:33:21.0316304Z      = help: add `#![feature(find_result)]` to the crate attributes to enable
2019-08-01T01:33:21.0316497Z 
2019-08-01T01:33:21.0328362Z error[E0658]: use of unstable library feature 'find_result': new API
2019-08-01T01:33:21.0328667Z     --> src/libcore/../libcore/tests/iter.rs:1266:26
2019-08-01T01:33:21.0329028Z      |
2019-08-01T01:33:21.0329272Z 1266 |     assert_eq!(xs.iter().find_result(testfn), Ok(Some(&2)));
2019-08-01T01:33:21.0329719Z      |
2019-08-01T01:33:21.0329719Z      |
2019-08-01T01:33:21.0330211Z      = note: for more information, see ***/issues/63178
2019-08-01T01:33:21.0330465Z      = help: add `#![feature(find_result)]` to the crate attributes to enable
2019-08-01T01:33:21.0330498Z 
2019-08-01T01:33:21.0342397Z error[E0658]: use of unstable library feature 'find_result': new API
2019-08-01T01:33:21.0342935Z     --> src/libcore/../libcore/tests/iter.rs:1268:26
2019-08-01T01:33:21.0343291Z      |
2019-08-01T01:33:21.0343669Z 1268 |     assert_eq!(xs.iter().find_result(testfn), Err(()));
2019-08-01T01:33:21.0345149Z      |
2019-08-01T01:33:21.0345149Z      |
2019-08-01T01:33:21.0345741Z      = note: for more information, see ***/issues/63178
2019-08-01T01:33:21.0346455Z      = help: add `#![feature(find_result)]` to the crate attributes to enable
2019-08-01T01:33:21.0346682Z 
2019-08-01T01:33:21.0358742Z error[E0658]: use of unstable library feature 'find_result': new API
2019-08-01T01:33:21.0359392Z     --> src/libcore/../libcore/tests/iter.rs:1272:21
2019-08-01T01:33:21.0359817Z      |
2019-08-01T01:33:21.0360439Z 1272 |     assert_eq!(iter.find_result(testfn), Ok(Some(&2)));
2019-08-01T01:33:21.0361994Z      |
2019-08-01T01:33:21.0361994Z      |
2019-08-01T01:33:21.0362831Z      = note: for more information, see ***/issues/63178
2019-08-01T01:33:21.0363557Z      = help: add `#![feature(find_result)]` to the crate attributes to enable
2019-08-01T01:33:21.0363724Z 
2019-08-01T01:33:21.0376001Z error[E0658]: use of unstable library feature 'find_result': new API
2019-08-01T01:33:21.0376567Z     --> src/libcore/../libcore/tests/iter.rs:1273:21
2019-08-01T01:33:21.0377066Z      |
2019-08-01T01:33:21.0377745Z 1273 |     assert_eq!(iter.find_result(testfn), Err(()));
2019-08-01T01:33:21.0378525Z      |
2019-08-01T01:33:21.0378525Z      |
2019-08-01T01:33:21.0379024Z      = note: for more information, see ***/issues/63178
2019-08-01T01:33:21.0379659Z      = help: add `#![feature(find_result)]` to the crate attributes to enable
2019-08-01T01:33:27.3009518Z error: aborting due to 5 previous errors
2019-08-01T01:33:27.3010100Z 
2019-08-01T01:33:27.3010483Z For more information about this error, try `rustc --explain E0658`.
2019-08-01T01:33:27.4120842Z error: Could not compile `core`.
2019-08-01T01:33:27.4120842Z error: Could not compile `core`.
2019-08-01T01:33:27.4121441Z 
2019-08-01T01:33:27.4121823Z To learn more, run the command again with --verbose.
2019-08-01T01:33:27.4140405Z 
2019-08-01T01:33:27.4140639Z 
2019-08-01T01:33:27.4141341Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "test" "--target" "x86_64-unknown-linux-gnu" "-j" "2" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace compiler-builtins-c" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "-p" "core" "--" "--quiet"
2019-08-01T01:33:27.4141742Z 
2019-08-01T01:33:27.4141851Z 
2019-08-01T01:33:27.4151992Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2019-08-01T01:33:27.4152242Z Build completed unsuccessfully in 1:18:01
2019-08-01T01:33:27.4152242Z Build completed unsuccessfully in 1:18:01
2019-08-01T01:33:27.9374229Z ##[error]Bash exited with code '1'.
2019-08-01T01:33:27.9420049Z ##[section]Starting: Checkout
2019-08-01T01:33:27.9421543Z ==============================================================================
2019-08-01T01:33:27.9421603Z Task         : Get sources
2019-08-01T01:33:27.9421640Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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 @TimNN. (Feature Requests)

@sfackler
Copy link
Member

sfackler commented Aug 1, 2019

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?

@sinkuu
Copy link
Contributor

sinkuu commented Aug 1, 2019

We already have try_fold and try_for_each that uses Try trait, so generic try_find may make more sense than a Result-specific method.

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

Using Try sounds reasonable, I’ll update the PR with generic Try instead of a Result.

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

I also had a feeling this Result-specific functionality does not belong with the Iterator trait, but then I deduced it’s not that bad. And now it’s going to be solved elegantly with Try - and it won’t extend the existing practices. Except for it’d be the first occasion of returning a Try (concrete Result as in the current version). Is this an issue? I feel like it’s ok.

@alexcrichton
Copy link
Member

I personally agree with @sfackler in terms of stabilization and @sinkuu in terms of implementation basically. I would be wary to stabilize just one try_* method without starting consider more, and it seems good for Result-intended methods to largely have consistent signatures.

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

@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 try_* calls.
I can even give it a try if this PR goes in successfully. I just want to get used to the contribution process before taking a bigger bite.

@alexcrichton
Copy link
Member

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 Try and perhaps tweak the name to try_* or something like that.

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

Right, I'm in progress with switching to Try and changing the name.
There seems to be a bit of an issue though - I'm not sure how to derive the return value of the whole fn (not the predicate) via trait combinations with Try. Might be worth to just explicitly use Result there...

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

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 R the return value like that? Or should it be Result<Option<Self::Item>, E> explicitly?

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 1, 2019

Returning the Result explicitly (instead of Try) kind of makes much more sense to me.

Any thoughts?

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-6.0 of your PR failed (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2019-08-02T00:51:11.7223319Z ##[command]git remote add origin https://github.com/rust-lang/rust
2019-08-02T00:51:11.7406187Z ##[command]git config gc.auto 0
2019-08-02T00:51:11.7495213Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2019-08-02T00:51:11.7586489Z ##[command]git config --get-all http.proxy
2019-08-02T00:51:11.7799232Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/63177/merge:refs/remotes/pull/63177/merge
---
2019-08-02T00:51:45.6215568Z do so (now or later) by using -b with the checkout command again. Example:
2019-08-02T00:51:45.6215613Z 
2019-08-02T00:51:45.6215795Z   git checkout -b <new-branch-name>
2019-08-02T00:51:45.6215822Z 
2019-08-02T00:51:45.6215865Z HEAD is now at c123b0fa5 Merge e0dbc3e57ab09e080499165617d9103459adcebd into 435236b8877cdb98c82eaebfb7887782277265c5
2019-08-02T00:51:45.6384597Z ##[section]Starting: Collect CPU-usage statistics in the background
2019-08-02T00:51:45.6387804Z ==============================================================================
2019-08-02T00:51:45.6387887Z Task         : Bash
2019-08-02T00:51:45.6387933Z Description  : Run a Bash script on macOS, Linux, or Windows
---
2019-08-02T01:48:56.6603545Z .................................................................................................... 1400/8821
2019-08-02T01:49:02.2298524Z .................................................................................................... 1500/8821
2019-08-02T01:49:14.4804627Z .................................................................i...............i.................. 1600/8821
2019-08-02T01:49:21.5839974Z .................................................................................................... 1700/8821
2019-08-02T01:49:35.8492732Z ...................................................iiiii............................................ 1800/8821
2019-08-02T01:49:46.4675457Z .................................................................................................... 2000/8821
2019-08-02T01:49:48.8544227Z .................................................................................................... 2100/8821
2019-08-02T01:49:52.2049894Z .................................................................................................... 2200/8821
2019-08-02T01:49:58.4840338Z .................................................................................................... 2300/8821
---
2019-08-02T01:53:43.4688859Z .................................................................................................... 5300/8821
2019-08-02T01:53:51.3939606Z ...............i.................................................................................... 5400/8821
2019-08-02T01:53:56.3736312Z .................................................................................................... 5500/8821
2019-08-02T01:54:08.3884713Z .................................................................................................... 5600/8821
2019-08-02T01:54:21.3696465Z .........ii...i..ii...........i..................................................................... 5700/8821
2019-08-02T01:54:36.2059084Z .................................................................................................... 5900/8821
2019-08-02T01:54:40.9344920Z .................................................................................................... 6000/8821
2019-08-02T01:54:40.9344920Z .................................................................................................... 6000/8821
2019-08-02T01:54:54.2325195Z .........i..ii...................................................................................... 6100/8821
2019-08-02T01:55:12.5204205Z ....................................................i............................................... 6300/8821
2019-08-02T01:55:14.4634016Z .................................................................................................... 6400/8821
2019-08-02T01:55:16.7478926Z .......................i............................................................................ 6500/8821
2019-08-02T01:55:20.9493280Z .................................................................................................... 6600/8821
---
2019-08-02T01:59:48.9525148Z  finished in 22.191
2019-08-02T01:59:48.9689740Z Check compiletest suite=codegen mode=codegen (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-02T01:59:49.1201150Z 
2019-08-02T01:59:49.1201307Z running 146 tests
2019-08-02T01:59:52.1574482Z i....iii......iii..iiii....i............................i..i................i....i.........ii.i.i..i 100/146
2019-08-02T01:59:53.9047367Z iii..............i.........iii.i......ii......
2019-08-02T01:59:53.9047811Z 
2019-08-02T01:59:53.9047855Z  finished in 4.935
2019-08-02T01:59:53.9212835Z Check compiletest suite=codegen-units mode=codegen-units (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-02T01:59:54.0616991Z 
---
2019-08-02T01:59:56.0106617Z  finished in 2.089
2019-08-02T01:59:56.0276884Z Check compiletest suite=assembly mode=assembly (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-02T01:59:56.1723349Z 
2019-08-02T01:59:56.1723881Z running 9 tests
2019-08-02T01:59:56.1724461Z iiiiiiiii
2019-08-02T01:59:56.1724822Z 
2019-08-02T01:59:56.1724859Z  finished in 0.144
2019-08-02T01:59:56.1881720Z Check compiletest suite=incremental mode=incremental (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-02T01:59:56.3370376Z 
---
2019-08-02T02:00:13.8425183Z  finished in 17.654
2019-08-02T02:00:13.8611952Z Check compiletest suite=debuginfo mode=debuginfo-gdb+lldb (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-02T02:00:14.0234464Z 
2019-08-02T02:00:14.0234808Z running 122 tests
2019-08-02T02:00:37.0889550Z .iiiii...i.....i..i...i..i.i.i..i.ii..i.i.....i..i....i..........iiii..........i...ii...i.......ii.i 100/122
2019-08-02T02:00:41.5230739Z .i.i......iii.i.....ii
2019-08-02T02:00:41.5234369Z 
2019-08-02T02:00:41.5234979Z  finished in 27.662
2019-08-02T02:00:41.5238750Z Uplifting stage1 rustc (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
2019-08-02T02:00:41.5239387Z Copying stage2 rustc from stage1 (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu / x86_64-unknown-linux-gnu)
---
2019-08-02T02:13:56.4932462Z 
2019-08-02T02:13:56.4933437Z    Doc-tests core
2019-08-02T02:14:00.3109700Z 
2019-08-02T02:14:00.3110662Z running 2381 tests
2019-08-02T02:14:12.4394069Z ......iiiii......................................................................................... 100/2381
2019-08-02T02:14:24.4127755Z .........................................................................ii......................... 200/2381
2019-08-02T02:14:52.8061489Z .................................................................................................... 400/2381
2019-08-02T02:14:52.8061489Z .................................................................................................... 400/2381
2019-08-02T02:15:03.4015042Z ........................FF......i..i.................iiii........................................... 500/2381
2019-08-02T02:15:25.8315461Z .................................................................................................... 700/2381
2019-08-02T02:15:37.3104857Z .................................................................................................... 800/2381
2019-08-02T02:15:48.8218963Z .................................................................................................... 900/2381
2019-08-02T02:16:00.0256426Z .................................................................................................... 1000/2381
---
2019-08-02T02:18:48.6030981Z ---- iter/traits/iterator.rs - iter::traits::iterator::Iterator::try_find (line 1972) stdout ----
2019-08-02T02:18:48.6031265Z error[E0277]: the trait bound `bool: std::ops::Try` is not satisfied
2019-08-02T02:18:48.6031713Z  --> iter/traits/iterator.rs:1976:23
2019-08-02T02:18:48.6032292Z   |
2019-08-02T02:18:48.6032512Z 7 | let result = a.iter().try_find(|&s| s.parse()? == 2);
2019-08-02T02:18:48.6032899Z   |                       ^^^^^^^^ the trait `std::ops::Try` is not implemented for `bool`
2019-08-02T02:18:48.6033081Z 
2019-08-02T02:18:48.6033280Z error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
2019-08-02T02:18:48.6033799Z  --> iter/traits/iterator.rs:1976:37
2019-08-02T02:18:48.6034049Z   |
2019-08-02T02:18:48.6034270Z 7 | let result = a.iter().try_find(|&s| s.parse()? == 2);
2019-08-02T02:18:48.6034480Z   |                                     ^^^^^^^^^^ cannot use the `?` operator in a function that returns `bool`
2019-08-02T02:18:48.6034868Z   = help: the trait `std::ops::Try` is not implemented for `bool`
2019-08-02T02:18:48.6035054Z   = note: required by `std::ops::Try::from_error`
2019-08-02T02:18:48.6035219Z 
2019-08-02T02:18:48.6035423Z error[E0308]: mismatched types
2019-08-02T02:18:48.6035423Z error[E0308]: mismatched types
2019-08-02T02:18:48.6036663Z  --> iter/traits/iterator.rs:1978:1
2019-08-02T02:18:48.6037004Z   |
2019-08-02T02:18:48.6037272Z 9 | assert_eq!(result, Ok(Some(&2)));
2019-08-02T02:18:48.6037742Z   | |
2019-08-02T02:18:48.6038023Z   | expected &str, found integer
2019-08-02T02:18:48.6038023Z   | expected &str, found integer
2019-08-02T02:18:48.6038267Z   | help: try using a variant of the expected type: `Err(*right_val)`
2019-08-02T02:18:48.6038495Z   |
2019-08-02T02:18:48.6038959Z   = note: expected type `std::result::Result<std::option::Option<&&str>, _>`
2019-08-02T02:18:48.6039442Z              found type `std::result::Result<std::option::Option<&{integer}>, _>`
2019-08-02T02:18:48.6040235Z 
2019-08-02T02:18:48.6040447Z error: aborting due to 3 previous errors
2019-08-02T02:18:48.6040624Z 
2019-08-02T02:18:48.6040842Z Some errors have detailed explanations: E0277, E0308.
2019-08-02T02:18:48.6040842Z Some errors have detailed explanations: E0277, E0308.
2019-08-02T02:18:48.6041285Z For more information about an error, try `rustc --explain E0277`.
2019-08-02T02:18:48.6041745Z Couldn't compile the test.
2019-08-02T02:18:48.6042228Z ---- iter/traits/iterator.rs - iter::traits::iterator::Iterator::try_find (line 1981) stdout ----
2019-08-02T02:18:48.6043343Z error[E0277]: the trait bound `bool: std::ops::Try` is not satisfied
2019-08-02T02:18:48.6044218Z  --> iter/traits/iterator.rs:1985:23
2019-08-02T02:18:48.6044603Z   |
2019-08-02T02:18:48.6044752Z 7 | let result = a.iter().try_find(|&s| s.parse()? == 5);
2019-08-02T02:18:48.6045074Z   |                       ^^^^^^^^ the trait `std::ops::Try` is not implemented for `bool`
2019-08-02T02:18:48.6046197Z 
2019-08-02T02:18:48.6046396Z error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
2019-08-02T02:18:48.6046884Z  --> iter/traits/iterator.rs:1985:37
2019-08-02T02:18:48.6047093Z   |
2019-08-02T02:18:48.6047256Z 7 | let result = a.iter().try_find(|&s| s.parse()? == 5);
2019-08-02T02:18:48.6047444Z   |                                     ^^^^^^^^^^ cannot use the `?` operator in a function that returns `bool`
2019-08-02T02:18:48.6047785Z   = help: the trait `std::ops::Try` is not implemented for `bool`
2019-08-02T02:18:48.6047963Z   = note: required by `std::ops::Try::from_error`
2019-08-02T02:18:48.6048101Z 
2019-08-02T02:18:48.6048257Z error: aborting due to 2 previous errors
---
2019-08-02T02:18:48.6051111Z 
2019-08-02T02:18:48.6267775Z error: test failed, to rerun pass '--doc'
2019-08-02T02:18:48.6282631Z 
2019-08-02T02:18:48.6282920Z 
2019-08-02T02:18:48.6283987Z command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "test" "--target" "x86_64-unknown-linux-gnu" "-j" "2" "--release" "--locked" "--color" "always" "--features" "panic-unwind backtrace compiler-builtins-c" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "-p" "core" "--" "--quiet"
2019-08-02T02:18:48.6284295Z 
2019-08-02T02:18:48.6284322Z 
2019-08-02T02:18:48.6296306Z failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
2019-08-02T02:18:48.6296413Z Build completed unsuccessfully in 1:21:11
2019-08-02T02:18:48.6296413Z Build completed unsuccessfully in 1:21:11
2019-08-02T02:18:49.0839307Z ##[error]Bash exited with code '1'.
2019-08-02T02:18:49.0886606Z ##[section]Starting: Checkout
2019-08-02T02:18:49.0888492Z ==============================================================================
2019-08-02T02:18:49.0888549Z Task         : Get sources
2019-08-02T02:18:49.0888633Z Description  : Get sources from a repository. Supports Git, TfsVC, and SVN repositories.

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 @TimNN. (Feature Requests)

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 2, 2019

I'm having troubles with running tests locally - ./x.py test -i -j 4 src/libcore --stage 1 --test-args iter::traits::iterator::Iterator::try_find seems to compile stage0 compiler every time, and it takes at least 10 mins. I just want to iterate quickly on my feature - how can I do that?

UPD: for later: --keep-stage 0 helps

@timvermeulen
Copy link
Contributor

I really like this, having it return a Result is unfortunate though. I believe returning R would require higher-kinded types, since R::Ok of the return value would need to be Option<Self::Item> instead of bool.

Something else to think about: similar to try_find there could be try_find_map / try_any / try_all / try_position, would those be as useful as try_find?

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 2, 2019

I think, as a general rule, almost anything that takes FnMut would benefit from a Try version... Maybe we should think about this in a broader scale.

With the result - I think I implement it with Try as return value instead of Result, but that would be an independent type - and user would have to somehow specify what it should be. Technically it should be ok I guess - unless it's used like .try_find(...)? - this would leave compiler with no hint about the result type, and will force user to do verbose stuff. I'd also prefer having a generic Try as the response type, but I just can't figure out how to make it not painful to use.

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Aug 2, 2019

I've created this: https://github.com/MOZGIII/iter-try-rs
It supports generalization over the result type. I decided to only provide a generic impl over Try for the Result type, here: https://github.com/MOZGIII/iter-try-rs/blob/946d5e61dbde2458d27b99387290bf3b933281d2/src/try_find.rs#L48-L51

Based on this experiment, I think it looks like we should only provide a single implementation of the Try value of the box for the whole try_find return value - because otherwise users would have to inelegantly specify the exact type to disambiguate the inference when using ? operator. In other words, generic Try as a result would hurt the usability, and surprisingly the usability of the Try (?) operator itself, lol.

I'd suggest to go with the Result type explicitly in this case. I think it's justified enough.

@bors
Copy link
Contributor

bors commented Dec 25, 2019

☔ The latest upstream changes (presumably #67596) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC-zz
Copy link

r? @Amanieu

@rust-highfive rust-highfive assigned Amanieu and unassigned KodrAus Dec 31, 2019
@Dylan-DPC-zz
Copy link

@MOZGIII if you can resolve the conflicts, we can proceed with this

@Dylan-DPC-zz Dylan-DPC-zz added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Dec 31, 2019
@MOZGIII
Copy link
Contributor Author

MOZGIII commented Jan 1, 2020

Rebased, ready to proceed

@Amanieu
Copy link
Member

Amanieu commented Jan 1, 2020

Run ./x.py fmt to fix CI.

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-7 of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
2020-01-01T21:04:33.5528778Z ##[command]git remote add origin https://github.com/rust-lang/rust
2020-01-01T21:04:33.5730211Z ##[command]git config gc.auto 0
2020-01-01T21:04:33.5793058Z ##[command]git config --get-all http.https://github.com/rust-lang/rust.extraheader
2020-01-01T21:04:33.5848531Z ##[command]git config --get-all http.proxy
2020-01-01T21:04:33.6009860Z ##[command]git -c http.extraheader="AUTHORIZATION: basic ***" fetch --force --tags --prune --progress --no-recurse-submodules --depth=2 origin +refs/heads/*:refs/remotes/origin/* +refs/pull/63177/merge:refs/remotes/pull/63177/merge

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 @TimNN. (Feature Requests)

@MOZGIII
Copy link
Contributor Author

MOZGIII commented Jan 1, 2020

Thx, done

@Amanieu
Copy link
Member

Amanieu commented Jan 2, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Jan 2, 2020

📌 Commit 5446cc9 has been approved by Amanieu

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 2, 2020
@bors
Copy link
Contributor

bors commented Jan 2, 2020

⌛ Testing commit 5446cc9 with merge 0a58f58...

bors added a commit that referenced this pull request Jan 2, 2020
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.
@bors
Copy link
Contributor

bors commented Jan 2, 2020

☀️ Test successful - checks-azure
Approved by: Amanieu
Pushing 0a58f58 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.