From 5ad522be43f40888ca45ed732799d3d8f74c7a18 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 12 Feb 2020 20:09:36 -0500 Subject: [PATCH] Removed unnecessary clone. --- src/app/data_harvester.rs | 46 ++++++++++++++++++++------------------- src/main.rs | 5 +++-- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/app/data_harvester.rs b/src/app/data_harvester.rs index 4c6fd938..7312e3dc 100644 --- a/src/app/data_harvester.rs +++ b/src/app/data_harvester.rs @@ -1,6 +1,5 @@ //! This is the main file to house data collection functions. -use crate::utils::error::Result; use std::{collections::HashMap, time::Instant}; use sysinfo::{System, SystemExt}; @@ -11,12 +10,6 @@ pub mod network; pub mod processes; pub mod temperature; -fn set_if_valid(result: &Result, value_to_set: &mut T) { - if let Ok(result) = result { - *value_to_set = (*result).clone(); - } -} - #[derive(Clone, Debug)] pub struct Data { pub cpu: cpu::CPUHarvest, @@ -69,6 +62,9 @@ pub struct DataState { mem_total_kb: u64, temperature_type: temperature::TemperatureType, use_current_cpu_total: bool, + last_collection_time: Instant, + total_rx: u64, + total_tx: u64, } impl Default for DataState { @@ -82,6 +78,9 @@ impl Default for DataState { mem_total_kb: 0, temperature_type: temperature::TemperatureType::Celsius, use_current_cpu_total: false, + last_collection_time: Instant::now(), + total_rx: 0, + total_tx: 0, } } } @@ -120,13 +119,16 @@ impl DataState { // Network self.data.network = network::get_network_data( &self.sys, - self.data.last_collection_time, - &mut self.data.network.total_rx, - &mut self.data.network.total_tx, + self.last_collection_time, + &mut self.total_rx, + &mut self.total_tx, current_instant, ) .await; + self.total_rx = self.data.network.total_rx; + self.total_tx = self.data.network.total_tx; + // Mem and swap if let Ok(memory) = mem::get_mem_data_list().await { self.data.memory = memory; @@ -154,20 +156,20 @@ impl DataState { } // What we want to do: For timed data, if there is an error, just do not add. For other data, just don't update! - set_if_valid( - &processes::get_sorted_processes_list( - &self.sys, - &mut self.prev_idle, - &mut self.prev_non_idle, - &mut self.prev_pid_stats, - self.use_current_cpu_total, - self.mem_total_kb, - current_instant, - ), - &mut self.data.list_of_processes, - ); + if let Ok(process_list) = processes::get_sorted_processes_list( + &self.sys, + &mut self.prev_idle, + &mut self.prev_non_idle, + &mut self.prev_pid_stats, + self.use_current_cpu_total, + self.mem_total_kb, + current_instant, + ) { + self.data.list_of_processes = process_list; + } // Update time self.data.last_collection_time = current_instant; + self.last_collection_time = current_instant; } } diff --git a/src/main.rs b/src/main.rs index 45815701..1ff556c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ use utils::error::{self, BottomError}; enum Event { KeyInput(I), MouseInput(J), - Update(Box), + Update(data_harvester::Data), Clean, } @@ -806,7 +806,8 @@ fn create_event_thread( } } futures::executor::block_on(data_state.update_data()); - let event = Event::Update(Box::from(data_state.data.clone())); + let event = Event::Update(data_state.data); + data_state.data = data_harvester::Data::default(); tx.send(event).unwrap(); thread::sleep(Duration::from_millis(update_rate_in_milliseconds)); }