1use crate::lib::*;
2
3pub fn from_bounds<I>(iter: &I) -> Option<usize>
4where
5 I: Iterator,
6{
7 helper(iter.size_hint())
8}
9
10#[cfg(any(feature = "std", feature = "alloc"))]
11pub fn cautious<Element>(hint: Option<usize>) -> usize {
12 const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
13
14 if mem::size_of::<Element>() == 0 {
15 0
16 } else {
17 cmp::min(
18 hint.unwrap_or(0),
19 MAX_PREALLOC_BYTES / mem::size_of::<Element>(),
20 )
21 }
22}
23
24fn helper(bounds: (usize, Option<usize>)) -> Option<usize> {
25 match bounds {
26 (lower, Some(upper)) if lower == upper => Some(upper),
27 _ => None,
28 }
29}
30