]> git.lizzy.rs Git - rust.git/blob - library/core/src/iter/traits/marker.rs
Use intra-doc links in core/src/iter when possible
[rust.git] / library / core / src / iter / traits / marker.rs
1 /// An iterator that always continues to yield `None` when exhausted.
2 ///
3 /// Calling next on a fused iterator that has returned `None` once is guaranteed
4 /// to return [`None`] again. This trait should be implemented by all iterators
5 /// that behave this way because it allows optimizing [`Iterator::fuse()`].
6 ///
7 /// Note: In general, you should not use `FusedIterator` in generic bounds if
8 /// you need a fused iterator. Instead, you should just call [`Iterator::fuse()`]
9 /// on the iterator. If the iterator is already fused, the additional [`Fuse`]
10 /// wrapper will be a no-op with no performance penalty.
11 ///
12 /// [`Fuse`]: crate::iter::Fuse
13 #[stable(feature = "fused", since = "1.26.0")]
14 #[rustc_unsafe_specialization_marker]
15 pub trait FusedIterator: Iterator {}
16
17 #[stable(feature = "fused", since = "1.26.0")]
18 impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
19
20 /// An iterator that reports an accurate length using size_hint.
21 ///
22 /// The iterator reports a size hint where it is either exact
23 /// (lower bound is equal to upper bound), or the upper bound is [`None`].
24 /// The upper bound must only be [`None`] if the actual iterator length is
25 /// larger than [`usize::MAX`]. In that case, the lower bound must be
26 /// [`usize::MAX`], resulting in a [`Iterator::size_hint()`] of
27 /// `(usize::MAX, None)`.
28 ///
29 /// The iterator must produce exactly the number of elements it reported
30 /// or diverge before reaching the end.
31 ///
32 /// # Safety
33 ///
34 /// This trait must only be implemented when the contract is upheld. Consumers
35 /// of this trait must inspect [`Iterator::size_hint()`]’s upper bound.
36 ///
37 /// [`usize::MAX`]: crate::usize::MAX
38 #[unstable(feature = "trusted_len", issue = "37572")]
39 #[rustc_unsafe_specialization_marker]
40 pub unsafe trait TrustedLen: Iterator {}
41
42 #[unstable(feature = "trusted_len", issue = "37572")]
43 unsafe impl<I: TrustedLen + ?Sized> TrustedLen for &mut I {}
44
45 /// An iterator that when yielding an item will have taken at least one element
46 /// from its underlying [`SourceIter`].
47 ///
48 /// Calling [`next()`] guarantees that at least one value of the iterator's underlying source
49 /// has been moved out and the result of the iterator chain could be inserted in its place,
50 /// assuming structural constraints of the source allow such an insertion.
51 /// In other words this trait indicates that an iterator pipeline can be collected in place.
52 ///
53 /// [`SourceIter`]: crate::iter::SourceIter
54 /// [`next()`]: Iterator::next
55 #[unstable(issue = "none", feature = "inplace_iteration")]
56 pub unsafe trait InPlaceIterable: Iterator {}