-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add Iterator::find_or_{first,nth,last} #79271
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
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (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. |
/// closure returns `true`. | ||
/// | ||
/// Because `find_or_last()` takes a reference, and many iterators iterate over references, this | ||
/// leads to a possibly confusing situation where the argument is a double reference. You can |
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.
/// leads to a possibly confusing situation where the argument is a double reference. You can | |
/// leads to a special situation where the argument is a double reference. You can |
Recommendation to remove passive wording.
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 copied the wording from Iterator::find
. Shall I change it there as well? It also present in some other functions, e.g. Iterator::filter
.
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.
It's simply a matter of style. I realize that wording which suggests probable 'confusion' is commonly used throughout Rust code and documentation.
FWIW I'm not in any position to be making requests of contributors :)
r? @m-ou-se |
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.
Thanks for your PR! And sorry for the delay.
Do you have some more motivating examples for adding these?
In the case of a Vec
or array, .find(..).or(v.first())
(or last
/ get
) would do the same, and adding new functions for this are probably not worth it. Your implementation does however clearly show that it isn't so simple for Iterator
s in general without having to iterate the iterator twice, which might be a good reason to add them.
Either way,
find_or_nth
seems to not solve a concrete use case.
Then it's probably better to not add that one for now. Just like find_or_max
etc., it's probably a bit too specific with very few use cases.
I understand Rust is a busy project. Have no worries.
The format listing mentioned in the first PR message is the only concrete example I stumbled upon, though I haven't attempted to look for further instances of this problem. I'm not sure how I would sanely approach making such a survey.
This only works for
If |
Ping from triage - @z33ky |
@JohnCSimon you got it. There are three open questions. 1) Usefulness of this changeAs @m-ou-se said:
I'm stating that on top of these methods working on 2)
|
I should maybe also state that @m-ou-se asked me if I have more motivating examples. My reply was that I have not and I am unsure how I would approach making a survey for concrete examples. |
☔ The latest upstream changes (presumably #76391) made this pull request unmergeable. Please resolve the merge conflicts. |
@z33ky can you resolve the conflicts? thanks |
@rust-lang/libs What do y'all think about adding these Iterator methods? |
These methods look incredibly niche to me. They might be a better fit for the
Speaking for myself personally, concrete use cases are perhaps the most important thing to consider when adding new APIs. |
Agreed -- I would prefer not to build these into |
I'd be up for it, but it looks like I'll be trying for itertools instead.
Yeah, makes sense. What good is code, even well written, if it goes unused. |
Triage: I'm gonna close this according to T-libs members' response above. Feel free to raise questions in T-libs stream on official Zulip instance if you want to continue this discussion. Thanks for your work anyway! |
I'm proposing to add the methods
find_or_{first,nth,last}
to theIterator
trait, to facilitate returning an item that is part of the sequence itself in case no item satisfies the predicate.Example:
Some APIs return different formats or profiles/configurations that it supports and the developer shall choose one from them. Usually these have differing properties, where the developer can choose one according to their requirements or preferences. Examples for such an API can be found in Vulkan, like choosing a color-format where a listing obtained is via
vkGetPhysicalDeviceSurfaceFormatsKHR
(Surface::get_physical_device_surface_formats
in ash, a wrapper library for Rust).With such a list at hand,
Iterator::find
can be used to find a fitting option, and if none is found, any item should be selected instead. By providing additional methods in theIterator
trait, this task can be implemented elegantly.For the mentioned use case the sequences that are processed may either have a specific order of preferences, with the most preferred one either first or last, or no communicated preference in the items.
In the former case (preference present), one would normally either want the first or last items as default, and in the latter it wouldn't matter. Either way,
find_or_nth
seems to not solve a concrete use case.I added it for some semblance of "symmetry" with other item-retrieval methods already available on
Iterator
. Likewise,find_or_min
andfind_or_max
could be added as well, though conceptually these would be like "find items with this (caller-provided) predicate, or else this other (hard-coded) predicate", which is a little weird, while the methods I intend to add are more like "find items with this predicate, or else return the item on this position", which in my head makes more sense to provide.Maybe only
find_or_first
andfind_or_last
are really useful to have though andfind_or_nth
shouldn't be added to libstd.I started implementing
DoubleEndedIterator::rfind_or_{first,nth_back,last}
, but again just for some semblance of symmetry. I think I'll see to some feedback first before spending time on those. An argument could perhaps be made for addingfind_or_nth_back
andrfind_or_nth
in that case, too.