diff --git a/src/widgets/process_table/process_data.rs b/src/widgets/process_table/process_data.rs index b251d7b6..f24950f4 100644 --- a/src/widgets/process_table/process_data.rs +++ b/src/widgets/process_table/process_data.rs @@ -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 { 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; } }