Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
RFC: add
try_all
andtry_any
toIterator
#3233base: master
Are you sure you want to change the base?
RFC: add
try_all
andtry_any
toIterator
#3233Changes from all commits
237e49d
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
Can there be a general adapter for these?
I did recently r+
try_collect
for nightly (rust-lang/rust#94041), but now I'm wondering if instead of having all of these -- especially for non-adapters -- we should expose the shunting mechanism that they use somehow instead.Notably, the
.try_collect()
could plausibly be exposed as.try_mut(|i| i.collect())
, so I wonder if there'd be a way thattry_all
andtry_find
and friends could also be exposed via a similar mechanism. (And thus removetry_collect
andtry_find
and such from the trait altogether.try_fold
is a slightly different case as it's there to be the internal iteration primitive, and is only secondarily there as something to be used directly.)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.
Huh, I am not sure if I fully understood it, but from the looks it does indeed look nice to have the
try_process
available through e.g.try_mut
/try
and it could possibly be used for all the iterator methods to forTry
types. Iftry_all
andtry_any
would be as simple asiter.try(|i| i.all(check))
oriter.try().all(check)
. The naming would be a point though, I am not sure if that would be very discoverable and whether it really works.The latter possiblity would also be a bigger one, yet more flexible. It would need to return something new with new implementations probably.
Nevertheless, I think it makes a lot sense to go for a way that allows using
Try
in all the iterater-consuming methods. It would be a different and bigger change probably and one needs to make sure things like.try_mut(|i| i.next())
and.try_mut(|i| i.map())
behave as expected. For map I am quite sure it cannot behave up to expectations, as it just returns a new iterator and doesn't iterate.If we would still go for adding
try_
methods, they should at least be able to use that method internally.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 this is unlikely (without major language changes).
try_find
/try_all
/etc have to deal with a closure, which makes them a lot more complicated thantry_collect
. If you wanted to implementedtry_find
in the shape of.try(|i| i.find(...))
then you'd have to return abool
from the closure, but you have a closure that returns animpl Try<Output = bool>
. So how do you handle a failure? There are two hurdles:Fn*
traits.bool
in this case). It doesn't even matter much what it returns, as the iteration stop before the next element anyway, but it needs to return something. ADefault
bound might solve it for most methods, but what about e.g.try_max_by_key
?We can almost get there, but not quite all the way it seems. Implementing non-
try
methods in terms of theirtry
counterparts is, of course, a lot simpler.