core/iter/traits/marker.rs
1#[cfg(not(feature = "ferrocene_certified"))]
2use crate::iter::Step;
3#[cfg(not(feature = "ferrocene_certified"))]
4use crate::num::NonZero;
5
6/// Same as FusedIterator
7///
8/// # Safety
9///
10/// This is used for specialization. Therefore implementations must not
11/// be lifetime-dependent.
12#[unstable(issue = "none", feature = "trusted_fused")]
13#[doc(hidden)]
14#[rustc_specialization_trait]
15#[cfg(not(feature = "ferrocene_certified"))]
16pub unsafe trait TrustedFused {}
17
18/// An iterator that always continues to yield `None` when exhausted.
19///
20/// Calling next on a fused iterator that has returned `None` once is guaranteed
21/// to return [`None`] again. This trait should be implemented by all iterators
22/// that behave this way because it allows optimizing [`Iterator::fuse()`].
23///
24/// Note: In general, you should not use `FusedIterator` in generic bounds if
25/// you need a fused iterator. Instead, you should just call [`Iterator::fuse()`]
26/// on the iterator. If the iterator is already fused, the additional [`Fuse`]
27/// wrapper will be a no-op with no performance penalty.
28///
29/// [`Fuse`]: crate::iter::Fuse
30#[stable(feature = "fused", since = "1.26.0")]
31#[rustc_unsafe_specialization_marker]
32// FIXME: this should be a #[marker] and have another blanket impl for T: TrustedFused
33// but that ICEs iter::Fuse specializations.
34#[lang = "fused_iterator"]
35#[cfg(not(feature = "ferrocene_certified"))]
36pub trait FusedIterator: Iterator {}
37
38#[stable(feature = "fused", since = "1.26.0")]
39#[cfg(not(feature = "ferrocene_certified"))]
40impl<I: FusedIterator + ?Sized> FusedIterator for &mut I {}
41
42/// An iterator that reports an accurate length using size_hint.
43///
44/// The iterator reports a size hint where it is either exact
45/// (lower bound is equal to upper bound), or the upper bound is [`None`].
46/// The upper bound must only be [`None`] if the actual iterator length is
47/// larger than [`usize::MAX`]. In that case, the lower bound must be
48/// [`usize::MAX`], resulting in an [`Iterator::size_hint()`] of
49/// `(usize::MAX, None)`.
50///
51/// The iterator must produce exactly the number of elements it reported
52/// or diverge before reaching the end.
53///
54/// # When *shouldn't* an adapter be `TrustedLen`?
55///
56/// If an adapter makes an iterator *shorter* by a given amount, then it's
57/// usually incorrect for that adapter to implement `TrustedLen`.  The inner
58/// iterator might return more than `usize::MAX` items, but there's no way to
59/// know what `k` elements less than that will be, since the `size_hint` from
60/// the inner iterator has already saturated and lost that information.
61///
62/// This is why [`Skip<I>`](crate::iter::Skip) isn't `TrustedLen`, even when
63/// `I` implements `TrustedLen`.
64///
65/// # Safety
66///
67/// This trait must only be implemented when the contract is upheld. Consumers
68/// of this trait must inspect [`Iterator::size_hint()`]’s upper bound.
69#[unstable(feature = "trusted_len", issue = "37572")]
70#[rustc_unsafe_specialization_marker]
71pub unsafe trait TrustedLen: Iterator {}
72
73#[unstable(feature = "trusted_len", issue = "37572")]
74unsafe impl<I: TrustedLen + ?Sized> TrustedLen for &mut I {}
75
76/// An iterator that when yielding an item will have taken at least one element
77/// from its underlying [`SourceIter`].
78///
79/// Calling any method that advances the iterator, e.g.  [`next()`] or [`try_fold()`],
80/// guarantees that for each step at least one value of the iterator's underlying source
81/// has been moved out and the result of the iterator chain could be inserted
82/// in its place, assuming structural constraints of the source allow such an insertion.
83/// In other words this trait indicates that an iterator pipeline can be collected in place.
84///
85/// The primary use of this trait is in-place iteration. Refer to the [`vec::in_place_collect`]
86/// module documentation for more information.
87///
88/// [`vec::in_place_collect`]: ../../../../alloc/vec/in_place_collect/index.html
89/// [`SourceIter`]: crate::iter::SourceIter
90/// [`next()`]: Iterator::next
91/// [`try_fold()`]: Iterator::try_fold
92#[unstable(issue = "none", feature = "inplace_iteration")]
93#[doc(hidden)]
94#[rustc_specialization_trait]
95#[cfg(not(feature = "ferrocene_certified"))]
96pub unsafe trait InPlaceIterable {
97    /// The product of one-to-many item expansions that happen throughout the iterator pipeline.
98    /// E.g. [[u8; 4]; 4].iter().flatten().flatten() would have a `EXPAND_BY` of 16.
99    /// This is an upper bound, i.e. the transformations will produce at most this many items per
100    /// input. It's meant for layout calculations.
101    const EXPAND_BY: Option<NonZero<usize>>;
102    /// The product of many-to-one item reductions that happen throughout the iterator pipeline.
103    /// E.g. [u8].iter().array_chunks::<4>().array_chunks::<4>() would have a `MERGE_BY` of 16.
104    /// This is a lower bound, i.e. the transformations will consume at least this many items per
105    /// output.
106    const MERGE_BY: Option<NonZero<usize>>;
107}
108
109/// A type that upholds all invariants of [`Step`].
110///
111/// The invariants of [`Step::steps_between()`] are a superset of the invariants
112/// of [`TrustedLen`]. As such, [`TrustedLen`] is implemented for all range
113/// types with the same generic type argument.
114///
115/// # Safety
116///
117/// The implementation of [`Step`] for the given type must guarantee all
118/// invariants of all methods are upheld. See the [`Step`] trait's documentation
119/// for details. Consumers are free to rely on the invariants in unsafe code.
120#[unstable(feature = "trusted_step", issue = "85731")]
121#[rustc_specialization_trait]
122#[cfg(not(feature = "ferrocene_certified"))]
123pub unsafe trait TrustedStep: Step + Copy {}