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