|
50 | 50 | }
|
51 | 51 |
|
52 | 52 | 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 | + } |
54 | 65 | }
|
55 | 66 | }
|
56 | 67 |
|
@@ -105,7 +116,18 @@ where
|
105 | 116 | }
|
106 | 117 |
|
107 | 118 | 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 | + } |
109 | 131 | }
|
110 | 132 | }
|
111 | 133 |
|
@@ -161,6 +183,17 @@ where
|
161 | 183 | }
|
162 | 184 |
|
163 | 185 | 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 | + } |
165 | 198 | }
|
166 | 199 | }
|
0 commit comments