diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2022-03-27 22:01:06 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-27 22:01:06 -0400 |
commit | d297ee4639f65e9bb1c0c636ebe9faad075822c1 (patch) | |
tree | 2b23199b40b3c26803bfea0732649d928c345c63 /src | |
parent | 747497cc8a42b885673c90ca9e5c7fde3ddb5c64 (diff) |
refactor: remove some simple as-casts (#697)
Remove some simple as casts that are easy to change to .into(), or easy to check.
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 9 | ||||
-rw-r--r-- | src/app/data_farmer.rs | 2 | ||||
-rw-r--r-- | src/canvas/dialogs/dd_dialog.rs | 10 | ||||
-rw-r--r-- | src/canvas/drawing_utils.rs | 2 | ||||
-rw-r--r-- | src/canvas/widgets/battery_display.rs | 2 | ||||
-rw-r--r-- | src/canvas/widgets/cpu_basic.rs | 2 | ||||
-rw-r--r-- | src/canvas/widgets/cpu_graph.rs | 2 | ||||
-rw-r--r-- | src/canvas/widgets/disk_table.rs | 18 | ||||
-rw-r--r-- | src/canvas/widgets/mem_graph.rs | 2 | ||||
-rw-r--r-- | src/canvas/widgets/network_graph.rs | 4 | ||||
-rw-r--r-- | src/canvas/widgets/process_table.rs | 20 | ||||
-rw-r--r-- | src/canvas/widgets/temp_table.rs | 18 | ||||
-rw-r--r-- | src/options.rs | 64 |
13 files changed, 82 insertions, 73 deletions
@@ -1,7 +1,6 @@ use std::{ cmp::{max, min}, collections::HashMap, - // io::Write, path::PathBuf, time::Instant, }; @@ -1383,7 +1382,7 @@ impl App { if current_key_press_inst .duration_since(self.last_key_press) .as_millis() - > constants::MAX_KEY_TIMEOUT_IN_MILLISECONDS as u128 + > constants::MAX_KEY_TIMEOUT_IN_MILLISECONDS.into() { self.reset_multi_tap_keys(); } @@ -1450,10 +1449,10 @@ impl App { '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => { let potential_index = caught_char.to_digit(10); if let Some(potential_index) = potential_index { - if (potential_index as usize) < self.help_dialog_state.index_shortcuts.len() - { + let potential_index = potential_index as usize; + if (potential_index) < self.help_dialog_state.index_shortcuts.len() { self.help_scroll_to_or_max( - self.help_dialog_state.index_shortcuts[potential_index as usize], + self.help_dialog_state.index_shortcuts[potential_index], ); } } diff --git a/src/app/data_farmer.rs b/src/app/data_farmer.rs index 578941ef..1e320caa 100644 --- a/src/app/data_farmer.rs +++ b/src/app/data_farmer.rs @@ -121,7 +121,7 @@ impl DataCollection { current_time .duration_since(*instant) .as_millis() - .cmp(&(max_time_millis as u128)) + .cmp(&(max_time_millis.into())) .reverse() }) { Ok(index) => index, diff --git a/src/canvas/dialogs/dd_dialog.rs b/src/canvas/dialogs/dd_dialog.rs index ac9c6910..b6bda9de 100644 --- a/src/canvas/dialogs/dd_dialog.rs +++ b/src/canvas/dialogs/dd_dialog.rs @@ -270,7 +270,7 @@ impl KillDialog for Painter { let layout = Layout::default() .direction(Direction::Vertical) - .constraints(vec![Constraint::Min(1); button_rect.height as usize]) + .constraints(vec![Constraint::Min(1); button_rect.height.into()]) .split(button_rect); let prev_offset: usize = app_state.delete_dialog_state.scroll_pos; @@ -278,15 +278,15 @@ impl KillDialog for Painter { 0 } else if selected < prev_offset + 1 { selected - 1 - } else if selected > prev_offset + (layout.len() as usize) - 1 { - selected - (layout.len() as usize) + 1 + } else if selected > prev_offset + layout.len() - 1 { + selected - layout.len() + 1 } else { prev_offset }; let scroll_offset: usize = app_state.delete_dialog_state.scroll_pos; - let mut buttons = signal_text[scroll_offset + 1 - ..min((layout.len() as usize) + scroll_offset, signal_text.len())] + let mut buttons = signal_text + [scroll_offset + 1..min((layout.len()) + scroll_offset, signal_text.len())] .iter() .map(|text| Span::raw(*text)) .collect::<Vec<Span<'_>>>(); diff --git a/src/canvas/drawing_utils.rs b/src/canvas/drawing_utils.rs index 129a80a9..7cdb3eb5 100644 --- a/src/canvas/drawing_utils.rs +++ b/src/canvas/drawing_utils.rs @@ -98,7 +98,7 @@ pub fn get_column_widths( let amount_per_slot = total_width_left / column_widths.len() as u16; total_width_left %= column_widths.len() as u16; for (index, width) in column_widths.iter_mut().enumerate() { - if (index as u16) < total_width_left { + if index < total_width_left.into() { *width += amount_per_slot + 1; } else { *width += amount_per_slot; diff --git a/src/canvas/widgets/battery_display.rs b/src/canvas/widgets/battery_display.rs index 0ed06432..af5d2963 100644 --- a/src/canvas/widgets/battery_display.rs +++ b/src/canvas/widgets/battery_display.rs @@ -165,7 +165,7 @@ impl BatteryDisplayWidget for Painter { margined_draw_loc, ); } else { - let mut contents = vec![Spans::default(); table_gap as usize]; + let mut contents = vec![Spans::default(); table_gap.into()]; contents.push(Spans::from(Span::styled( "No data found for this battery", diff --git a/src/canvas/widgets/cpu_basic.rs b/src/canvas/widgets/cpu_basic.rs index 2c0da311..089502ec 100644 --- a/src/canvas/widgets/cpu_basic.rs +++ b/src/canvas/widgets/cpu_basic.rs @@ -70,7 +70,7 @@ impl CpuBasicWidget for Painter { const COMBINED_SPACING: usize = CPU_NAME_SPACE + BAR_BOUND_SPACE + PERCENTAGE_SPACE + MARGIN_SPACE; const REDUCED_SPACING: usize = CPU_NAME_SPACE + PERCENTAGE_SPACE + MARGIN_SPACE; - let chunk_width = chunks[0].width as usize; + let chunk_width: usize = chunks[0].width.into(); // Inspired by htop. // We do +4 as if it's too few bars in the bar length, it's kinda pointless. diff --git a/src/canvas/widgets/cpu_graph.rs b/src/canvas/widgets/cpu_graph.rs index ff6838f2..25362a3f 100644 --- a/src/canvas/widgets/cpu_graph.rs +++ b/src/canvas/widgets/cpu_graph.rs @@ -159,7 +159,7 @@ impl CpuGraphWidget for Painter { Axis::default().bounds([time_start, 0.0]) } else if let Some(time) = cpu_widget_state.autohide_timer { if std::time::Instant::now().duration_since(time).as_millis() - < AUTOHIDE_TIMEOUT_MILLISECONDS as u128 + < AUTOHIDE_TIMEOUT_MILLISECONDS.into() { Axis::default() .bounds([time_start, 0.0]) diff --git a/src/canvas/widgets/disk_table.rs b/src/canvas/widgets/disk_table.rs index 9833213d..0928abb5 100644 --- a/src/canvas/widgets/disk_table.rs +++ b/src/canvas/widgets/disk_table.rs @@ -128,17 +128,19 @@ impl DiskTableWidget for Painter { if *desired_col_width > *calculated_col_width && *calculated_col_width > 0 { + let calculated_col_width: usize = + (*calculated_col_width).into(); + let graphemes = UnicodeSegmentation::graphemes(entry.as_str(), true) .collect::<Vec<&str>>(); - if graphemes.len() > *calculated_col_width as usize - && *calculated_col_width > 1 + if graphemes.len() > calculated_col_width + && calculated_col_width > 1 { // Truncate with ellipsis - let first_n = graphemes - [..(*calculated_col_width as usize - 1)] - .concat(); + let first_n = + graphemes[..(calculated_col_width - 1)].concat(); return Text::raw(format!("{}…", first_n)); } } @@ -171,7 +173,7 @@ impl DiskTableWidget for Painter { app_state.canvas_data.disk_data.len() ); - if title_string.len() <= draw_loc.width as usize { + if title_string.len() <= draw_loc.width.into() { title_string } else { " Disk ".to_string() @@ -186,7 +188,7 @@ impl DiskTableWidget for Painter { let (chosen_title_base, expanded_title_base) = { let temp_title_base = format!("{}{}", title_base, ESCAPE_ENDING); - if temp_title_base.len() > draw_loc.width as usize { + if temp_title_base.len() > draw_loc.width.into() { ( " Disk ".to_string(), format!("{}{}", " Disk ", ESCAPE_ENDING), @@ -254,7 +256,7 @@ impl DiskTableWidget for Painter { .table_width_state .calculated_column_widths .iter() - .map(|calculated_width| Constraint::Length(*calculated_width as u16)) + .map(|calculated_width| Constraint::Length(*calculated_width)) .collect::<Vec<_>>()), ), margined_draw_loc, diff --git a/src/canvas/widgets/mem_graph.rs b/src/canvas/widgets/mem_graph.rs index b60fcdcb..50a626f1 100644 --- a/src/canvas/widgets/mem_graph.rs +++ b/src/canvas/widgets/mem_graph.rs @@ -50,7 +50,7 @@ impl MemGraphWidget for Painter { Axis::default().bounds([time_start, 0.0]) } else if let Some(time) = mem_widget_state.autohide_timer { if std::time::Instant::now().duration_since(time).as_millis() - < AUTOHIDE_TIMEOUT_MILLISECONDS as u128 + < AUTOHIDE_TIMEOUT_MILLISECONDS.into() { Axis::default() .bounds([time_start, 0.0]) diff --git a/src/canvas/widgets/network_graph.rs b/src/canvas/widgets/network_graph.rs index 8cdf9449..e40f294a 100644 --- a/src/canvas/widgets/network_graph.rs +++ b/src/canvas/widgets/network_graph.rs @@ -438,7 +438,7 @@ impl NetworkGraphWidget for Painter { Axis::default().bounds([time_start, 0.0]) } else if let Some(time) = network_widget_state.autohide_timer { if std::time::Instant::now().duration_since(time).as_millis() - < AUTOHIDE_TIMEOUT_MILLISECONDS as u128 + < AUTOHIDE_TIMEOUT_MILLISECONDS.into() { Axis::default() .bounds([time_start, 0.0]) @@ -761,7 +761,7 @@ impl NetworkGraphWidget for Painter { .widths( &(intrinsic_widths .iter() - .map(|calculated_width| Constraint::Length(*calculated_width as u16)) + .map(|calculated_width| Constraint::Length(*calculated_width)) .collect::<Vec<_>>()), ), draw_loc, diff --git a/src/canvas/widgets/process_table.rs b/src/canvas/widgets/process_table.rs index c65fe62f..d54818d1 100644 --- a/src/canvas/widgets/process_table.rs +++ b/src/canvas/widgets/process_table.rs @@ -215,7 +215,7 @@ impl ProcessTableWidget for Painter { finalized_process_data.len() ); - if title.len() <= draw_loc.width as usize { + if title.len() <= draw_loc.width.into() { title } else { " Processes ".to_string() @@ -239,7 +239,7 @@ impl ProcessTableWidget for Painter { let (chosen_title_base, expanded_title_base) = { let temp_title_base = format!("{}{}", title_base, ESCAPE_ENDING); - if temp_title_base.len() > draw_loc.width as usize { + if temp_title_base.len() > draw_loc.width.into() { ( " Processes ".to_string(), format!("{}{}", " Processes ", ESCAPE_ENDING), @@ -442,19 +442,21 @@ impl ProcessTableWidget for Painter { if *desired_col_width > *calculated_col_width && *calculated_col_width > 0 { + let calculated_col_width: usize = + (*calculated_col_width).into(); + let graphemes = UnicodeSegmentation::graphemes(entry.as_str(), true) .collect::<Vec<&str>>(); if let Some(alternative) = alternative { Text::raw(alternative) - } else if graphemes.len() > *calculated_col_width as usize - && *calculated_col_width > 1 + } else if graphemes.len() > calculated_col_width + && calculated_col_width > 1 { // Truncate with ellipsis - let first_n = graphemes - [..(*calculated_col_width as usize - 1)] - .concat(); + let first_n = + graphemes[..(calculated_col_width - 1)].concat(); Text::raw(format!("{}…", first_n)) } else { Text::raw(entry) @@ -493,9 +495,7 @@ impl ProcessTableWidget for Painter { .table_width_state .calculated_column_widths .iter() - .map(|calculated_width| { - Constraint::Length(*calculated_width as u16) - }) + .map(|calculated_width| Constraint::Length(*calculated_width)) .collect::<Vec<_>>()), ), margined_draw_loc, diff --git a/src/canvas/widgets/temp_table.rs b/src/canvas/widgets/temp_table.rs index 2ba0c61c..7e6c05da 100644 --- a/src/canvas/widgets/temp_table.rs +++ b/src/canvas/widgets/temp_table.rs @@ -111,17 +111,19 @@ impl TempTableWidget for Painter { if *desired_col_width > *calculated_col_width && *calculated_col_width > 0 { + let calculated_col_width: usize = + (*calculated_col_width).into(); + let graphemes = UnicodeSegmentation::graphemes(entry.as_str(), true) .collect::<Vec<&str>>(); - if graphemes.len() > *calculated_col_width as usize - && *calculated_col_width > 1 + if graphemes.len() > calculated_col_width + && calculated_col_width > 1 { // Truncate with ellipsis - let first_n = graphemes - [..(*calculated_col_width as usize - 1)] - .concat(); + let first_n = + graphemes[..(calculated_col_width - 1)].concat(); Text::raw(format!("{}…", first_n)) } else { Text::raw(entry) @@ -160,7 +162,7 @@ impl TempTableWidget for Painter { app_state.canvas_data.temp_sensor_data.len() ); - if title_string.len() <= draw_loc.width as usize { + if title_string.len() <= draw_loc.width.into() { title_string } else { " Temperatures ".to_string() @@ -175,7 +177,7 @@ impl TempTableWidget for Painter { let (chosen_title_base, expanded_title_base) = { let temp_title_base = format!("{}{}", title_base, ESCAPE_ENDING); - if temp_title_base.len() > draw_loc.width as usize { + if temp_title_base.len() > draw_loc.width.into() { ( " Temperatures ".to_string(), format!("{}{}", " Temperatures ", ESCAPE_ENDING), @@ -243,7 +245,7 @@ impl TempTableWidget for Painter { .table_width_state .calculated_column_widths .iter() - .map(|calculated_width| Constraint::Length(*calculated_width as u16)) + .map(|calculated_width| Constraint::Length(*calculated_width)) .collect::<Vec<_>>()), ), margined_draw_loc, diff --git a/src/options.rs b/src/options.rs index d469dc69..b7377e6e 100644 --- a/src/options.rs +++ b/src/options.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use std::{ borrow::Cow, collections::{HashMap, HashSet}, + convert::TryInto, path::PathBuf, str::FromStr, time::Instant, @@ -592,28 +593,28 @@ fn get_update_rate_in_milliseconds( matches: &clap::ArgMatches, config: &Config, ) -> error::Result<u64> { let update_rate_in_milliseconds = if let Some(update_rate) = matches.value_of("rate") { - update_rate.parse::<u128>()? + update_rate.parse::<u64>().map_err(|_| { + BottomError::ConfigError( + "could not parse as a valid 64-bit unsigned integer".to_string(), + ) + })? } else if let Some(flags) = &config.flags { if let Some(rate) = flags.rate { - rate as u128 + rate } else { - DEFAULT_REFRESH_RATE_IN_MILLISECONDS as u128 + DEFAULT_REFRESH_RATE_IN_MILLISECONDS } } else { - DEFAULT_REFRESH_RATE_IN_MILLISECONDS as u128 + DEFAULT_REFRESH_RATE_IN_MILLISECONDS }; if update_rate_in_milliseconds < 250 { return Err(BottomError::ConfigError( "set your update rate to be at least 250 milliseconds.".to_string(), )); - } else if update_rate_in_milliseconds as u128 > std::u64::MAX as u128 { - return Err(BottomError::ConfigError( - "set your update rate to be at most unsigned INT_MAX.".to_string(), - )); } - Ok(update_rate_in_milliseconds as u64) + Ok(update_rate_in_milliseconds) } fn get_temperature( @@ -704,56 +705,64 @@ fn get_use_basic_mode(matches: &clap::ArgMatches, config: &Config) -> bool { fn get_default_time_value(matches: &clap::ArgMatches, config: &Config) -> error::Result<u64> { let default_time = if let Some(default_time_value) = matches.value_of("default_time_value") { - default_time_value.parse::<u128>()? + default_time_value.parse::<u64>().map_err(|_| { + BottomError::ConfigError( + "could not parse as a valid 64-bit unsigned integer".to_string(), + ) + })? } else if let Some(flags) = &config.flags { if let Some(default_time_value) = flags.default_time_value { - default_time_value as u128 + default_time_value } else { - DEFAULT_TIME_MILLISECONDS as u128 + DEFAULT_TIME_MILLISECONDS } } else { - DEFAULT_TIME_MILLISECONDS as u128 + DEFAULT_TIME_MILLISECONDS }; if default_time < 30000 { return Err(BottomError::ConfigError( "set your default value to be at least 30000 milliseconds.".to_string(), )); - } else if default_time as u128 > STALE_MAX_MILLISECONDS as u128 { + } else if default_time > STALE_MAX_MILLISECONDS { return Err(BottomError::ConfigError(format!( "set your default value to be at most {} milliseconds.", STALE_MAX_MILLISECONDS ))); } - Ok(default_time as u64) + Ok(default_time) } fn get_time_interval(matches: &clap::ArgMatches, config: &Config) -> error::Result<u64> { let time_interval = if let Some(time_interval) = matches.value_of("time_delta") { - time_interval.parse::<u128>()? + time_interval.parse::<u64>().map_err(|_| { + BottomError::ConfigError( + "could not parse as a valid 64-bit unsigned integer".to_string(), + ) + })? } else if let Some(flags) = &config.flags { if let Some(time_interval) = flags.time_delta { - time_interval as u128 + time_interval } else { - TIME_CHANGE_MILLISECONDS as u128 + TIME_CHANGE_MILLISECONDS } } else { - TIME_CHANGE_MILLISECONDS as u128 + TIME_CHANGE_MILLISECONDS }; if time_interval < 1000 { return Err(BottomError::ConfigError( "set your time delta to be at least 1000 milliseconds.".to_string(), )); - } else if time_interval > STALE_MAX_MILLISECONDS as u128 { + } else if time_interval > STALE_MAX_MILLISECONDS { return Err(BottomError::ConfigError(format!( "set your time delta to be at most {} milliseconds.", STALE_MAX_MILLISECONDS ))); } - Ok(time_interval as u64) + Ok(time_interval) } pub fn get_app_grouping(matches: &clap::ArgMatches, config: &Config) -> bool { @@ -853,20 +862,17 @@ fn get_default_widget_and_count( } else if let Some(flags) = &config.flags { flags .default_widget_count - .map(|widget_count| widget_count as u128) + .map(|widget_count| widget_count.into()) } else { None }; match (widget_type, widget_count) { (Some(widget_type), Some(widget_count)) => { - if widget_count > std::u64::MAX as u128 { - Err(BottomError::ConfigError( - "set your widget count to be at most unsigned INT_MAX.".to_string(), - )) - } else { - Ok((Some(widget_type), widget_count as u64)) - } + let widget_count = widget_count.try_into().map_err(|_| BottomError::ConfigError( + "set your widget count to be at most unsigned INT_MAX.".to_string() + ))?; + Ok((Some(widget_type), widget_count)) } (Some(widget_type), None) => Ok((Some(widget_type), 1)), (None, Some(_widget_count)) => Err(BottomError::ConfigError( |