refactor: use add impl for adding memory values in the neum (#2148)

This commit is contained in:
Clement Tsang
2026-07-14 00:08:05 -04:00
committed by GitHub
parent 9c79622422
commit ddbb8aae99
+19 -17
View File
@@ -3,6 +3,7 @@ use std::{
cmp::{Ordering, max},
fmt::Display,
num::NonZeroU16,
ops::Add,
sync::Arc,
time::Duration,
};
@@ -88,12 +89,27 @@ impl Display for Id {
}
}
#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Clone, Debug, Copy)]
pub enum MemUsage {
Percent(f32),
Bytes(u64),
}
impl Add for MemUsage {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(MemUsage::Percent(a), MemUsage::Percent(b)) => MemUsage::Percent(a + b),
(MemUsage::Bytes(a), MemUsage::Bytes(b)) => MemUsage::Bytes(a + b),
(MemUsage::Bytes(_), MemUsage::Percent(_))
| (MemUsage::Percent(_), MemUsage::Bytes(_)) => {
unreachable!("trying to add together two different memory usage types!")
}
}
}
}
impl PartialOrd for MemUsage {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
@@ -287,14 +303,7 @@ impl ProcWidgetData {
pub fn add(&mut self, other: &Self) {
self.cpu_usage_percent += other.cpu_usage_percent;
self.mem_usage = match (&self.mem_usage, &other.mem_usage) {
(MemUsage::Percent(a), MemUsage::Percent(b)) => MemUsage::Percent(a + b),
(MemUsage::Bytes(a), MemUsage::Bytes(b)) => MemUsage::Bytes(a + b),
(MemUsage::Percent(_), MemUsage::Bytes(_))
| (MemUsage::Bytes(_), MemUsage::Percent(_)) => {
unreachable!("trying to add together two different memory usage types!")
}
};
self.mem_usage = self.mem_usage + other.mem_usage;
self.rps += other.rps;
self.wps += other.wps;
self.total_read += other.total_read;
@@ -302,14 +311,7 @@ impl ProcWidgetData {
self.time = self.time.max(other.time);
#[cfg(feature = "gpu")]
{
self.gpu_mem_usage = match (&self.gpu_mem_usage, &other.gpu_mem_usage) {
(MemUsage::Percent(a), MemUsage::Percent(b)) => MemUsage::Percent(a + b),
(MemUsage::Bytes(a), MemUsage::Bytes(b)) => MemUsage::Bytes(a + b),
(MemUsage::Percent(_), MemUsage::Bytes(_))
| (MemUsage::Bytes(_), MemUsage::Percent(_)) => {
unreachable!("trying to add together two different memory usage types!")
}
};
self.gpu_mem_usage = self.gpu_mem_usage + other.gpu_mem_usage;
self.gpu_usage += other.gpu_usage;
}
}