]> git.lizzy.rs Git - rust.git/blob - library/core/src/iter/traits/marker.rs
Rollup merge of #101308 - nerdypepper:feature/is-ascii-octdigit, r=joshtriplett
[rust.git] / library / core / src / iter / traits / marker.rs
1 use crate::iter::Step;
2
3 /// An iterator that always continues to yield `None` when exhausted.
4 ///
5 /// Calling next on a fused iterator that has returned `None` once is guaranteed
6 /// to return [`None`] again. This trait should be implemented by all iterators
7 /// that behave this way because it allows optimizing [`Iterator::fuse()`].
8 ///
9 /// Note: In general, you should not use `FusedIterator` in generic bounds if
10 /// you need a fused iterator. Instead, you should just call [`Iterator::fuse()`]
11 /// on the iterator. If the iterator is already fused, the additional [`Fuse`]
12 /// wrapper will be a no-op with no performance penalty.
13 ///
14 /// [`Fuse`]: crate::iter::Fuse
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 an [`Iterator::size_hint()`] of
29 /// `(usize::MAX, None)`.
30 ///
31 /// The iterator must produce exactly the number of elements it reported
32 /// or diverge before reaching the end.
33 ///
34 /// # Safety
35 ///
36 /// This trait must only be implemented when the contract is upheld. Consumers
37 /// of this trait must inspect [`Iterator::size_hint()`]’s upper bound.
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 any method that advances the iterator, e.g.  [`next()`] or [`try_fold()`],
49 /// guarantees that for each step at least one value of the iterator's underlying source
50 /// has been moved out and the result of the iterator chain could be inserted
51 /// in its place, assuming structural constraints of the source allow such an insertion.
52 /// In other words this trait indicates that an iterator pipeline can be collected in place.
53 ///
54 /// The primary use of this trait is in-place iteration. Refer to the [`vec::in_place_collect`]
55 /// module documentation for more information.
56 ///
57 /// [`vec::in_place_collect`]: ../../../../alloc/vec/in_place_collect/index.html
58 /// [`SourceIter`]: crate::iter::SourceIter
59 /// [`next()`]: Iterator::next
60 /// [`try_fold()`]: Iterator::try_fold
61 #[unstable(issue = "none", feature = "inplace_iteration")]
62 #[doc(hidden)]
63 pub unsafe trait InPlaceIterable: Iterator {}
64
65 /// A type that upholds all invariants of [`Step`].
66 ///
67 /// The invariants of [`Step::steps_between()`] are a superset of the invariants
68 /// of [`TrustedLen`]. As such, [`TrustedLen`] is implemented for all range
69 /// types with the same generic type argument.
70 ///
71 /// # Safety
72 ///
73 /// The implementation of [`Step`] for the given type must guarantee all
74 /// invariants of all methods are upheld. See the [`Step`] trait's documentation
75 /// for details. Consumers are free to rely on the invariants in unsafe code.
76 #[unstable(feature = "trusted_step", issue = "85731")]
77 #[rustc_specialization_trait]
78 pub unsafe trait TrustedStep: Step {}