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