pub(crate) trait UncheckedIterator: TrustedLen {
    // Provided method
    unsafe fn next_unchecked(&mut self) -> Self::Item { ... }
}
Expand description

TrustedLen cannot have methods, so this allows augmenting it.

It currently requires TrustedLen because it’s unclear whether it’s reasonably possible to depend on the size_hint of anything else.

Provided Methods§

source

unsafe fn next_unchecked(&mut self) -> Self::Item

🔬This is a nightly-only experimental API. (trusted_len_next_unchecked #37572)

Gets the next item from a non-empty iterator.

Because there’s always a value to return, that means it can return the Item type directly, without wrapping it in an Option.

Safety

This can only be called if size_hint().0 != 0, guaranteeing that there’s at least one item available.

Otherwise (aka when size_hint().1 == Some(0)), this is UB.

Note to Implementers

This has a default implementation using Option::unwrap_unchecked. That’s probably sufficient if your next always returns Some, such as for infinite iterators. In more complicated situations, however, sometimes there can still be insertvalue/assume/extractvalue instructions remaining in the IR from the Option handling, at which point you might want to implement this manually instead.

Implementors§

source§

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

source§

impl<'a, T> UncheckedIterator for Iter<'a, T>

source§

impl<'a, T> UncheckedIterator for IterMut<'a, T>

source§

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

source§

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

source§

impl<T> UncheckedIterator for Drain<'_, T>