From 86a1df04ad0fe2827a35efbdbba5dea83a491457 Mon Sep 17 00:00:00 2001 From: Scott Pruett Date: Sat, 11 Jun 2022 14:07:59 -0400 Subject: [PATCH] reduce int sizing --- src/analysis.rs | 4 ++-- src/main.rs | 18 +++++++++--------- src/state.rs | 40 ++++++++++++++++++++-------------------- src/tts.rs | 2 +- 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/analysis.rs b/src/analysis.rs index 0797060..dc28bc3 100644 --- a/src/analysis.rs +++ b/src/analysis.rs @@ -158,8 +158,8 @@ fn handle_new_frame(state: &mut AppState, lap_state: LapState, image: &RgbImage) merged.lap = prev_lap.lap.map(|l| l + 1); } } - if merged.lap.is_none() { - merged.lap = Some(race.laps.len() + 1); + if merged.lap.is_none() || merged.total_laps.is_none() { + merged.lap = Some((race.laps.len() + 1) as u16); } crate::tts::lap_summary(&state.tts, state.config.as_ref(), &merged, race); diff --git a/src/main.rs b/src/main.rs index 9ec15c3..8d89814 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,11 +78,11 @@ fn main() -> anyhow::Result<()> { ); } -fn show_optional_usize_with_diff( +fn show_optional_u8_with_diff( ui: &mut Ui, color: Color32, - old: &Option, - v: &Option, + old: &Option, + v: &Option, ) { let diff_string = match (old, v) { (Some(old), Some(v)) => { @@ -217,8 +217,8 @@ fn show_race_state( let fastest_lap = race.fastest_lap(); for (i, lap) in race.laps.iter_mut().enumerate() { if let Some(prev) = &prev_lap { - if lap.gas.unwrap_or_default() > prev.gas.unwrap_or(usize::MAX) - || lap.tyres.unwrap_or_default() > prev.tyres.unwrap_or(usize::MAX) + if lap.gas.unwrap_or_default() > prev.gas.unwrap_or(u8::MAX) + || lap.tyres.unwrap_or_default() > prev.tyres.unwrap_or(u8::MAX) { ui.separator(); ui.separator(); @@ -230,7 +230,7 @@ fn show_race_state( } } if let Some(lap_time) = lap.lap_time { - ui.label(format!("#{}", lap.lap.unwrap_or(i + 1))); + ui.label(format!("#{}", lap.lap.unwrap_or((i + 1) as u16))); ui.label(format!("P{}", lap.position.unwrap_or_default())); match lap @@ -263,19 +263,19 @@ fn show_race_state( label_time_delta(ui, lap_time, prev_lap.and_then(|p| p.lap_time)); label_time_delta(ui, lap_time, fastest_lap); - show_optional_usize_with_diff( + show_optional_u8_with_diff( ui, Color32::RED, &prev_lap.and_then(|p| p.health), &lap.health, ); - show_optional_usize_with_diff( + show_optional_u8_with_diff( ui, Color32::LIGHT_BLUE, &prev_lap.and_then(|p| p.gas), &lap.gas, ); - show_optional_usize_with_diff( + show_optional_u8_with_diff( ui, Color32::GREEN, &prev_lap.and_then(|p| p.tyres), diff --git a/src/state.rs b/src/state.rs index b621257..270a732 100644 --- a/src/state.rs +++ b/src/state.rs @@ -13,15 +13,15 @@ use crate::{config::Config, learned_tracks::LearnedTracks, ocr_db::OcrDatabase, #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct LapState { - pub lap: Option, - pub total_laps: Option, + pub lap: Option, + pub total_laps: Option, - pub position: Option, - pub total_positions: Option, + pub position: Option, + pub total_positions: Option, - pub health: Option, - pub gas: Option, - pub tyres: Option, + pub health: Option, + pub gas: Option, + pub tyres: Option, pub best_time: Option, pub lap_time: Option, @@ -48,7 +48,7 @@ fn parse_to_duration(time: Option<&String>) -> Option { parse_duration(time?) } -fn check_0_100(v: usize) -> Option { +fn check_0_100(v: u8) -> Option { if v > 100 { None } else { @@ -56,11 +56,11 @@ fn check_0_100(v: usize) -> Option { } } -fn parse_to_0_100(v: Option<&String>) -> Option { - check_0_100(v?.parse::().ok()?) +fn parse_to_0_100(v: Option<&String>) -> Option { + check_0_100(v?.parse::().ok()?) } -fn parse_with_slash(v: Option<&String>) -> (Option, Option) { +fn parse_with_slash(v: Option<&String>) -> (Option, Option) { let v = match v { Some(v) => v, None => return (None, None), @@ -90,7 +90,7 @@ impl LapState { } } -fn wear_values(values: &[Option]) -> Vec { +fn wear_values(values: &[Option]) -> Vec { let mut wear_values = Vec::new(); let mut last_value = 100; for val in values.iter().flatten() { @@ -102,7 +102,7 @@ fn wear_values(values: &[Option]) -> Vec { wear_values } -fn median_wear(values: Vec>) -> Option { +fn median_wear(values: Vec>) -> Option { let mut wear_values = wear_values(&values); wear_values.sort_unstable(); wear_values.get(wear_values.len() / 2).cloned() @@ -115,9 +115,9 @@ pub struct RaceState { #[serde(skip)] pub last_lap_record_time: Option, - pub starting_position: usize, - pub total_laps: usize, - pub total_positions: usize, + pub starting_position: u16, + pub total_laps: u16, + pub total_positions: u16, #[serde(skip)] pub screencap: Option, @@ -154,15 +154,15 @@ impl RaceState { self.laps.iter().filter(|lap| !lap.striked).filter_map(|lap| lap.lap_time).min() } - pub fn tyre_wear(&self) -> Option { + pub fn tyre_wear(&self) -> Option { median_wear(self.laps.iter().map(|lap| lap.tyres).collect()) } - pub fn gas_per_lap(&self) -> Option { + pub fn gas_per_lap(&self) -> Option { median_wear(self.laps.iter().map(|lap| lap.gas).collect()) } - pub fn latest_gas_diff(&self, to: usize) -> Option { + pub fn latest_gas_diff(&self, to: u8) -> Option { let last_gas = self.laps.iter().last().map(|l| l.gas).unwrap_or(Some(100))?; if to < last_gas { Some(last_gas - to) @@ -170,7 +170,7 @@ impl RaceState { None } } - pub fn latest_tyre_diff(&self, to: usize) -> Option { + pub fn latest_tyre_diff(&self, to: u8) -> Option { let last_tyres = self.laps.iter().last().map(|l| l.tyres).unwrap_or(Some(100))?; if to < last_tyres { Some(last_tyres - to) diff --git a/src/tts.rs b/src/tts.rs index 7cc31d3..d132ee6 100644 --- a/src/tts.rs +++ b/src/tts.rs @@ -82,7 +82,7 @@ pub fn lap_summary(tts: &Tts, config: &Config, lap_state: &LapState, race: &Race if lap == 1 { let laps_per_tank = 100 / gas_wear; - let pit_stops = race.total_laps / laps_per_tank; + let pit_stops = race.total_laps / (laps_per_tank as u16); tts.speak( config, TtsKind::FirstLap,