float_pigment_consistent_bincode/config/
limit.rs1use alloc::boxed::Box;
2
3use crate::error::{ErrorKind, Result};
4
5pub trait SizeLimit {
7 fn add(&mut self, n: u64) -> Result<()>;
10 fn limit(&self) -> Option<u64>;
12}
13
14#[derive(Copy, Clone)]
17pub struct Bounded(pub u64);
18
19#[derive(Copy, Clone)]
22pub struct Infinite;
23
24impl SizeLimit for Bounded {
25 #[inline(always)]
26 fn add(&mut self, n: u64) -> Result<()> {
27 if self.0 >= n {
28 self.0 -= n;
29 Ok(())
30 } else {
31 Err(Box::new(ErrorKind::SizeLimit))
32 }
33 }
34
35 #[inline(always)]
36 fn limit(&self) -> Option<u64> {
37 Some(self.0)
38 }
39}
40
41impl SizeLimit for Infinite {
42 #[inline(always)]
43 fn add(&mut self, _: u64) -> Result<()> {
44 Ok(())
45 }
46
47 #[inline(always)]
48 fn limit(&self) -> Option<u64> {
49 None
50 }
51}