Skip to content

Commit 8db2782

Browse files
committed
refactor: moved new out of ZipImpl
1 parent f4eb5d9 commit 8db2782

File tree

1 file changed

+35
-15
lines changed
  • library/core/src/iter/adapters

1 file changed

+35
-15
lines changed

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ pub struct Zip<A, B> {
1919
}
2020
impl<A: Iterator, B: Iterator> Zip<A, B> {
2121
pub(in crate::iter) fn new(a: A, b: B) -> Zip<A, B> {
22-
ZipImpl::new(a, b)
22+
ZipNew::new(a, b)
2323
}
24+
2425
fn super_nth(&mut self, mut n: usize) -> Option<(A::Item, B::Item)> {
2526
while let Some(x) = Iterator::next(self) {
2627
if n == 0 {
@@ -32,6 +33,39 @@ impl<A: Iterator, B: Iterator> Zip<A, B> {
3233
}
3334
}
3435

36+
#[doc(hidden)]
37+
trait ZipNew<A, B> {
38+
fn new(a: A, b: B) -> Self;
39+
}
40+
41+
#[doc(hidden)]
42+
impl<A, B> ZipNew<A, B> for Zip<A, B>
43+
where
44+
A: Iterator,
45+
B: Iterator,
46+
{
47+
default fn new(a: A, b: B) -> Self {
48+
Zip {
49+
a,
50+
b,
51+
index: 0, // unused
52+
len: 0, // unused
53+
}
54+
}
55+
}
56+
57+
#[doc(hidden)]
58+
impl<A, B> ZipNew<A, B> for Zip<A, B>
59+
where
60+
A: TrustedRandomAccess + Iterator,
61+
B: TrustedRandomAccess + Iterator,
62+
{
63+
fn new(a: A, b: B) -> Self {
64+
let len = cmp::min(a.size(), b.size());
65+
Zip { a, b, index: 0, len }
66+
}
67+
}
68+
3569
#[stable(feature = "rust1", since = "1.0.0")]
3670
impl<A, B> Iterator for Zip<A, B>
3771
where
@@ -82,7 +116,6 @@ where
82116
#[doc(hidden)]
83117
trait ZipImpl<A, B> {
84118
type Item;
85-
fn new(a: A, b: B) -> Self;
86119
fn next(&mut self) -> Option<Self::Item>;
87120
fn size_hint(&self) -> (usize, Option<usize>);
88121
fn nth(&mut self, n: usize) -> Option<Self::Item>;
@@ -104,14 +137,6 @@ where
104137
B: Iterator,
105138
{
106139
type Item = (A::Item, B::Item);
107-
default fn new(a: A, b: B) -> Self {
108-
Zip {
109-
a,
110-
b,
111-
index: 0, // unused
112-
len: 0, // unused
113-
}
114-
}
115140

116141
#[inline]
117142
default fn next(&mut self) -> Option<(A::Item, B::Item)> {
@@ -183,11 +208,6 @@ where
183208
A: TrustedRandomAccess + Iterator,
184209
B: TrustedRandomAccess + Iterator,
185210
{
186-
fn new(a: A, b: B) -> Self {
187-
let len = cmp::min(a.size(), b.size());
188-
Zip { a, b, index: 0, len }
189-
}
190-
191211
#[inline]
192212
fn next(&mut self) -> Option<(A::Item, B::Item)> {
193213
if self.index < self.len {

0 commit comments

Comments
 (0)