reth_transaction_pool/pool/size.rs
1//! Tracks a size value.
2
3use std::ops::{AddAssign, SubAssign};
4
5/// Keeps track of accumulated size in bytes.
6///
7/// Note: We do not assume that size tracking is always exact. Depending on the bookkeeping of the
8/// additions and subtractions the total size might be slightly off. Therefore, the underlying value
9/// is an `isize`, so that the value does not wrap.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
11pub struct SizeTracker(isize);
12
13impl SizeTracker {
14 /// Reset the size tracker.
15 pub fn reset(&mut self) {
16 self.0 = 0;
17 }
18}
19
20impl AddAssign<usize> for SizeTracker {
21 fn add_assign(&mut self, rhs: usize) {
22 self.0 += rhs as isize
23 }
24}
25
26impl SubAssign<usize> for SizeTracker {
27 fn sub_assign(&mut self, rhs: usize) {
28 self.0 -= rhs as isize
29 }
30}
31
32impl From<SizeTracker> for usize {
33 fn from(value: SizeTracker) -> Self {
34 value.0 as Self
35 }
36}