Trait SourceIter
pub unsafe trait SourceIter
This trait provides transitive access to source-stage in an iterator-adapter pipeline under the conditions that
- the iterator source
Sitself implementsSourceIter<Source = S> - there is a delegating implementation of this trait for each adapter in the pipeline between the source and the pipeline consumer.
When the source is an owning iterator struct (commonly called IntoIter) then
this can be useful for specializing FromIterator implementations or recovering the
remaining elements after an iterator has been partially exhausted.
Note that implementations do not necessarily have to provide access to the innermost source of a pipeline. A stateful intermediate adapter might eagerly evaluate a part of the pipeline and expose its internal storage as source.
The trait is unsafe because implementers must uphold additional safety properties.
See as_inner for details.
The primary use of this trait is in-place iteration. Refer to the vec::in_place_collect
module documentation for more information.
Examples
Retrieving a partially consumed source:
#
# use SourceIter;
let mut iter = vec!.into_iter.map;
let _ = iter.next;
let mut remainder = replace;
println!;
Associated Types
type Source;A source stage in an iterator pipeline.
Required Methods
unsafe fn as_inner(&mut self) -> &mut Self::SourceRetrieve the source of an iterator pipeline.
Safety
Implementations must return the same mutable reference for their lifetime, unless replaced by a caller.
Callers may only replace the reference when they stopped iteration and drop the iterator pipeline after extracting the source.
This means iterator adapters can rely on the source not changing during iteration but they cannot rely on it in their Drop implementations.
Implementing this method means adapters relinquish private-only access to their source and can only rely on guarantees made based on method receiver types. The lack of restricted access also requires that adapters must uphold the source's public API even when they have access to its internals.
Callers in turn must expect the source to be in any state that is consistent with its public API since adapters sitting between it and the source have the same access. In particular an adapter may have consumed more elements than strictly necessary.
The overall goal of these requirements is to let the consumer of a pipeline use
- whatever remains in the source after iteration has stopped
- the memory that has become unused by advancing a consuming iterator
Implementors
impl<A, B> SourceIter for Zip<A, B> where A: SourceIter,impl<I> SourceIter for Cloned<I> where I: SourceIter,impl<I> SourceIter for Copied<I> where I: SourceIter,impl<I> SourceIter for Enumerate<I> where I: SourceIter,impl<I> SourceIter for Flatten<I> where I: SourceIter + TrustedFused + Iterator, <I as Iterator>::Item: IntoIterator,impl<I> SourceIter for Fuse<I> where I: SourceIter + TrustedFused,impl<I> SourceIter for Peekable<I> where I: SourceIter + Iterator,impl<I> SourceIter for Skip<I> where I: SourceIter,impl<I> SourceIter for Take<I> where I: SourceIter,impl<I, F> SourceIter for FilterMap<I, F> where I: SourceIter,impl<I, F> SourceIter for Inspect<I, F> where I: SourceIter,impl<I, F> SourceIter for Map<I, F> where I: SourceIter,impl<I, P> SourceIter for MapWhile<I, P> where I: SourceIter,impl<I, R> SourceIter for GenericShunt<'_, I, R> where I: SourceIter,impl<I, U, F> SourceIter for FlatMap<I, U, F> where I: SourceIter + TrustedFused, U: IntoIterator,impl<I, const N: usize> SourceIter for ArrayChunks<I, N> where I: SourceIter + Iterator,impl<P, I> SourceIter for Filter<I, P> where I: SourceIter,impl<P, I> SourceIter for SkipWhile<I, P> where I: SourceIter,impl<P, I> SourceIter for TakeWhile<I, P> where I: SourceIter,impl<St, F, I> SourceIter for Scan<I, St, F> where I: SourceIter,