mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-31 14:31:55 +00:00
Add a doc note about why Chain
is not ExactSizeIterator
This commit is contained in:
parent
50d3ba5bcb
commit
79d2430e99
@ -21,6 +21,16 @@
|
||||
///
|
||||
/// [`len`]: ExactSizeIterator::len
|
||||
///
|
||||
/// # When *shouldn't* an adapter be `ExactSizeIterator`?
|
||||
///
|
||||
/// If an adapter makes an iterator *longer*, then it's usually incorrect for
|
||||
/// that adapter to implement `ExactSizeIterator`. The inner exact-sized
|
||||
/// iterator might already be `usize::MAX`-long, and thus the length of the
|
||||
/// longer adapted iterator would no longer be exactly representable in `usize`.
|
||||
///
|
||||
/// This is why [`Chain<A, B>`](crate::iter::Chain) isn't `ExactSizeIterator`,
|
||||
/// even when `A` and `B` are both `ExactSizeIterator`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Basic usage:
|
||||
|
@ -31,6 +31,17 @@ impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
|
||||
/// The iterator must produce exactly the number of elements it reported
|
||||
/// or diverge before reaching the end.
|
||||
///
|
||||
/// # When *shouldn't* an adapter be `TrustedLen`?
|
||||
///
|
||||
/// If an adapter makes an iterator *shorter* by a given amount, then it's
|
||||
/// usually incorrect for that adapter to implement `TrustedLen`. The inner
|
||||
/// iterator might return more than `usize::MAX` items, but there's no way to
|
||||
/// know what `k` elements less than that will be, since the `size_hint` from
|
||||
/// the inner iterator has already saturated and lost that information.
|
||||
///
|
||||
/// This is why [`Skip<I>`](crate::iter::Skip) isn't `TrustedLen`, even when
|
||||
/// `I` implements `TrustedLen`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// This trait must only be implemented when the contract is upheld. Consumers
|
||||
|
Loading…
Reference in New Issue
Block a user