From 9c94122d1d95100337a5d8593d52299d02e5e793 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sun, 12 Dec 2021 10:00:09 -0800 Subject: [PATCH] Address many clippy lints. --- src/bin/home.rs | 2 +- src/bin/work.rs | 2 +- src/widgets/cpu.rs | 34 +++++++++++++++++----------------- src/widgets/network.rs | 27 ++++++++++++--------------- src/widgets/power.rs | 18 +++++++++--------- 5 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/bin/home.rs b/src/bin/home.rs index 3cf1fc8..82bb14b 100644 --- a/src/bin/home.rs +++ b/src/bin/home.rs @@ -22,7 +22,7 @@ fn main() { let opts = Opt::from_args(); bar.push(PowerSupply::default()); - bar.push(CpuWidget::new()); + bar.push(CpuWidget::default()); // Realtime upload/download rate for a interface bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); diff --git a/src/bin/work.rs b/src/bin/work.rs index 7ac4f3e..3725eb0 100644 --- a/src/bin/work.rs +++ b/src/bin/work.rs @@ -22,7 +22,7 @@ fn main() { let opts = Opt::from_args(); bar.push(PowerSupply::default()); - bar.push(CpuWidget::new()); + bar.push(CpuWidget::default()); // Realtime upload/download rate for a interface bar.push(NetworkSpeedWidget::new(&opts.nic, 6)); diff --git a/src/widgets/cpu.rs b/src/widgets/cpu.rs index 6b6ec82..43bae81 100644 --- a/src/widgets/cpu.rs +++ b/src/widgets/cpu.rs @@ -42,13 +42,13 @@ impl CpuWidget { let tokens: Vec<_> = line.trim().split(|c| c == ' ' || c == '\t').collect(); // The sum total CPU entry has a space instead of a number, so we skip the line when // there's an extra empty column. - if tokens[1] == "" { + if tokens[1].is_empty() { continue; } if tokens[0].starts_with("cpu") { let parsed = tokens[1..5] .iter() - .map(|x| u64::from_str_radix(x, 10).unwrap()) + .map(|x| x.parse::().unwrap()) .collect::>(); // [0] user // [1] nice @@ -62,20 +62,6 @@ impl CpuWidget { Ok(stats) } - pub fn new() -> Self { - let last_stats = Self::read_status().unwrap(); - let num_samples = 1; - let history = (0..last_stats.len()) - .map(|_| (0..num_samples).map(|_| 0.).collect::>()) - .collect::>>(); - let ret = Self { - last_stats, - history, - }; - - return ret; - } - fn draw_history(&mut self) -> Option { let stats = Self::read_status().ok()?; let percentages: Vec<_> = stats @@ -101,7 +87,7 @@ impl CpuWidget { .history .iter() .map(|history| { - let sg = g.render(&history.iter().map(|v| *v).collect::>()); + let sg = g.render(&history.iter().copied().collect::>()); history .iter() .zip(sg.chars()) @@ -121,6 +107,20 @@ impl CpuWidget { } } +impl Default for CpuWidget { + fn default() -> Self { + let last_stats = Self::read_status().unwrap(); + let num_samples = 1; + let history = (0..last_stats.len()) + .map(|_| (0..num_samples).map(|_| 0.).collect::>()) + .collect::>>(); + Self { + last_stats, + history, + } + } +} + impl Widget for CpuWidget { fn update(&mut self) -> Option { if let Some(history) = self.draw_history() { diff --git a/src/widgets/network.rs b/src/widgets/network.rs index a7e62a1..7f181cf 100644 --- a/src/widgets/network.rs +++ b/src/widgets/network.rs @@ -9,8 +9,8 @@ use std::time::SystemTime; use crate::spark; -const NETWORK_PATH_PREFIX: &'static str = "/sys/class/net"; -const NETWORK_STAT_SUFFIX: &'static str = "statistics/dummy"; +const NETWORK_PATH_PREFIX: &str = "/sys/class/net"; +const NETWORK_STAT_SUFFIX: &str = "statistics/dummy"; struct TransferStat { rx: u64, @@ -35,20 +35,19 @@ impl TransferStat { return line; } - return Err(Error::new(ErrorKind::Other, "Empty file")); + Err(Error::new(ErrorKind::Other, "Empty file")) }; - let rx = u64::from_str_radix(&(read_stat_file("rx_bytes")?), 10).unwrap(); - let tx = u64::from_str_radix(&(read_stat_file("tx_bytes")?), 10).unwrap(); + let rx = (read_stat_file("rx_bytes")?).parse().unwrap(); + let tx = (read_stat_file("tx_bytes")?).parse().unwrap(); let ts = SystemTime::now(); - return Ok(Self { rx, tx, ts }); + Ok(Self { rx, tx, ts }) } fn duration(&self, earlier: &Self) -> f64 { let duration = self.ts.duration_since(earlier.ts).unwrap(); - let secs = duration.as_secs() as f64 + duration.subsec_nanos() as f64 / 1_000_000_000.0; - return secs; + duration.as_secs() as f64 + duration.subsec_nanos() as f64 / 1_000_000_000.0 } fn rx_rate(&self, earlier: &Self) -> f64 { @@ -57,7 +56,7 @@ impl TransferStat { return std::f64::NAN; } - return (self.rx - earlier.rx) as f64 / duration; + (self.rx - earlier.rx) as f64 / duration } fn tx_rate(&self, earlier: &Self) -> f64 { @@ -66,7 +65,7 @@ impl TransferStat { return std::f64::NAN; } - return (self.tx - earlier.tx) as f64 / duration; + (self.tx - earlier.tx) as f64 / duration } } @@ -100,7 +99,7 @@ impl NetworkSpeedWidget { return "N/A".to_string(); } - const UNIT_NAME: [&'static str; 6] = [" B/s", "KB/s", "MB/s", "GB/s", "TB/s", "PB/s"]; + const UNIT_NAME: [&str; 6] = [" B/s", "KB/s", "MB/s", "GB/s", "TB/s", "PB/s"]; let mut best_unit = UNIT_NAME[0]; let mut best_multiplier = 1.0; @@ -113,9 +112,7 @@ impl NetworkSpeedWidget { best_multiplier *= 1024.0 } - let ret = format!("{:6.1}{}", rate / best_multiplier, best_unit); - - return ret; + format!("{:6.1}{}", rate / best_multiplier, best_unit) } fn get_human_readable_stat(&mut self) -> Result<(String, String)> { @@ -130,7 +127,7 @@ impl NetworkSpeedWidget { self.tx_history.pop_front(); self.last_stat = cur_stat; - return Ok((Self::format_rate(rx_rate), Self::format_rate(tx_rate))); + Ok((Self::format_rate(rx_rate), Self::format_rate(tx_rate))) } } diff --git a/src/widgets/power.rs b/src/widgets/power.rs index a1b55f0..cda7cdb 100644 --- a/src/widgets/power.rs +++ b/src/widgets/power.rs @@ -19,7 +19,7 @@ use thiserror::Error; #[derive(Debug, Default)] pub struct PowerSupply {} -const POWER_SUPPLY_GLOB: &'static str = "/sys/class/power_supply/*/uevent"; +const POWER_SUPPLY_GLOB: &str = "/sys/class/power_supply/*/uevent"; #[derive(Error, Debug)] enum PowerSupplyError { @@ -39,11 +39,11 @@ enum PowerSupplyError { impl PowerSupply { fn get_update(&mut self) -> Result, PowerSupplyError> { - const BATT_100: &'static str = ""; - const BATT_75: &'static str = ""; - const BATT_50: &'static str = ""; - const BATT_25: &'static str = ""; - const BATT_0: &'static str = ""; + const BATT_100: &str = ""; + const BATT_75: &str = ""; + const BATT_50: &str = ""; + const BATT_25: &str = ""; + const BATT_0: &str = ""; let mut res = Vec::new(); for ps in glob(POWER_SUPPLY_GLOB)? { @@ -51,9 +51,9 @@ impl PowerSupply { let values: HashMap<&str, &str> = std::str::from_utf8(&bytes)? .lines() .map(|l| { - l.split_once('=').ok_or(PowerSupplyError::ParseError( - "missing = in uevent line".to_string(), - )) + l.split_once('=').ok_or_else(|| { + PowerSupplyError::ParseError("missing = in uevent line".to_string()) + }) }) .collect::>()?; let cap: u32 = values