From 6f387769fe4663d679f97b2d7d37933e666b8c09 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Tue, 4 May 2021 17:28:44 +0200 Subject: [PATCH] fix(memory_usage): prevent used swap underflow on windows (#2689) --- src/modules/memory_usage.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/memory_usage.rs b/src/modules/memory_usage.rs index 12185567..327e0ac6 100644 --- a/src/modules/memory_usage.rs +++ b/src/modules/memory_usage.rs @@ -51,7 +51,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { 0 => system.free, _ => system.avail, }; - let used_memory_kib = system.total - avail_memory_kib; + let used_memory_kib = system.total.saturating_sub(avail_memory_kib); let total_memory_kib = system.total; let ram_used = (used_memory_kib as f64 / total_memory_kib as f64) * 100.; let ram_pct = format_pct(ram_used, pct_sign); @@ -63,7 +63,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { let ram = format_usage_total(used_memory_kib, total_memory_kib); let total_swap_kib = system.swap_total; - let used_swap_kib = system.swap_total - system.swap_free; + let used_swap_kib = system.swap_total.saturating_sub(system.swap_free); let percent_swap_used = (used_swap_kib as f64 / total_swap_kib as f64) * 100.; let swap_pct = format_pct(percent_swap_used, pct_sign); let swap = format_usage_total(used_swap_kib, total_swap_kib);