Skip to content

Commit a66cd5b

Browse files
committed
specialize Zip::size_hint for TrustedRandomAccess
1 parent e6d6e5c commit a66cd5b

File tree

1 file changed

+25
-17
lines changed
  • library/core/src/iter/adapters

1 file changed

+25
-17
lines changed

library/core/src/iter/adapters/zip.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,7 @@ where
7777

7878
#[inline]
7979
fn size_hint(&self) -> (usize, Option<usize>) {
80-
// for TRA:
81-
//let size = cmp::min(self.a.size(), self.b.size());
82-
//(size, Some(size))
83-
84-
let (a_lower, a_upper) = self.a.size_hint();
85-
let (b_lower, b_upper) = self.b.size_hint();
86-
87-
let lower = cmp::min(a_lower, b_lower);
88-
89-
let upper = match (a_upper, b_upper) {
90-
(Some(x), Some(y)) => Some(cmp::min(x, y)),
91-
(Some(x), None) => Some(x),
92-
(None, Some(y)) => Some(y),
93-
(None, None) => None,
94-
};
95-
96-
(lower, upper)
80+
self.spec_size_hint()
9781
}
9882

9983
fn fold<T, F>(self, init: T, f: F) -> T
@@ -158,6 +142,7 @@ trait ZipSpec: Iterator {
158142
F: FnMut(B, Self::Item) -> R,
159143
R: Try<Output = B>;
160144

145+
fn spec_size_hint(&self) -> (usize, Option<usize>);
161146
}
162147

163148
impl<A, B> ZipSpec for Zip<A, B>
@@ -189,6 +174,23 @@ where
189174
}
190175
try { accum }
191176
}
177+
178+
#[inline]
179+
default fn spec_size_hint(&self) -> (usize, Option<usize>) {
180+
let (a_lower, a_upper) = self.a.size_hint();
181+
let (b_lower, b_upper) = self.b.size_hint();
182+
183+
let lower = cmp::min(a_lower, b_lower);
184+
185+
let upper = match (a_upper, b_upper) {
186+
(Some(x), Some(y)) => Some(cmp::min(x, y)),
187+
(Some(x), None) => Some(x),
188+
(None, Some(y)) => Some(y),
189+
(None, None) => None,
190+
};
191+
192+
(lower, upper)
193+
}
192194
}
193195

194196
impl<A, B> ZipSpec for Zip<A, B>
@@ -233,6 +235,12 @@ where
233235
self.cleanup(len, true);
234236
try { accum }
235237
}
238+
239+
#[inline]
240+
fn spec_size_hint(&self) -> (usize, Option<usize>) {
241+
let size = cmp::min(self.a.size_hint().0, self.b.size_hint().0);
242+
(size, Some(size))
243+
}
236244
}
237245

238246
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)