core/iter/traits/
marker.rs

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