Skip to content

Commit d0f9feb

Browse files
committed
This should work
1 parent 2e1beb1 commit d0f9feb

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,18 @@ where
5050
}
5151

5252
fn size_hint(&self) -> (usize, Option<usize>) {
53-
if self.last.is_some() { (1, self.inner.size_hint().1) } else { (0, Some(0)) }
53+
if self.last.is_some() {
54+
// If we have a last item stored, the iterator can yield at most
55+
// as many items at the inner iterator plus the stored one. Yet we
56+
// can only guarantee that the iterator yields at least one more item
57+
// since all other items in the inner iterator might be duplicates.
58+
let (_, max) = self.inner.size_hint();
59+
(1, max.and_then(|k| k.checked_add(1)))
60+
} else {
61+
// If the last item we got from the inner iterator is `None`,
62+
// the iterator is empty.
63+
(0, Some(0))
64+
}
5465
}
5566
}
5667

@@ -105,7 +116,18 @@ where
105116
}
106117

107118
fn size_hint(&self) -> (usize, Option<usize>) {
108-
if self.last.is_some() { (1, self.inner.size_hint().1) } else { (0, Some(0)) }
119+
if self.last.is_some() {
120+
// If we have a last item stored, the iterator can yield at most
121+
// as many items at the inner iterator plus the stored one. Yet we
122+
// can only guarantee that the iterator yields at least one more item
123+
// since all other items in the inner iterator might be duplicates.
124+
let (_, max) = self.inner.size_hint();
125+
(1, max.and_then(|k| k.checked_add(1)))
126+
} else {
127+
// If the last item we got from the inner iterator is `None`,
128+
// the iterator is empty.
129+
(0, Some(0))
130+
}
109131
}
110132
}
111133

@@ -161,6 +183,17 @@ where
161183
}
162184

163185
fn size_hint(&self) -> (usize, Option<usize>) {
164-
if self.last.is_some() { (1, self.inner.size_hint().1) } else { (0, Some(0)) }
186+
if self.last.is_some() {
187+
// If we have a last item stored, the iterator can yield at most
188+
// as many items at the inner iterator plus the stored one. Yet we
189+
// can only guarantee that the iterator yields at least one more item
190+
// since all other items in the inner iterator might be duplicates.
191+
let (_, max) = self.inner.size_hint();
192+
(1, max.and_then(|k| k.checked_add(1)))
193+
} else {
194+
// If the last item we got from the inner iterator is `None`,
195+
// the iterator is empty.
196+
(0, Some(0))
197+
}
165198
}
166199
}

0 commit comments

Comments
 (0)