Coverage Report

Created: 2026-11-12 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bincode-1.3.3/src/config/limit.rs
Line
Count
Source
1
use error::{ErrorKind, Result};
2
3
/// A trait for stopping serialization and deserialization when a certain limit has been reached.
4
pub trait SizeLimit {
5
    /// Tells the SizeLimit that a certain number of bytes has been
6
    /// read or written.  Returns Err if the limit has been exceeded.
7
    fn add(&mut self, n: u64) -> Result<()>;
8
    /// Returns the hard limit (if one exists)
9
    fn limit(&self) -> Option<u64>;
10
}
11
12
/// A SizeLimit that restricts serialized or deserialized messages from
13
/// exceeding a certain byte length.
14
#[derive(Copy, Clone)]
15
pub struct Bounded(pub u64);
16
17
/// A SizeLimit without a limit!
18
/// Use this if you don't care about the size of encoded or decoded messages.
19
#[derive(Copy, Clone)]
20
pub struct Infinite;
21
22
impl SizeLimit for Bounded {
23
    #[inline(always)]
24
168k
    fn add(&mut self, n: u64) -> Result<()> {
25
168k
        if self.0 >= n {
26
168k
            self.0 -= n;
27
168k
            Ok(())
28
        } else {
29
166
            Err(Box::new(ErrorKind::SizeLimit))
30
        }
31
168k
    }
32
33
    #[inline(always)]
34
0
    fn limit(&self) -> Option<u64> {
35
0
        Some(self.0)
36
0
    }
37
}
38
39
impl SizeLimit for Infinite {
40
    #[inline(always)]
41
0
    fn add(&mut self, _: u64) -> Result<()> {
42
0
        Ok(())
43
0
    }
44
45
    #[inline(always)]
46
0
    fn limit(&self) -> Option<u64> {
47
0
        None
48
0
    }
49
}