Skip to content

Commit 4c09d13

Browse files
committed
add size_hint impl for FlattenOk
1 parent 2fb1784 commit 4c09d13

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/flatten_ok.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ where
4848
}
4949
}
5050
}
51+
52+
fn size_hint(&self) -> (usize, Option<usize>) {
53+
if let Some(inner) = &self.inner {
54+
// If we have an inner iterator, then its lower bound is our lower bound,
55+
// but we still don't know the upper bound.
56+
let (inner_lower, inner_higher) = inner.size_hint();
57+
let (_, outer_higher) = self.iter.size_hint();
58+
if outer_higher == Some(0) {
59+
// If there is nothing remaining in the outer iterator, we know the upper bound.
60+
(inner_lower, inner_higher)
61+
} else {
62+
// However, if the outer iterator could have more items in it, we don't
63+
// know the upper bound.
64+
(inner_lower, None)
65+
}
66+
} else if self.iter.size_hint() == (0, Some(0)) {
67+
// If the outer iterator is empty, we have no items.
68+
(0, Some(0))
69+
} else {
70+
// Otherwise we do not know anything about the number of items.
71+
(0, None)
72+
}
73+
}
5174
}
5275

5376
impl<I, T, E> Clone for FlattenOk<I, T, E>

0 commit comments

Comments
 (0)