Trait core::iter::TrustedLen

source ·
pub unsafe trait TrustedLen: Iterator { }
🔬This is a nightly-only experimental API. (trusted_len #37572)
Expand description

An iterator that reports an accurate length using size_hint.

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 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> 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.

Implementors§

source§

impl TrustedLen for Bytes<'_>

source§

impl TrustedLen for StepBy<Range<u8>>

Safety: This macro is only applied to ranges over types <= usize which means the inner length is guaranteed to fit into a usize and so the outer length calculation won’t encounter clamped values

source§

impl TrustedLen for StepBy<Range<u16>>

Safety: This macro is only applied to ranges over types <= usize which means the inner length is guaranteed to fit into a usize and so the outer length calculation won’t encounter clamped values

source§

impl TrustedLen for StepBy<Range<u32>>

Safety: This macro is only applied to ranges over types <= usize which means the inner length is guaranteed to fit into a usize and so the outer length calculation won’t encounter clamped values

source§

impl TrustedLen for StepBy<Range<u64>>

Safety: This macro is only applied to ranges over types <= usize which means the inner length is guaranteed to fit into a usize and so the outer length calculation won’t encounter clamped values

source§

impl TrustedLen for StepBy<Range<usize>>

Safety: This macro is only applied to ranges over types <= usize which means the inner length is guaranteed to fit into a usize and so the outer length calculation won’t encounter clamped values

source§

impl<'a, I, T> TrustedLen for Cloned<I>
where I: TrustedLen<Item = &'a T>, T: Clone + 'a,

1.36.0 · source§

impl<'a, I, T> TrustedLen for Copied<I>
where I: TrustedLen<Item = &'a T>, T: Copy + 'a,

source§

impl<A> TrustedLen for core::option::IntoIter<A>

source§

impl<A> TrustedLen for core::option::Iter<'_, A>

source§

impl<A> TrustedLen for core::option::IterMut<'_, A>

source§

impl<A> TrustedLen for core::result::IntoIter<A>

source§

impl<A> TrustedLen for core::result::Iter<'_, A>

source§

impl<A> TrustedLen for core::result::IterMut<'_, A>

source§

impl<A, B> TrustedLen for Chain<A, B>
where A: TrustedLen, B: TrustedLen<Item = A::Item>,

source§

impl<A, B> TrustedLen for Zip<A, B>
where A: TrustedLen, B: TrustedLen,

source§

impl<A, F: FnMut() -> A> TrustedLen for RepeatWith<F>

1.43.0 · source§

impl<A, F: FnOnce() -> A> TrustedLen for OnceWith<F>

source§

impl<A: Clone> TrustedLen for Repeat<A>

source§

impl<A: TrustedStep> TrustedLen for Range<A>

source§

impl<A: TrustedStep> TrustedLen for RangeFrom<A>

source§

impl<A: TrustedStep> TrustedLen for RangeInclusive<A>

source§

impl<B, I, F> TrustedLen for Map<I, F>
where I: TrustedLen, F: FnMut(I::Item) -> B,

source§

impl<I> TrustedLen for Enumerate<I>
where I: TrustedLen,

source§

impl<I> TrustedLen for Flatten<I>
where I: Iterator<Item: IntoIterator>, FlattenCompat<I, <I::Item as IntoIterator>::IntoIter>: TrustedLen,

source§

impl<I> TrustedLen for Fuse<I>
where I: TrustedLen,

source§

impl<I> TrustedLen for Peekable<I>
where I: TrustedLen,

source§

impl<I> TrustedLen for Rev<I>

source§

impl<I, U, F> TrustedLen for FlatMap<I, U, F>
where I: Iterator, U: IntoIterator, F: FnMut(I::Item) -> U, FlattenCompat<Map<I, F>, <U as IntoIterator>::IntoIter>: TrustedLen,

source§

impl<I: TrustedLen + ?Sized> TrustedLen for &mut I

source§

impl<I: TrustedLen> TrustedLen for Take<I>

source§

impl<T> TrustedLen for Chunks<'_, T>

source§

impl<T> TrustedLen for ChunksExact<'_, T>

source§

impl<T> TrustedLen for ChunksExactMut<'_, T>

source§

impl<T> TrustedLen for ChunksMut<'_, T>

source§

impl<T> TrustedLen for core::slice::Iter<'_, T>

source§

impl<T> TrustedLen for core::slice::IterMut<'_, T>

source§

impl<T> TrustedLen for RChunks<'_, T>

source§

impl<T> TrustedLen for RChunksExact<'_, T>

source§

impl<T> TrustedLen for RChunksExactMut<'_, T>

source§

impl<T> TrustedLen for RChunksMut<'_, T>

source§

impl<T> TrustedLen for Windows<'_, T>

source§

impl<T> TrustedLen for Empty<T>

source§

impl<T> TrustedLen for Once<T>

1.40.0 · source§

impl<T, const N: usize> TrustedLen for core::array::IntoIter<T, N>

source§

impl<T, const N: usize> TrustedLen for ArrayChunks<'_, T, N>

source§

impl<T, const N: usize> TrustedLen for ArrayChunksMut<'_, T, N>