Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd Iterator::single #55355
Conversation
Centril
added
the
T-libs
label
Oct 25, 2018
rust-highfive
assigned
alexcrichton
Oct 25, 2018
rust-highfive
added
the
S-waiting-on-review
label
Oct 25, 2018
rust-lang
deleted a comment from
rust-highfive
Oct 25, 2018
rust-lang
deleted a comment from
rust-highfive
Oct 25, 2018
This comment has been minimized.
This comment has been minimized.
|
Docs should probably mention that in the too-many-items case, two elements are consumed by the Iterator. Actually, it can probably be overridden by Prior art: C#: Enumerable.Single |
This comment has been minimized.
This comment has been minimized.
Hmm... I debated with myself whether I should include such a note but ultimately I decided it was "too much information" / detail; but I don't feel strongly. Do you have some good phrasing I copy into the docs perhaps?
Thanks! |
This comment has been minimized.
This comment has been minimized.
|
I'm really not a fan of making "0 elements" and "more than one element" indistinguishable. I feel like the whole point of |
This comment has been minimized.
This comment has been minimized.
If by "coding error" you mean logic bug, then I disagree. In the cases (proc macros) I wanted
Idk what that means... elaborate? I feel strongly that the |
This comment has been minimized.
This comment has been minimized.
|
Seems silly, but why not |
This comment has been minimized.
This comment has been minimized.
|
Actually, it would be |
This comment has been minimized.
This comment has been minimized.
|
@clarcharr
That's not so bad actually... I could be convinced in favor of it since it doesn't invent new types...
So |
This comment has been minimized.
This comment has been minimized.
|
I'm not sure how best the docs could mention that two elements of the iterator are consumed. But I'm pretty certain that most uses of this fn would be when the author is reasonably certain that the iterator should have a single element, and otherwise is a logic error or input error. There is benefit in returning the second item in the most common case I've seen for In conclusion, I have confused myself out of what would be the ideal, canonical signature for this fn. I think I lean very weakly towards |
This comment has been minimized.
This comment has been minimized.
I guess we could just say "the method consumes at most two elements from the iterator and does not exhaustively consume the entire iterator" or something.
Logic errors or input errors are very different and shouldn't be handled the same way.
I buy that I guess; I could go with Should we keep it as is?, change it?, wait for someone on T-libs before landing? |
This comment has been minimized.
This comment has been minimized.
|
cc @alexcrichton any chance you might get to this (there are some minor open design questions here for you to resolve...)? |
This comment has been minimized.
This comment has been minimized.
|
Er well I don't think I'm personally on the hook to solve any design questions here. If this isn't ready to land then I think we should close this PR until those design questions are fleshed out, or they can be hashed out on thread as well. My personal gut reaction to this method is that it's not worth it to add to the standard library, so I don't really have many opinions about the precise pieces here. If signatures like |
This comment has been minimized.
This comment has been minimized.
Eh well... The language team is on the hook to resolve (not solve) design questions about the language so it's not too crazy for y'all to resolve libs questions and pick between designs. :)
Perhaps we can land the PR as-is and get some experience on nightly then?
Can you elaborate on why perhaps? I'd like to note that numerous people noted that this method was useful to them on internals (see https://internals.rust-lang.org/t/what-do-you-think-about-iterator-single/8608).
OK but then it seems like you do have some opinions about the design (namely that the signature as-is in the PR belongs more in libcore than other signatures?). |
This comment has been minimized.
This comment has been minimized.
|
@Centril you singled me out specifically as specifically getting to this and solving open design problems here. It's not really my job, nor the libs team's, to solve any arbitrary bikeshed presented to them. We're all busy folks and are balancing contributing and reviewing with other tasks, and if something doesn't grab anyone's attention or doesn't seem like it should go in libstd, then it ends up not doing so. Modifying core traits like Iterators are used by practically everyone who uses Rust, so no matter what method you propose for an iterator some subset of folks will want to see it included in libstd and standardized. To me, however, this seems like very niche functionality where you want a guarantee that there's only one element in the iterator rather than just the next element. Start talking about recovering the iterator and item in erroneous cases and that's just way oer the top for me to be included in libstd. |
Centril commentedOct 25, 2018
Tracking issue: #55354
Suggested and discussed in https://internals.rust-lang.org/t/what-do-you-think-about-iterator-single/8608.
Alternatives
Designs
Alternative designs could use a dedicated enum:
but I think the common case doesn't need that.
This addition is also cheaper complexity wise this way.
Bikeshed
I picked
singlesince it strikes a balance between brevity and clarity.r? @alexcrichton