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
//! Tracks a size value.

use std::ops::{AddAssign, SubAssign};

/// Keeps track of accumulated size in bytes.
///
/// Note: We do not assume that size tracking is always exact. Depending on the bookkeeping of the
/// additions and subtractions the total size might be slightly off. Therefore, the underlying value
/// is an `isize`, so that the value does not wrap.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SizeTracker(isize);

impl SizeTracker {
    /// Reset the size tracker.
    pub fn reset(&mut self) {
        self.0 = 0;
    }
}

impl AddAssign<usize> for SizeTracker {
    fn add_assign(&mut self, rhs: usize) {
        self.0 += rhs as isize
    }
}

impl SubAssign<usize> for SizeTracker {
    fn sub_assign(&mut self, rhs: usize) {
        self.0 -= rhs as isize
    }
}

impl From<SizeTracker> for usize {
    fn from(value: SizeTracker) -> Self {
        value.0 as Self
    }
}