File tree 2 files changed +21
-0
lines changed
library/core/src/iter/traits
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 21
21
///
22
22
/// [`len`]: ExactSizeIterator::len
23
23
///
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
+ ///
24
34
/// # Examples
25
35
///
26
36
/// Basic usage:
Original file line number Diff line number Diff line change @@ -31,6 +31,17 @@ impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
31
31
/// The iterator must produce exactly the number of elements it reported
32
32
/// or diverge before reaching the end.
33
33
///
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
+ ///
34
45
/// # Safety
35
46
///
36
47
/// This trait must only be implemented when the contract is upheld. Consumers
You can’t perform that action at this time.
0 commit comments