Concept Subspace :: sus :: iter :: TrustedLen
template <class T>concept TrustedLen
requires(const std::remove_cvref_t<T>& t) {
{ t.trusted_len() } -> std::same_as<__private::TrustedLenMarker>;
}
An iterator that reports an accurate length.
The iterator reports a size hint where it is either exact (lower bound is
equal to upper bound), or the upper bound is None. The upper bound must
only be None if the actual iterator length is larger than
usize::MAX
. In that case, the lower bound must be
usize::MAX
, resulting in an
Iterator::size_hint()
of (usize::MAX, None)
.
The iterator must produce exactly the number of elements it reported.
Implementing TrustedLen
To opt into implementing TrustedLen
a trusted_len() const
method should
return the TrustedLenMarker
type.
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>
isn't TrustedLen
, even
when I
implements TrustedLen
.
Safety
This trait must only be implemented when the contract is upheld. Consumers
of this trait must inspect Iterator::size_hint()
's upper bound.