-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 array::try_from_iter
to improve discoverability and ergonomics
#107979
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon. Please see the contribution instructions for more information. |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
@rustbot label +T-libs-api -T-libs |
@rustbot label -T-libs-api +T-libs |
/// ``` | ||
#[unstable(feature = "try_from_iter", issue = "none")] | ||
#[inline] | ||
pub fn try_from_iter<I, const N: usize>(iter: I) -> Result<[I::Item; N], IntoIter<I::Item, N>> |
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.
This consumes iter
, while next_chunk
takes a &mut iter
. Those are somewhat different semantics because it raises the question what happens when the remainder of the iterator could yield additional elements.
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.
yess that is true! i was wondering about this as well when choosing the API layout, but ended up going with taking ownership of iter
, as it seems to be the standard behavior of from
functions and is probably what users would expect. Your point is still very valid, as most other collections that can be created through some form of from
, From
, try_from
, or TryFrom
don't have this size constraint that [T; N]
has.
I'll add this to the RFC under Alterntives; yet, if this &mut self
ends up making more sense then changing it to that should not be to difficult!
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.
One could also maybe imagine returning something like Result<([I::Item; N], I::Iterator), IntoIter<I::Item, N>>
so that any remaining elements can be extracted from the original collection, but that might be a bit to contrived? It would mean you'd always have to expression match out of it, and would appear to severely reduce ergonomics.
☔ The latest upstream changes (presumably #107634) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
It was great to see the Drawbacks section of your RFC. Definitely my biggest question here would be how it interacts with Probably a bunch of this is related to optimization issues -- IIRC P.S. I added |
@scottmcm that is a valid question!
In terms implementation interaction, it really is just a wrapper, so any change to
As for its usefulness alongside
By default implementation, do you mean the default implementation of
By |
Ohh, I see, thank you haha! I was not quite sure which one applied here! That makes sense though! |
So, it's pretty rare that we have the same thing exposed in two ways. I can't actually think of a single example where one side is a normal function -- everything I can come up with it's traits on both sides. Let's look at a few and see if we can divine any patterns:
|
That is a detail I not considered, or really known about at all, but a good point. It may be to some extent due to me not having much experience using/resorting to
That pattern, of having an associated function first and then deriving (and maybe even specializing) a chain-able method is interesting. That was one of the things I enjoyed when first discovering Rust, the ability it gives you to do this functional-esque chaining. And them
That is also very valid and something I had not thought of!
Yess, I remember you mentioning that I your write up here #107634. Are you suggesting that the existing fn write_prefix_to_maybeuninit_buffer<'a>(
&mut self,
buf: &'a mut [MaybeUninit<Self::Item>],
) -> PartialInitDropGuard<'a, Self::Item>; sort of interface? That would be a very interesting change to make, and implement, though would it not mean that Just so I understand, Also, I really want to thank you for your super thoughtful response and patience with me haha! |
☔ The latest upstream changes (presumably #106934) made this pull request unmergeable. Please resolve the merge conflicts. |
As described above, I don't think I'm confident enough in this API to add it on my own initiative (not being a libs-api member), so I'm going to ask that you file an ACP https://std-dev-guide.rust-lang.org/feature-lifecycle/api-change-proposals.html to get it seconded if you're still interested in adding it. |
hey @scottmcm, thank you for looking and talking over this PR with me! the points you raised were all extremely valid! it makes complete sense that you would not want to make a decisions such as this! thank you for giving me the option of the ACP, i'll look into this !! i really really appreciate your feedback and views on this! :)) |
Hmm, so the ACP ended up with
So I'm not really sure what should happen here. I guess it's waiting on more exploration of alternatives before it can land? |
so since the ACP wasn't accepted, and no new ACP is created, it is better to close this till a new ACP is created, else it will remain stagnant. |
Hey everyone!
This is my first attempt at contributing to this project so please excuse all the faux pas I may be committing! It also means I will totally understand if this gets shot down as I am super happy to just learn! :)
In essence, this change takes advantage of the relatively new
iter::next_chunk
method to provide a more direct way of creating a fixed-size array[T; N]
from anyIntoIterator<Item = T>
. More precisely itarray::try_from_iter
takes anIntoIterator<Item = T>
which will result in a[T; N]
, if and only if theIntoIterator
holds>= N
items. Otherwise, it returns anarray::IntoIter<T>
with the originalIntoIterator
's items. The proposed method signature follows:It could be used as follows:
This is essentially just a wrapper around
iter::next_chunk
and its main purpose is to increase ergonomics and discoverability in an effort to reduce the use ofunsafe
in crates and code bases.Here is a more detailed RFC.