This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
library/core/src/iter/traits Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 2121///
2222/// [`len`]: ExactSizeIterator::len
2323///
24+ /// # When *shouldn't* an adapter be `ExactSizeIterator`?
25+ ///
26+ /// If an adapter makes an iterator *longer*, then it's usually incorrect for
27+ /// that adapter to implement `ExactSizeIterator`. The inner exact-sized
28+ /// iterator might already be `usize::MAX`-long, and thus the length of the
29+ /// longer adapted iterator would no longer be exactly representable in `usize`.
30+ ///
31+ /// This is why [`Chain<A, B>`](crate::iter::Chain) isn't `ExactSizeIterator`,
32+ /// even when `A` and `B` are both `ExactSizeIterator`.
33+ ///
2434/// # Examples
2535///
2636/// Basic usage:
Original file line number Diff line number Diff line change @@ -31,6 +31,17 @@ impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
3131/// The iterator must produce exactly the number of elements it reported
3232/// or diverge before reaching the end.
3333///
34+ /// # When *shouldn't* an adapter be `TrustedLen`?
35+ ///
36+ /// If an adapter makes an iterator *shorter* by a given amount, then it's
37+ /// usually incorrect for that adapter to implement `TrustedLen`. The inner
38+ /// iterator might return more than `usize::MAX` items, but there's no way to
39+ /// know what `k` elements less than that will be, since the `size_hint` from
40+ /// the inner iterator has already saturated and lost that information.
41+ ///
42+ /// This is why [`Skip<I>`](crate::iter::Skip) isn't `TrustedLen`, even when
43+ /// `I` implements `TrustedLen`.
44+ ///
3445/// # Safety
3546///
3647/// This trait must only be implemented when the contract is upheld. Consumers
You can’t perform that action at this time.
0 commit comments