valence_protocol/impls.rs
1//! Implementations of [`Encode`](crate::Encode) and [`Decode`](crate::Decode)
2//! on foreign types.
3
4mod map;
5mod math;
6mod other;
7mod pointer;
8mod primitive;
9mod sequence;
10mod string;
11mod tuple;
12
13use std::mem;
14
15/// Prevents preallocating too much memory in case we get a malicious or invalid
16/// sequence length.
17fn cautious_capacity<Element>(size_hint: usize) -> usize {
18 const MAX_PREALLOC_BYTES: usize = 1024 * 1024;
19
20 if mem::size_of::<Element>() == 0 {
21 0
22 } else {
23 size_hint.min(MAX_PREALLOC_BYTES / mem::size_of::<Element>())
24 }
25}