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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use crate::varint::varint_max;
use core::{
    marker::PhantomData,
    num::{
        NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
        NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
    },
};

/// This trait is used to enforce the maximum size required to
/// store the serialization of a given type.
pub trait MaxSize {
    /// The maximum possible size that the serialization of this
    /// type can have, in bytes.
    const POSTCARD_MAX_SIZE: usize;
}

impl MaxSize for bool {
    const POSTCARD_MAX_SIZE: usize = 1;
}

impl MaxSize for i8 {
    const POSTCARD_MAX_SIZE: usize = 1;
}

impl MaxSize for i16 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for i32 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for i64 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for i128 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for isize {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for u8 {
    const POSTCARD_MAX_SIZE: usize = 1;
}

impl MaxSize for u16 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for u32 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for u64 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for u128 {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for usize {
    const POSTCARD_MAX_SIZE: usize = varint_max::<Self>();
}

impl MaxSize for f32 {
    const POSTCARD_MAX_SIZE: usize = 4;
}

impl MaxSize for f64 {
    const POSTCARD_MAX_SIZE: usize = 8;
}

impl MaxSize for char {
    const POSTCARD_MAX_SIZE: usize = 5;
}

impl<T: MaxSize> MaxSize for Option<T> {
    const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE + 1;
}

impl<T: MaxSize, E: MaxSize> MaxSize for Result<T, E> {
    const POSTCARD_MAX_SIZE: usize = max(T::POSTCARD_MAX_SIZE, E::POSTCARD_MAX_SIZE) + 1;
}

impl MaxSize for () {
    const POSTCARD_MAX_SIZE: usize = 0;
}

impl<T: MaxSize, const N: usize> MaxSize for [T; N] {
    const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE * N;
}

impl<T: MaxSize> MaxSize for &'_ T {
    const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
}

impl<T: MaxSize> MaxSize for &'_ mut T {
    const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroI8 {
    const POSTCARD_MAX_SIZE: usize = i8::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroI16 {
    const POSTCARD_MAX_SIZE: usize = i16::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroI32 {
    const POSTCARD_MAX_SIZE: usize = i32::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroI64 {
    const POSTCARD_MAX_SIZE: usize = i64::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroI128 {
    const POSTCARD_MAX_SIZE: usize = i128::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroIsize {
    const POSTCARD_MAX_SIZE: usize = isize::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroU8 {
    const POSTCARD_MAX_SIZE: usize = u8::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroU16 {
    const POSTCARD_MAX_SIZE: usize = u16::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroU32 {
    const POSTCARD_MAX_SIZE: usize = u32::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroU64 {
    const POSTCARD_MAX_SIZE: usize = u64::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroU128 {
    const POSTCARD_MAX_SIZE: usize = u128::POSTCARD_MAX_SIZE;
}

impl MaxSize for NonZeroUsize {
    const POSTCARD_MAX_SIZE: usize = usize::POSTCARD_MAX_SIZE;
}

impl<T> MaxSize for PhantomData<T> {
    const POSTCARD_MAX_SIZE: usize = 0;
}

impl<A: MaxSize> MaxSize for (A,) {
    const POSTCARD_MAX_SIZE: usize = A::POSTCARD_MAX_SIZE;
}

impl<A: MaxSize, B: MaxSize> MaxSize for (A, B) {
    const POSTCARD_MAX_SIZE: usize = A::POSTCARD_MAX_SIZE + B::POSTCARD_MAX_SIZE;
}

impl<A: MaxSize, B: MaxSize, C: MaxSize> MaxSize for (A, B, C) {
    const POSTCARD_MAX_SIZE: usize =
        A::POSTCARD_MAX_SIZE + B::POSTCARD_MAX_SIZE + C::POSTCARD_MAX_SIZE;
}

impl<A: MaxSize, B: MaxSize, C: MaxSize, D: MaxSize> MaxSize for (A, B, C, D) {
    const POSTCARD_MAX_SIZE: usize =
        A::POSTCARD_MAX_SIZE + B::POSTCARD_MAX_SIZE + C::POSTCARD_MAX_SIZE + D::POSTCARD_MAX_SIZE;
}

impl<A: MaxSize, B: MaxSize, C: MaxSize, D: MaxSize, E: MaxSize> MaxSize for (A, B, C, D, E) {
    const POSTCARD_MAX_SIZE: usize = A::POSTCARD_MAX_SIZE
        + B::POSTCARD_MAX_SIZE
        + C::POSTCARD_MAX_SIZE
        + D::POSTCARD_MAX_SIZE
        + E::POSTCARD_MAX_SIZE;
}

impl<A: MaxSize, B: MaxSize, C: MaxSize, D: MaxSize, E: MaxSize, F: MaxSize> MaxSize
    for (A, B, C, D, E, F)
{
    const POSTCARD_MAX_SIZE: usize = A::POSTCARD_MAX_SIZE
        + B::POSTCARD_MAX_SIZE
        + C::POSTCARD_MAX_SIZE
        + D::POSTCARD_MAX_SIZE
        + E::POSTCARD_MAX_SIZE
        + F::POSTCARD_MAX_SIZE;
}

#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
impl<T: MaxSize, const N: usize> MaxSize for heapless::Vec<T, N> {
    const POSTCARD_MAX_SIZE: usize = <[T; N]>::POSTCARD_MAX_SIZE + varint_size(N);
}

#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
impl<const N: usize> MaxSize for heapless::String<N> {
    const POSTCARD_MAX_SIZE: usize = <[u8; N]>::POSTCARD_MAX_SIZE + varint_size(N);
}

const fn varint_size(max_n: usize) -> usize {
    const BITS_PER_BYTE: usize = 8;
    const BITS_PER_VARINT_BYTE: usize = 7;

    if max_n == 0 {
        return 1;
    }

    // How many data bits do we need for `max_n`.
    let bits = core::mem::size_of::<usize>() * BITS_PER_BYTE - max_n.leading_zeros() as usize;

    // We add (BITS_PER_BYTE - 1), to ensure any integer divisions
    // with a remainder will always add exactly one full byte, but
    // an evenly divided number of bits will be the same
    let roundup_bits = bits + (BITS_PER_VARINT_BYTE - 1);

    // Apply division, using normal "round down" integer division
    roundup_bits / BITS_PER_VARINT_BYTE
}

const fn max(lhs: usize, rhs: usize) -> usize {
    if lhs > rhs {
        lhs
    } else {
        rhs
    }
}