musli_core/internal/
size_hint.rs

1use crate::de::SizeHint;
2
3// Uses the same heuristic as:
4// https://github.com/serde-rs/serde/blob/d91f8ba950e2faf4db4e283e917ba2ee94a9b8a4/serde/src/de/size_hint.rs#L12
5#[inline]
6pub(crate) fn cautious<T>(hint: impl Into<SizeHint>) -> usize {
7    const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
8
9    if size_of::<T>() == 0 {
10        return 0;
11    }
12
13    hint.into()
14        .or_default()
15        .min(MAX_PREALLOC_BYTES / size_of::<T>())
16}