mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-27 04:07:55 +00:00
5e102c4da1
Let's keep crate::__private for only things that *need* to be accessible to the macro-generated code. Size_hint can be pub(crate).
30 lines
644 B
Rust
30 lines
644 B
Rust
use crate::lib::*;
|
|
|
|
pub fn from_bounds<I>(iter: &I) -> Option<usize>
|
|
where
|
|
I: Iterator,
|
|
{
|
|
helper(iter.size_hint())
|
|
}
|
|
|
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
pub fn cautious<Element>(hint: Option<usize>) -> usize {
|
|
const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
|
|
|
|
if mem::size_of::<Element>() == 0 {
|
|
0
|
|
} else {
|
|
cmp::min(
|
|
hint.unwrap_or(0),
|
|
MAX_PREALLOC_BYTES / mem::size_of::<Element>(),
|
|
)
|
|
}
|
|
}
|
|
|
|
fn helper(bounds: (usize, Option<usize>)) -> Option<usize> {
|
|
match bounds {
|
|
(lower, Some(upper)) if lower == upper => Some(upper),
|
|
_ => None,
|
|
}
|
|
}
|