1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//! Implementations of [`Encode`](crate::Encode) and [`Decode`](crate::Decode)
//! on foreign types.

mod map;
mod math;
mod other;
mod pointer;
mod primitive;
mod sequence;
mod string;
mod tuple;

use std::mem;

/// Prevents preallocating too much memory in case we get a malicious or invalid
/// sequence length.
fn cautious_capacity<Element>(size_hint: usize) -> usize {
    const MAX_PREALLOC_BYTES: usize = 1024 * 1024;

    if mem::size_of::<Element>() == 0 {
        0
    } else {
        size_hint.min(MAX_PREALLOC_BYTES / mem::size_of::<Element>())
    }
}