-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Force exhaustion in iter::ArrayChunks::into_remainder #123406
Conversation
#[unstable(feature = "iter_array_chunks", reason = "recently added", issue = "100450")] | ||
#[inline] | ||
pub fn into_remainder(self) -> Option<array::IntoIter<I::Item, N>> { | ||
pub fn into_remainder(mut self) -> Option<array::IntoIter<I::Item, N>> { | ||
while let Some(_) = self.next() {} |
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.
while let Some(_) = self.next() {} | |
if self.remainder.is_none() { | |
while let Some(_) = self.next() {} | |
} |
If I: DoubleEndedIterator + ExactSizeIterator
(thus Self: DoubleEndedIterator
), and self.next_back()
has been called, and there is a remainder, then self.remainder
is already set to Some
, so we can skip exhausting the iterator. (Optimization , not required for correctness).
Hypothetically, specialization could be used to do the "fast" thing if I: DEI + ESI
regardless of if the remainder has already been found, but in general that would be unsound (I think?), since user implementations of DEI/ESI can have lifetime requirements.
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.
Nice catch regarding next_back. I'll implement it.
I didn't get your point about specialization.
ed0f1ce
to
d9a8903
Compare
pub fn into_remainder(self) -> Option<array::IntoIter<I::Item, N>> { | ||
pub fn into_remainder(mut self) -> Option<array::IntoIter<I::Item, N>> { | ||
if self.remainder.is_none() { | ||
while let Some(_) = self.next() {} |
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.
Hmm, normally we say to use for_each(drop)
for this
rust/library/core/src/iter/traits/iterator.rs
Line 1996 in 0e15f5e
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"] |
but I guess this can't use that because it consumes it :/
Maybe it should be
while let Some(_) = self.next() {} | |
self.try_for_each(NeverShortCircuit::wrap_mut_1(drop)); |
?
Meh, we can always change it later if it ends up mattering. This is probably fine.
Thanks for the fix! I wondered for a bit about what would happen with this after exhaustion or if it's called twice, but then I remembered it's @bors r+ |
…cottmcm Force exhaustion in iter::ArrayChunks::into_remainder Closes: rust-lang#123333
…cottmcm Force exhaustion in iter::ArrayChunks::into_remainder Closes: rust-lang#123333
…kingjubilee Rollup of 9 pull requests Successful merges: - rust-lang#117919 (Introduce perma-unstable `wasm-c-abi` flag) - rust-lang#123406 (Force exhaustion in iter::ArrayChunks::into_remainder) - rust-lang#123752 (Properly handle emojis as literal prefix in macros) - rust-lang#123935 (Don't inline integer literals when they overflow - new attempt) - rust-lang#123980 ( Add an opt-in to store incoming edges in `VecGraph` + misc) - rust-lang#124019 (Use raw-dylib for Windows synchronization functions) - rust-lang#124110 (Fix negating `f16` and `f128` constants) - rust-lang#124112 (Fix ICE when there is a non-Unicode entry in the incremental crate directory) - rust-lang#124116 (when suggesting RUST_BACKTRACE=1, add a special note for Miri's env var isolation) r? `@ghost` `@rustbot` modify labels: rollup
…kingjubilee Rollup of 7 pull requests Successful merges: - rust-lang#123406 (Force exhaustion in iter::ArrayChunks::into_remainder) - rust-lang#123752 (Properly handle emojis as literal prefix in macros) - rust-lang#123935 (Don't inline integer literals when they overflow - new attempt) - rust-lang#123980 ( Add an opt-in to store incoming edges in `VecGraph` + misc) - rust-lang#124019 (Use raw-dylib for Windows synchronization functions) - rust-lang#124110 (Fix negating `f16` and `f128` constants) - rust-lang#124116 (when suggesting RUST_BACKTRACE=1, add a special note for Miri's env var isolation) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#123406 - krtab:fix_remainder_iterchunk, r=scottmcm Force exhaustion in iter::ArrayChunks::into_remainder Closes: rust-lang#123333
Closes: #123333