From 7656d04cd8fc0b534f58a81846151a79fd9f25a7 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 14 Nov 2022 10:54:10 -0600 Subject: [PATCH] Remove Backplane::{cards,cards_mut} --- src/backplane.rs | 31 ++++++++++++++++++++++++++----- src/main.rs | 38 ++++++++++---------------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/backplane.rs b/src/backplane.rs index c4a4b90..86712c8 100644 --- a/src/backplane.rs +++ b/src/backplane.rs @@ -1,4 +1,7 @@ +use std::fmt::Display; + use anyhow::anyhow; +use itertools::Itertools; use nullable_result::{GeneralIterExt, NullableResult}; use crate::{ @@ -11,6 +14,20 @@ pub struct Backplane { cards: Vec>, } +impl Display for Backplane { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_fmt(format_args!( + "{}", + self.cards + .iter() + .enumerate() + .format_with("\n", |(i, card), g| { + g(&format_args!("Card {i}: {card}")) + }) + )) + } +} + impl Backplane { pub fn new(cards: Vec>) -> anyhow::Result { if cards.len() > 255 { @@ -24,13 +41,17 @@ impl Backplane { }) } - #[allow(dead_code)] - pub fn cards(&self) -> &[Box] { - self.cards.as_ref() + pub fn reset(&mut self) { + for card in &mut self.cards { + card.reset(); + } } - pub fn cards_mut(&mut self) -> &mut Vec> { - &mut self.cards + pub fn card_cmd(&mut self, card_num: u8, cmd: &[&str]) -> anyhow::Result<()> { + self.cards + .get_mut(card_num as usize) + .ok_or_else(|| anyhow!("Card {} does not exist", card_num))? + .cmd(cmd) } pub fn read_word(&mut self, address: u32) -> Result { diff --git a/src/main.rs b/src/main.rs index 07a5b39..9b38b37 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,36 +85,20 @@ fn main() -> Result<(), anyhow::Error> { .about("Send a command to a card"), |args, state| { let num = args.get_one::("num").unwrap().parse::()?; - state - .cpu - .bus_mut() - .cards_mut() - .get_mut(num as usize) - .ok_or_else(|| anyhow!("Card {} does not exist", num))? - .cmd( - &args - .get_many::("cmd") - .unwrap() - .map(String::as_str) - .collect_vec(), - )?; + state.cpu.bus_mut().card_cmd( + num, + &args + .get_many::("cmd") + .unwrap() + .map(String::as_str) + .collect_vec(), + )?; Ok(None) }, ) .with_command( Command::new("ls").about("List the cards in the system"), - |_, state| { - Ok(Some( - state - .cpu - .bus_mut() - .cards() - .iter() - .enumerate() - .map(|(i, card)| format!("Card {i}: {card}")) - .join("\n"), - )) - }, + |_, state| Ok(Some(state.cpu.bus_mut().to_string())), ) .with_command( Command::new("regs").about("Show CPU registers"), @@ -209,9 +193,7 @@ fn main() -> Result<(), anyhow::Error> { .with_command( Command::new("reset").about("Reset the cards and CPU, in that order"), |_, state| { - for card in state.cpu.bus_mut().cards_mut() { - card.reset(); - } + state.cpu.bus_mut().reset(); state.cpu.reset(); Ok(None) },