1 use crate::iter::{DoubleEndedIterator, FusedIterator, Iterator, TrustedLen};
5 /// An iterator that links two iterators together, in a chain.
7 /// This `struct` is created by [`Iterator::chain`]. See its documentation
9 #[derive(Clone, Debug)]
10 #[must_use = "iterators are lazy and do nothing unless consumed"]
11 #[stable(feature = "rust1", since = "1.0.0")]
12 pub struct Chain<A, B> {
13 // These are "fused" with `Option` so we don't need separate state to track which part is
14 // already exhausted, and we may also get niche layout for `None`. We don't use the real `Fuse`
15 // adapter because its specialization for `FusedIterator` unconditionally descends into the
16 // iterator, and that could be expensive to keep revisiting stuff like nested chains. It also
17 // hurts compiler performance to add more iterator layers to `Chain`.
19 // Only the "first" iterator is actually set `None` when exhausted, depending on whether you
20 // iterate forward or backward. If you mix directions, then both sides may be `None`.
24 impl<A, B> Chain<A, B> {
25 pub(in super::super) fn new(a: A, b: B) -> Chain<A, B> {
26 Chain { a: Some(a), b: Some(b) }
30 /// Fuse the iterator if the expression is `None`.
32 ($self:ident . $iter:ident . $($call:tt)+) => {
34 Some(ref mut iter) => match iter.$($call)+ {
46 /// Try an iterator method without fusing,
47 /// like an inline `.as_mut().and_then(...)`
49 ($self:ident . $iter:ident . $($call:tt)+) => {
51 Some(ref mut iter) => iter.$($call)+,
57 #[stable(feature = "rust1", since = "1.0.0")]
58 impl<A, B> Iterator for Chain<A, B>
61 B: Iterator<Item = A::Item>,
66 fn next(&mut self) -> Option<A::Item> {
67 match fuse!(self.a.next()) {
68 None => maybe!(self.b.next()),
74 #[rustc_inherit_overflow_checks]
75 fn count(self) -> usize {
76 let a_count = match self.a {
80 let b_count = match self.b {
87 fn try_fold<Acc, F, R>(&mut self, mut acc: Acc, mut f: F) -> R
90 F: FnMut(Acc, Self::Item) -> R,
93 if let Some(ref mut a) = self.a {
94 acc = a.try_fold(acc, &mut f)?;
97 if let Some(ref mut b) = self.b {
98 acc = b.try_fold(acc, f)?;
99 // we don't fuse the second iterator
104 fn fold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc
106 F: FnMut(Acc, Self::Item) -> Acc,
108 if let Some(a) = self.a {
109 acc = a.fold(acc, &mut f);
111 if let Some(b) = self.b {
112 acc = b.fold(acc, f);
118 fn nth(&mut self, mut n: usize) -> Option<A::Item> {
119 if let Some(ref mut a) = self.a {
120 while let Some(x) = a.next() {
128 maybe!(self.b.nth(n))
132 fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item>
134 P: FnMut(&Self::Item) -> bool,
136 match fuse!(self.a.find(&mut predicate)) {
137 None => maybe!(self.b.find(predicate)),
143 fn last(self) -> Option<A::Item> {
144 // Must exhaust a before b.
145 let a_last = match self.a {
149 let b_last = match self.b {
157 fn size_hint(&self) -> (usize, Option<usize>) {
159 Chain { a: Some(a), b: Some(b) } => {
160 let (a_lower, a_upper) = a.size_hint();
161 let (b_lower, b_upper) = b.size_hint();
163 let lower = a_lower.saturating_add(b_lower);
165 let upper = match (a_upper, b_upper) {
166 (Some(x), Some(y)) => x.checked_add(y),
172 Chain { a: Some(a), b: None } => a.size_hint(),
173 Chain { a: None, b: Some(b) } => b.size_hint(),
174 Chain { a: None, b: None } => (0, Some(0)),
179 #[stable(feature = "rust1", since = "1.0.0")]
180 impl<A, B> DoubleEndedIterator for Chain<A, B>
182 A: DoubleEndedIterator,
183 B: DoubleEndedIterator<Item = A::Item>,
186 fn next_back(&mut self) -> Option<A::Item> {
187 match fuse!(self.b.next_back()) {
188 None => maybe!(self.a.next_back()),
194 fn nth_back(&mut self, mut n: usize) -> Option<A::Item> {
195 if let Some(ref mut b) = self.b {
196 while let Some(x) = b.next_back() {
204 maybe!(self.a.nth_back(n))
208 fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
210 P: FnMut(&Self::Item) -> bool,
212 match fuse!(self.b.rfind(&mut predicate)) {
213 None => maybe!(self.a.rfind(predicate)),
218 fn try_rfold<Acc, F, R>(&mut self, mut acc: Acc, mut f: F) -> R
221 F: FnMut(Acc, Self::Item) -> R,
224 if let Some(ref mut b) = self.b {
225 acc = b.try_rfold(acc, &mut f)?;
228 if let Some(ref mut a) = self.a {
229 acc = a.try_rfold(acc, f)?;
230 // we don't fuse the second iterator
235 fn rfold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc
237 F: FnMut(Acc, Self::Item) -> Acc,
239 if let Some(b) = self.b {
240 acc = b.rfold(acc, &mut f);
242 if let Some(a) = self.a {
243 acc = a.rfold(acc, f);
249 // Note: *both* must be fused to handle double-ended iterators.
250 #[stable(feature = "fused", since = "1.26.0")]
251 impl<A, B> FusedIterator for Chain<A, B>
254 B: FusedIterator<Item = A::Item>,
258 #[unstable(feature = "trusted_len", issue = "37572")]
259 unsafe impl<A, B> TrustedLen for Chain<A, B>
262 B: TrustedLen<Item = A::Item>,