itertools
itertools copied to clipboard
Add array_chunks
Added an implementation of arrays based on the work in next_array(). Currently the N = 0 produces a post-monomorphization error, see the discussion in #1012.
Some other choices that I'm not sure about:
- If the number of items yielded by the input iterator is not a multiple of
Nthen there is exactly one allocation the first timenext()returnsNone. I don't think this is a performance issue but this means the method has to be gated behinduse_alloc. The alternative would be something like implementingIntoIteratorforArrayBuilder, which seems possible but the safety invariants get more complicated. A middle ground would be to only gate theremainder()method, but this seems worse. - Should
ArrayChunks::remainder()return a custom type, so that it can beExactSizeIterator? - Is it okay to not specify the behavior if the input iterator is not fused? I think the current implementation behaves slightly differently depending on whether the None is on a multiple of
N.
Codecov Report
Attention: Patch coverage is 91.25000% with 7 lines in your changes missing coverage. Please review.
Project coverage is 94.32%. Comparing base (
6814180) to head (db21b2f). Report is 137 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/arrays.rs | 82.92% | 7 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #1023 +/- ##
==========================================
- Coverage 94.38% 94.32% -0.07%
==========================================
Files 48 51 +3
Lines 6665 6963 +298
==========================================
+ Hits 6291 6568 +277
- Misses 374 395 +21
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Re the MSRV failure, I'm not sure what the idiomatic way is to trigger a PME on 1.63 based on N > 0.
Re the MSRV failure, I'm not sure what the idiomatic way is to trigger a PME on 1.63 based on
N > 0.
You can do it like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=233fb2f6ca3df52d15e3df96b8c4cb61
what are the thoughts about having an array_chunks which can fill the partially filled last item by fetching from a filler that the caller provides? I think it would be a very useful API in case people always want a certain length chunks
im curious why you didnt want to just copy the previous stdlib implementation? you can simply implement this on top of a manual as_chunks?
returning an owned array and using a vec for remainder is kind of funky.
@bend-n Do you mean slice::as_chunks? That works for slices, but this is for a general iterator.
There's also a nightly version of this in std: Iterator::array_chunks. If you're asking about the implementation there, I don't think the code in this PR is that different, just trying to reuse things already in the crate. But TBF I haven't looked at either that implementation or this PR closely in a while.
oh, my bad. i forgot that iterator::array_chunks was still unstable and assumed this was with regards to slice array chunks for some reason.
you could still copy the implementation of iterator::array_chunks, though?