ic_wire/limits.rs
1/// Consumer-selected Serde traversal allowances.
2///
3/// Custom Serde code is trusted: work and allocation outside visited values
4/// cannot be constrained by these allowances. Allocation is conservative
5/// accounting, not a live-heap cap, and depends on target type layouts.
6#[derive(Clone, Copy, Debug)]
7pub struct Limits {
8 /// Maximum encoded/input bytes, including the header when framed.
9 pub bytes: usize,
10 /// Reserve all configured bytes, then eight times visited value sizes and
11 /// byte/text spans. Borrowed spans pay the same charge as owned spans.
12 pub allocation: usize,
13 /// Maximum visits, including seed visits and zero-sized values.
14 pub nodes: usize,
15 /// Maximum visit depth, starting at one.
16 pub depth: usize,
17}
18
19impl Limits {
20 pub(crate) fn without_prefix(self, prefix: usize) -> Result<Self, crate::Error> {
21 Ok(Self {
22 bytes: self
23 .bytes
24 .checked_sub(prefix)
25 .ok_or(crate::Error::Limit(crate::LimitKind::Bytes))?,
26 allocation: self
27 .allocation
28 .checked_sub(prefix)
29 .ok_or(crate::Error::Limit(crate::LimitKind::Allocation))?,
30 ..self
31 })
32 }
33}