]> git.lizzy.rs Git - rust.git/blob - src/libcore/iter/traits/marker.rs
a9ba3908c38982a1e4014e0bf6efb09e4c068456
[rust.git] / src / libcore / 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 /// [`None`]: ../../std/option/enum.Option.html#variant.None
13 /// [`Iterator::fuse`]: ../../std/iter/trait.Iterator.html#method.fuse
14 /// [`Fuse`]: ../../std/iter/struct.Fuse.html
15 #[stable(feature = "fused", since = "1.26.0")]
16 #[rustc_unsafe_specialization_marker]
17 pub trait FusedIterator: Iterator {}
18
19 #[stable(feature = "fused", since = "1.26.0")]
20 impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
21
22 /// An iterator that reports an accurate length using size_hint.
23 ///
24 /// The iterator reports a size hint where it is either exact
25 /// (lower bound is equal to upper bound), or the upper bound is [`None`].
26 /// The upper bound must only be [`None`] if the actual iterator length is
27 /// larger than [`usize::MAX`]. In that case, the lower bound must be
28 /// [`usize::MAX`], resulting in a [`.size_hint`] of `(usize::MAX, None)`.
29 ///
30 /// The iterator must produce exactly the number of elements it reported
31 /// or diverge before reaching the end.
32 ///
33 /// # Safety
34 ///
35 /// This trait must only be implemented when the contract is upheld.
36 /// Consumers of this trait must inspect [`.size_hint`]’s upper bound.
37 ///
38 /// [`None`]: ../../std/option/enum.Option.html#variant.None
39 /// [`usize::MAX`]: ../../std/usize/constant.MAX.html
40 /// [`.size_hint`]: ../../std/iter/trait.Iterator.html#method.size_hint
41 #[unstable(feature = "trusted_len", issue = "37572")]
42 #[rustc_unsafe_specialization_marker]
43 pub unsafe trait TrustedLen: Iterator {}
44
45 #[unstable(feature = "trusted_len", issue = "37572")]
46 unsafe impl<I: TrustedLen + ?Sized> TrustedLen for &mut I {}