Uh oh!
There was an error while loading. Please reload this page.
Add slice::{split_,}{first,last}_chunk{,_mut} - #95198
Conversation
rust-highfive
commented
Mar 22, 2022
r? @yaahc (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
scottmcm
commented
Mar 22, 2022
I've definitely written a bunch of examples that did I don't know if this is how I'd expect it to look, though. For example, the indexing could be left to subslicing, like Of course, the other thing that comes to mind is wanting |
clarfonthey
commented
Mar 22, 2022
Hmm, you do have a point about subslicing; it might be more natural to have Essentially, we'd mimic the |
scottmcm
commented
Mar 22, 2022
I don't know what the best approach would be here. Might be worth seeing if libs-api has any specific preferences. The whole "add const generic versions of most of the slice stuff" is a big project, but seems useful, so maybe they could have some generalized guidance of how they'd like to see it done? If that existed then I'd be happy to sign off on a bunch of individual PRs for adding the various pieces. |
That's incredibly fair. Since I was feeling particularly motivated, I decided to go ahead and implement the first/last chunk semantics that I mentioned and maybe that can help explain how this would work out. I personally find the result rather intuitive, although the chunk naming will probably need work before stabilisation. Hopefully seeing how this would work in code will help iron out that process. EDIT: And I finished modifying the PR description to properly summarise the new API and the use cases it allows. |
There was a problem hiding this comment.
I genuinely have no idea how this wasn't required before, since the existing implementation of split_at requires split_at_unchecked ??
Anyway, I decided to reuse this code for these methods and needed this.
scottmcm
commented
Mar 22, 2022
These look great to me, but apparently I was blind looking at the rustdoc, as I forgot about https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.split_array_mut from #90091 (comment) . I personally like these better, because they solve the |
clarfonthey
commented
Mar 22, 2022
Yeah, it's always a challenge to figure out what's there when there are so many methods and they're not really sorted in any logical way. But, I guess, that's a challenge for another day. Before merge, we can maybe decide which tracking issue this should go in, since, in hindsight, it makes the most sense for it to have its own tracking issue. |
jethrogb
commented
Mar 23, 2022
I don't think |
bors
commented
Mar 26, 2022
☔ The latest upstream changes (presumably #95274) made this pull request unmergeable. Please resolve the merge conflicts. |
Dylan-DPC
commented
Apr 25, 2022
r? @rust-lang/libs-api |
Nominating for libs-api to try to unblock this. (Since it just came up on URLO.) Should we just merge to nightly as fine for unstable? Do you have strong feelings about the direction to take for "get arrays from slices" APIs? Does this need an ACP/RFC now? (Of course ACPs didn't exist when this PR was opened.) |
bors
commented
Aug 26, 2022
☔ The latest upstream changes (presumably #101017) made this pull request unmergeable. Please resolve the merge conflicts. |
GKFX
commented
Nov 11, 2022
With the APIs proposed, I don't see a nice way to extract a subslice at arbitrary offset (and fixed length) as an array and get an Option without panicking. impl[T]{pubconstfnget_array<constN:usize>(&self,start_idx:usize) -> Option<&[T;N]>;pubconstfnget_array_mut<constN:usize>(&mutself,start_idx:usize) -> Option<&mut[T;N]>;}I think it's acceptable to have to specify a zero to get the first chunk:
The methods |
Looks like the ACP was accepted! 🎉 @clarfonthey Can you rebase and resolve conflicts and such please? |
2baf530 to
1974c7fCompare
This comment has been minimized.
This comment has been minimized.
clarfonthey
commented
May 19, 2023
(Going to wait on final CI run to pass the torch, but things should be rebased now. The biggest change I had to make was using |
clarfonthey
commented
May 21, 2023
@rustbot ready |
| #[unstable(feature = "slice_first_last_chunk", issue = "111774")] | ||
| #[rustc_const_unstable(feature = "slice_first_last_chunk", issue = "111774")] | ||
| #[inline] | ||
| pub const fn split_last_chunk<const N: usize>(&self) -> Option<(&[T; N], &[T])> { |
There was a problem hiding this comment.
Hmm, I would have expected this to be -> Option<(&[T], &[T; N])>, but this is consistent with split_last, so I guess it's fine for now. I'll put a note in the tracking issue.
There was a problem hiding this comment.
Yeah, there are arguments for both but honestly, I don't feel too swayed either way. I think that the potential issues here are greater because of how the types are only nominally different and can both be coerced to be the same, although I don't think there's a whole much that can be done about that than encourage folks to pay more attention.
There was a problem hiding this comment.
Yeah, I'm worried that someone might try something that boils down to let (head, tail) = x.split_last_chunk(); frobble(tail[0]) and being confused when it compiles (as you said) but doesn't do what they expected.
There was a problem hiding this comment.
I think that should probably be mentioned on the tracking issue, because I would write something like that... 😅
There was a problem hiding this comment.
@WaffleLapkin Is the note in #111774 (comment) not sufficient?
scottmcm
commented
May 24, 2023
Thanks! I'm excited to have these. @bors r+ rollup |
bors
commented
May 24, 2023
…iaskrgr Rollup of 4 pull requests Successful merges: - rust-lang#95198 (Add slice::{split_,}{first,last}_chunk{,_mut}) - rust-lang#109899 (Use apple-m1 as target CPU for aarch64-apple-darwin.) - rust-lang#111624 (Emit diagnostic for privately uninhabited uncovered witnesses.) - rust-lang#111875 (Don't leak the function that is called on drop) r? `@ghost` `@rustbot` modify labels: rollup
This adds to the existing tracking issue for
slice::array_chunks(#74985) under a separate feature,slice_get_chunk.Currently, we have the existing
first/lastAPI for slices:This augments it with a
first_chunk/last_chunkAPI that allows retrieving multiple elements at once:The code is based off of a copy of the existing API, with the documentation and examples properly modified. Currently, the most common way to perform these kinds of lookups with the existing methods is via
slice.as_chunks::<N>().0[0]or the worseslice.as_chunks::<N>().0[slice.len() - N], which is substantially less readable thanslice.first_chunk::<N>()orslice.last_chunk::<N>().ACP: rust-lang/libs-team#69