Skip to content

Commit 79d2430

Browse files
committed
Add a doc note about why Chain is not ExactSizeIterator
1 parent 50d3ba5 commit 79d2430

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

library/core/src/iter/traits/exact_size.rs

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
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:

library/core/src/iter/traits/marker.rs

+11
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)