Эх сурвалжийг харах

Fix divide by zero in netspeed

Thomas Dy 9 жил өмнө
parent
commit
683023814c

+ 7 - 6
src/sensors/netspeed.rs

@@ -47,13 +47,14 @@ impl Sensor for NetSpeedSensor {
                 tx: acc.tx + elem.tx
             });
 
-        let output = match self.stats.as_ref() {
-            Some(pstats) => {
-                let rx = (stats.rx - pstats.rx) / (curr_time - self.last_time);
-                let tx = (stats.tx - pstats.tx) / (curr_time - self.last_time);
+        let diff_time = curr_time - self.last_time;
+        let output = match (self.stats.as_ref(), diff_time) {
+            (_, 0) | (None, _) => "?".to_string(),
+            (Some(pstats), diff_time) => {
+                let rx = (stats.rx - pstats.rx) / diff_time;
+                let tx = (stats.tx - pstats.tx) / diff_time;
                 format!("{}↓ {}↑", format_bytes(rx), format_bytes(tx))
-            },
-            None => "?".to_string()
+            }
         };
 
         self.last_time = curr_time;