From 90bf6aa04f6acfc1c7cc3f18c57f92472a28fea7 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 31 Jan 2024 15:23:48 -0600 Subject: [PATCH] Clean up code --- src/audio.rs | 4 ++-- src/card.rs | 1 + src/cpu.rs | 2 +- src/frontpanel.rs | 37 +++++++------------------------------ src/frontpanel/switch.rs | 28 ++++++++++++++-------------- src/load_bin_window.rs | 14 ++++++-------- src/main.rs | 1 - src/option_window.rs | 2 +- src/state.rs | 13 ++++++------- 9 files changed, 38 insertions(+), 64 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index 5a6ce9f..61f1063 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -101,7 +101,7 @@ impl AudioThread { .unwrap(); sw_click }; - let slf: AudioThread = Self { + let slf = Self { fan_startup, fan_loop, fan_shutdown, @@ -183,7 +183,7 @@ impl AudioThread { self.fan_state = FanState::On; } AudioMessage::FanOff => match self.fan_state { - FanState::Off | FanState::Stopping { .. } => return, + FanState::Off | FanState::Stopping { .. } => (), FanState::Starting { start_time, sound_offset, diff --git a/src/card.rs b/src/card.rs index ef99dd0..899c856 100644 --- a/src/card.rs +++ b/src/card.rs @@ -9,6 +9,7 @@ pub enum CardEnum { #[enum_dispatch] pub trait Card { + #[allow(clippy::new_ret_no_self)] fn new(_settings: ron::Value) -> CardEnum; fn read_mem(&mut self, _address: u16) -> Option { diff --git a/src/cpu.rs b/src/cpu.rs index e193e96..d9fb588 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -13,7 +13,7 @@ use ux::{u4, u5}; use self::opcode::{Condition, Opcode, Register, RegisterPair}; bitflags! { - #[derive(Clone, Copy, Debug, PartialEq, Eq)] + #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub struct Status: u8 { const INTA = 0x1; const WO = 0x2; diff --git a/src/frontpanel.rs b/src/frontpanel.rs index cad037a..94d297d 100644 --- a/src/frontpanel.rs +++ b/src/frontpanel.rs @@ -26,12 +26,11 @@ pub enum ActionSwitch { #[derive(Clone, Copy, Debug)] pub enum FrontpanelInteraction { - ActionSwChanged(ActionSwitch, SwitchState), - AdChanged(u16), - PowerChanged(bool), + ActionSw(ActionSwitch, SwitchState), + Power(bool), } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Default)] pub struct FrontpanelState { ad_sws: u16, power: bool, @@ -49,24 +48,6 @@ pub struct FrontpanelState { } impl FrontpanelState { - pub fn new() -> Self { - Self { - ad_sws: 0, - power: false, - runstop: SwitchState::Neut, - single_step: SwitchState::Neut, - exam: SwitchState::Neut, - dep: SwitchState::Neut, - reset: SwitchState::Neut, - prot: SwitchState::Neut, - aux1: SwitchState::Neut, - aux2: SwitchState::Neut, - addr: 0, - data: 0, - status: Status::empty(), - } - } - pub fn ad_sws(&self) -> u16 { self.ad_sws } @@ -154,8 +135,7 @@ impl Widget for &mut Frontpanel<'_> { .changed() { self.state.power = !power_inv; - self.interaction = - Some(FrontpanelInteraction::PowerChanged(self.state.power)); + self.interaction = Some(FrontpanelInteraction::Power(self.state.power)); }; }); } @@ -165,7 +145,7 @@ impl Widget for &mut Frontpanel<'_> { ui.allocate_ui_at_rect(Rect::from_center_size(pos, vec2(11.0, 25.0)), |ui| { let key_offset = (i - 1) % 8; let key = ["Q", "W", "E", "R", "T", "Y", "U", "I"][key_offset]; - let key = Key::from_name(&key).unwrap(); + let key = Key::from_name(key).unwrap(); let mods = if (i - 1) < 8 { Modifiers::SHIFT } else { @@ -181,9 +161,7 @@ impl Widget for &mut Frontpanel<'_> { .changed() { self.state.ad_sws = - (self.state.ad_sws & !(bit_mask)) | ((sw_state as u16) << (16 - i)); - self.interaction = - Some(FrontpanelInteraction::AdChanged(self.state.ad_sws)); + (self.state.ad_sws & !(bit_mask)) | (u16::from(sw_state) << (16 - i)); }; }); } @@ -227,8 +205,7 @@ impl Widget for &mut Frontpanel<'_> { 24 => ActionSwitch::Aux2, _ => unreachable!(), }; - self.interaction = - Some(FrontpanelInteraction::ActionSwChanged(sw, *state)); + self.interaction = Some(FrontpanelInteraction::ActionSw(sw, *state)); }; }); } diff --git a/src/frontpanel/switch.rs b/src/frontpanel/switch.rs index b528645..1a8ee54 100644 --- a/src/frontpanel/switch.rs +++ b/src/frontpanel/switch.rs @@ -7,33 +7,33 @@ const NULL_UV: Rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)); const NULL_TINT: Color32 = Color32::WHITE; pub struct Textures { - sw_up: TextureHandle, - sw_neut: TextureHandle, - sw_down: TextureHandle, + up: TextureHandle, + neut: TextureHandle, + down: TextureHandle, } impl Textures { - pub fn new(sw_up: TextureHandle, sw_neut: TextureHandle, sw_down: TextureHandle) -> Self { + pub fn new(up: TextureHandle, neut: TextureHandle, down: TextureHandle) -> Self { Self { - sw_up, - sw_neut, - sw_down, + up, + neut, + down, } } pub fn get_for_state(&self, state: SwitchState) -> TextureId { match state { - SwitchState::Up => self.sw_up.id(), - SwitchState::Neut => self.sw_neut.id(), - SwitchState::Down => self.sw_down.id(), + SwitchState::Up => self.up.id(), + SwitchState::Neut => self.neut.id(), + SwitchState::Down => self.down.id(), } } pub fn get_for_bool(&self, state: bool) -> TextureId { if state { - self.sw_up.id() + self.up.id() } else { - self.sw_down.id() + self.down.id() } } } @@ -106,8 +106,8 @@ impl Widget for ThreePosSwitch<'_> { let old_state = *self.state; if resp.dragged() { let interact_pos = resp.interact_pointer_pos().unwrap(); - let sw_center = painter.clip_rect().left_top() + vec2(5.0, 12.0); - if interact_pos.y > sw_center.y { + let center = painter.clip_rect().left_top() + vec2(5.0, 12.0); + if interact_pos.y > center.y { *self.state = SwitchState::Down; } else { *self.state = SwitchState::Up; diff --git a/src/load_bin_window.rs b/src/load_bin_window.rs index 9e3e890..9d2f78c 100644 --- a/src/load_bin_window.rs +++ b/src/load_bin_window.rs @@ -65,7 +65,7 @@ impl LoadBinWindow { match self.ftype { BinaryType::Raw => { let bin = std::fs::read(path).unwrap(); - state.write_binary(self.start_addr as usize, bin); + state.write_binary(self.start_addr as usize, &bin); } BinaryType::IntelHex => { let data = std::fs::read_to_string(path).unwrap(); @@ -73,7 +73,7 @@ impl LoadBinWindow { let record = record.unwrap(); match record { ihex::Record::Data { offset, value } => { - state.write_binary(offset as usize, value); + state.write_binary(offset as usize, &value); } ihex::Record::StartLinearAddress(_) => todo!(), ihex::Record::EndOfFile => (), @@ -94,12 +94,10 @@ impl Window for LoadBinWindow { if ui.button("Choose file").clicked() { self.choose_file(); } - ui.label(format!( - "{}", - self.path.as_ref().map_or("".into(), |x| x - .file_name() - .map_or("".into(), |x| x.to_string_lossy().to_string())) - )); + ui.label(self.path.as_ref().map_or(String::new(), |x| { + x.file_name() + .map_or(String::new(), |x| x.to_string_lossy().to_string()) + })); }); egui::ComboBox::from_label("File type") .selected_text(format!("{}", self.ftype)) diff --git a/src/main.rs b/src/main.rs index 281132d..38c2722 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,6 @@ use eframe::{ use frontpanel::Textures; use load_bin_window::LoadBinWindow; use option_window::OptionWindow; -use rfd::FileDialog; use serde::{Deserialize, Serialize}; use state::EmuState; use window::Window; diff --git a/src/option_window.rs b/src/option_window.rs index 1b7ecbb..f41fe41 100644 --- a/src/option_window.rs +++ b/src/option_window.rs @@ -18,7 +18,7 @@ impl OptionWindow { pub fn new(ctx: &egui::Context, state: &EmuState) -> Self { Modal::new(ctx, "options_modal").open(); Self { - options: state.options().clone(), + options: state.options(), category: OptionsCategory::General, } } diff --git a/src/state.rs b/src/state.rs index dae89e0..deb994d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -22,13 +22,13 @@ impl EmuState { pub fn new(audio_tx: Sender, options: Options) -> Self { let mut mem = [0; 65536]; rand::thread_rng().fill_bytes(&mut mem); - let mut slf = EmuState { + let mut slf = Self { mem, cpu: I8080::new(), running: false, audio_tx, options, - fp_state: FrontpanelState::new(), + fp_state: FrontpanelState::default(), }; slf.apply_options(); slf @@ -37,7 +37,7 @@ impl EmuState { pub fn handle_fp_interaction(&mut self, interaction: FrontpanelInteraction) { self.audio_tx.send(AudioMessage::PlaySwitchClick).unwrap(); match interaction { - FrontpanelInteraction::ActionSwChanged(sw, state) => { + FrontpanelInteraction::ActionSw(sw, state) => { if self.fp_state.power() { match sw { ActionSwitch::RunStop => { @@ -92,8 +92,7 @@ impl EmuState { } } } - FrontpanelInteraction::AdChanged(_) => (), - FrontpanelInteraction::PowerChanged(pwr) => { + FrontpanelInteraction::Power(pwr) => { if pwr { self.audio_tx.send(AudioMessage::FanOn).unwrap(); self.cpu = I8080::new(); @@ -154,9 +153,9 @@ impl EmuState { self.cpu.finish_m_cycle(data); } - pub fn write_binary(&mut self, start: usize, data: Vec) { + pub fn write_binary(&mut self, start: usize, data: &[u8]) { assert!(0x1_0000 - start >= data.len()); - self.mem[start..(start + data.len())].copy_from_slice(&data); + self.mem[start..(start + data.len())].copy_from_slice(data); } pub fn update_options(&mut self, new_opts: Options) {