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