Remove incorrect impl `TrustedLen` for `ArrayChunks`

As explained in the review of the previous attempt to add `ArrayChunks`,
adapters that shrink the length can't implement `TrustedLen`.
This commit is contained in:
Maybe Waffle 2022-08-01 18:30:55 +04:00
parent b8b14864c0
commit 4db628a801
2 changed files with 3 additions and 10 deletions

View File

@ -1,5 +1,5 @@
use crate::array;
use crate::iter::{Fuse, FusedIterator, Iterator, TrustedLen};
use crate::iter::{Fuse, FusedIterator, Iterator};
use crate::mem;
use crate::mem::MaybeUninit;
use crate::ops::{ControlFlow, Try};
@ -54,11 +54,7 @@ where
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (lower, upper) = self.iter.size_hint();
// Keep infinite iterator size hint lower bound as `usize::MAX`. This
// is required to implement `TrustedLen`.
if lower == usize::MAX {
return (lower, upper);
}
(lower / N, upper.map(|n| n / N))
}
@ -318,6 +314,3 @@ where
self.iter.len() / N == 0
}
}
#[unstable(feature = "trusted_len", issue = "37572")]
unsafe impl<I, const N: usize> TrustedLen for ArrayChunks<I, N> where I: TrustedLen {}

View File

@ -50,7 +50,7 @@ fn test_iterator_array_chunks_size_hint() {
assert_eq!(it.size_hint(), (0, Some(0)));
let it = (1..).array_chunks::<2>();
assert_eq!(it.size_hint(), (usize::MAX, None));
assert_eq!(it.size_hint(), (usize::MAX / 2, None));
let it = (1..).filter(|x| x % 2 != 0).array_chunks::<2>();
assert_eq!(it.size_hint(), (0, None));