-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Tracking Issue for slice::array_chunks #74985
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
Comments
Add `slice::array_chunks_mut` This follows `array_chunks` from rust-lang#74373 with a mutable version, `array_chunks_mut`. The implementation is identical apart from mutability. The new tests are adaptations of the `chunks_exact_mut` tests, plus an inference test like the one for `array_chunks`. I reused the unstable feature `array_chunks` and tracking issue rust-lang#74985, but I can separate that if desired. r? `@withoutboats` cc `@lcnr`
Add array_windows fn This mimicks the functionality added by array_chunks, and implements a const-generic form of `windows`. It makes egregious use of `unsafe`, but by necessity because the array must be re-interpreted as a slice of arrays, and unlike array_chunks this cannot be done by casting the original array once, since each time the index is advanced it needs to move one element, not `N`. I'm planning on adding more tests, but this should be good enough as a premise for the functionality. Notably: should there be more functions overwritten for the iterator implementation/in general? ~~I've marked the issue as rust-lang#74985 as there is no corresponding exact issue for `array_windows`, but it's based of off `array_chunks`.~~ Edit: See Issue rust-lang#75027 created by @lcnr for tracking issue ~~Do not merge until I add more tests, please.~~ r? @lcnr
…lbertodt Add [T]::as_chunks(_mut) Allows getting the slices directly, rather than just through an iterator as in `array_chunks(_mut)`. The constructors for those iterators are then written in terms of these methods, so the iterator constructors no longer have any `unsafe` of their own. Unstable, of course. rust-lang#74985
Is there any intention of providing the reverse versions of these functions? |
What would that look like? |
Same API, but iterating backwards. I have a use case in which I need to convert either a prefix or a suffix of a slice to an array reference. This API is perfect for the prefix use case, and I'd love to be able to use it for the suffix use case as well. |
@joshlf |
That's only true if |
Ah, I misunderstood your use case, you want |
Yep, exactly 😃 |
Yeah, array_rchunks is definitely worth having. |
Also useful would be the inverse operation: |
Do we need to wait until |
Hi, is there any alternatives before this is stable? Thanks! |
Yeah |
Thanks! |
I personally didn't respond because to me, examples aren't what convinces me to prefer panics. Post-mono errors are what convinces me to prefer panics. (Because I think post-mono errors should be avoided at almost all costs.) |
I'll just second this point, actually, because I think it's a good one. I used to write
A really interesting (but slightly off-topic) thing this makes me think about: Okasaki chapter 9 is about numerical representations, and the equivalence between a way of writing a number and a container of that length. The simplest example is that a linked list is a unary representation of its own length. The book does a neat job of extending that to other things too, like what the container equivalent of a binary number would be -- and how the So from that perspective, |
This is getting very off-topic, but this equivalence is what I found so incredibly fascinating about lambda calculus. It's taught me that data is defined by how you interact with it – and these interactions are basically nothing but folds. |
@BurntSushi thanks, just note that I've read somewhere that post-mono errors are preferred over panics. I'm not sure if the stance on this changed yet. |
Perhaps you mean this comment?
|
@shepmaster correct, thanks! |
I would rather wait for the language features (e.g., pattern types) such that this check could become a proper pre-mono error. The standard library is forever. If we stabilize a sub-par API now, we will be stuck with it for as long as people keep using Rust. There is no reason to rush; people can always use an external crate if they need this feature now. |
FTR, I thought that post-mono errors can be changed to pre-mono but they can't: fn foo<const N: usize>(x: &[u8]) {
bar(x.as_chunks::<N>());
}
foo::<42>(y); Would become invalid if |
That's probably possible for the compiler to enforce though it feels ad-hoc. Cc @BoxyUwU It also feels like it'd be very annoying. I couldn't write functions as a user that wrap |
Very much possible, yeah 😁 |
In practice what happens is not “people pull in some well-known crate that does it properly” (a quick search found one crate that provides it but it has exactly one dependent). Instead, they either contort their code to make do with stable standard library functions (example I wrote myself recently) that already exist, or they just implement it themselves with some It’s not even obvious to me that there will be a clearly better way to have pre-mono errors in future Rust. There are vague ideas for what language features could enable them, but there’s no experience with how well they work in practice. So I don’t consider it a given that T-libs-api will say in five years “obviously the API should do XYZ to get a pre-mono error for N = 0”. Some way to put bounds on the values of const parameters will probably be needed eventually, but using it for preventing the specific error we’re talking about here may not be a good API design tradeoff after all. |
@RalfJung yes I suggest something like that upthread but it seems like libs is not interested in deferring supporting generic callers until we know how things will pan out with enforcing the |
I agree with the sentiment to shrimply postpone this until there's a better way to enforce |
And I don't think we'll never get mechanisms for enforcing that at compile-time, e.g. changing |
Note that even if |
If This wouldn't be the first case where a subset of a feature is stabilized because it is useful enough on it's own (see const-generics). |
I largely agree with @hanna-kruppe here with regards to postponing. Also, just more broadly speaking, the downside here is not significant from my perspective. We're not talking about a subtle footgun that could lead to incorrect answers. We're talking about a slightly worse failure mode for one particular corner case. That on its own isn't enough to delay a useful API like this indefinitely IMO. |
I agree that the present situation isn’t absolutely ideal. But I think the way to address it is something like Niko Matsakis’s “preview crates” idea, or the |
In this particular case I don’t think that’ll really help. I’m not avoiding depending on a crate for this functionality because of trust, but because these functions are so trivial (ca. five lines of code each) that the complication and overhead of discovering a crate and depending on it is not worth it compared to the alternatives I described above. A similar challenge applies on the Rust project side: who’s gonna be motivated to voluntarily take care of setting up a new repository and crate under the Rust project, publishing it, watch out for issues and PRs (unlikely in this specific case, but possible in theory), etc. when the code in question is so boring and the status quo is perfectly tolerable? |
Yup. Same. I would never depend on a micro-crate providing a function like this, no matter who built it. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
…lfJung indirect-const-stabilize the `exact_div` intrinsic See rust-lang#74985 (comment)
Rollup merge of rust-lang#139163 - scottmcm:stabilize-exact_div, r=RalfJung indirect-const-stabilize the `exact_div` intrinsic See rust-lang#74985 (comment)
…lfJung indirect-const-stabilize the `exact_div` intrinsic See rust-lang#74985 (comment)
The feature gate for the issue is
#![feature(array_chunks)]
.Also tracks
as_(r)chunks(_mut)
in#![feature(slice_as_chunks)]
-- these were FCPed starting at #74985 (comment) below.About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
RestrictMakeN
to values greater than 0 at compile time instead of using a runtime panic.<[T]>::array_*
methods fail to compile on 0 len arrays #99471N == 0
is unreachable due to conditionals Tracking Issue for slice::array_chunks #74985 (comment)slice_as_chunks
library feature #139656Unresolved Questions
[T]::array_chunks
really need to be an iterator? #76354Implementation history
slice::array_chunks
to std #74373slice::array_chunks_mut
#75021as_chunks{_mut}
in Add [T]::as_chunks(_mut) #76635as_rchunks{_mut}
and unchecked versions in Addas_rchunks
(and friends) to slices #78818as_(r)chunk
in constifyslice_as_chunks
(unstable) #111453The text was updated successfully, but these errors were encountered: