-
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
Refactor try_find
a little
#71899
Refactor try_find
a little
#71899
Conversation
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
Instead of Iterator::try_find_map I'd like more useful methods like Iterator::sorted, Iterator::unfold, and few other handy things. |
@leonardo-m - the usual playground for such stuff is the |
Why is try_find_map necessary? Why can't try_find_map wait Try to be stable so you can implment it in the playground itertools? |
It's on par with |
This comment has been minimized.
This comment has been minimized.
This is non-trivial enough that I'm going to reassign it to an actual T-libs member 🙂. r? @sfackler |
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.
I think I would prefer not to add the new method. The other try
iterator methods (try_fold and try_for_each) expose a tangible new capability over the non-try (fold, for_each) because the non-try do not have an ability to early terminate. But Iterator::find_map is already entirely about early termination. In find_map the closure is supposed to return Some if you want to early terminate and None to keep iterating, and find_map's return value gives the thing you early terminated with if any, whether that's an error or an object if you want. The proposed try_find_map is the same but with a "transposed" interpretation of the closure:
iter.try_find_map(|s| parse_even(s))
// equivalent to:
iter.find_map(|s| parse_even(s).transpose()).transpose()
All in all I don't think the benefit is there more than e.g. try_map, try_filter, try_filter_map, try_skip_while, try_scan, try_any, try_partition etc, and I'm not on board with more in this direction just for small convenience. I think the fallible-iterator crate is a suitable place for this kind of thing.
I would be happy to take the try_find fixes though: the doc fix, removed type parameter, and reduced parametricity.
The `E` type parameter was unnecessary, so it's now removed. The folding closure now has reduced parametricity on just `T = Self::Item`, rather than the whole `Self` iterator type. There's otherwise no functional change in this.
OK, thanks for the detailed consideration. I've dropped the |
@bors r+ |
📌 Commit db0d70e has been approved by |
…arth Rollup of 16 pull requests Successful merges: - rust-lang#71420 (Specialization is unsound) - rust-lang#71899 (Refactor `try_find` a little) - rust-lang#72689 (add str to common types) - rust-lang#72791 (update coerce docs and unify relevant tests) - rust-lang#72934 (forbid mutable references in all constant contexts except for const-fns) - rust-lang#73027 (Make `need_type_info_err` more conservative) - rust-lang#73347 (Diagnose use of incompatible sanitizers) - rust-lang#73359 (shim.rs: avoid creating `Call` terminators calling `Self`) - rust-lang#73399 (Clean up E0668 explanation) - rust-lang#73436 (Clean up E0670 explanation) - rust-lang#73440 (Add src/librustdoc as an alias for src/tools/rustdoc) - rust-lang#73442 (pretty/mir: const value enums with no variants) - rust-lang#73452 (Unify region variables when projecting associated types) - rust-lang#73458 (Use alloc::Layout in DroplessArena API) - rust-lang#73484 (Update the doc for std::prelude to the correct behavior) - rust-lang#73506 (Bump Rustfmt and RLS) Failed merges: r? @ghost
This works likefind_map
, but mapping to aTry
type. It stops whenOk
isSome(value)
, with an additional short-circuit onTry::Error
. This is similar to the unstabletry_find
, but has the advantage of being able to directly return the user'sR: Try
type directly, rather than converting toResult
.(removed --
try_find_map
was declined in review)This PR also refactors
try_find
a little to match style. TheE
type parameter was unnecessary, so it's now removed. The folding closure now has reduced parametricity on justT = Self::Item
, ratherthan the whole
Self
iterator type. There's otherwise no functional change in this.