From 9ee6045d6fb26b357ee6b8b2faa7b2101f033fa3 Mon Sep 17 00:00:00 2001 From: pjht Date: Fri, 11 Nov 2022 20:34:41 -0600 Subject: [PATCH] Change Card::new to take the mapping by ownership --- src/backplane.rs | 2 +- src/card.rs | 8 ++++---- src/main.rs | 2 +- src/ram.rs | 2 +- src/rom.rs | 2 +- src/storage.rs | 2 +- src/term.rs | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/backplane.rs b/src/backplane.rs index 18afd48..0d60472 100644 --- a/src/backplane.rs +++ b/src/backplane.rs @@ -34,7 +34,7 @@ impl Backplane { &mut self.cards } - pub fn add_card(&mut self, type_name: &str, config: &Mapping) -> anyhow::Result { + pub fn add_card(&mut self, type_name: &str, config: Mapping) -> anyhow::Result { if self.cards.len() >= 255 { return Err(CardAddError::BackplaneFull.into()); } diff --git a/src/card.rs b/src/card.rs index f2016fb..9e885cc 100644 --- a/src/card.rs +++ b/src/card.rs @@ -5,7 +5,7 @@ use std::fmt::{Debug, Display}; pub struct Type { pub name: &'static str, - new: fn(data: &Mapping) -> anyhow::Result>, + new: fn(data: Mapping) -> anyhow::Result>, } impl Type { @@ -16,7 +16,7 @@ impl Type { } } - pub fn new_card(&self, data: &Mapping) -> anyhow::Result> { + pub fn new_card(&self, data: Mapping) -> anyhow::Result> { (self.new)(data) } } @@ -24,10 +24,10 @@ impl Type { inventory::collect!(Type); pub trait Card: Debug + Display { - fn new(data: &Mapping) -> anyhow::Result + fn new(data: Mapping) -> anyhow::Result where Self: Sized; - fn new_dyn(data: &Mapping) -> anyhow::Result> + fn new_dyn(data: Mapping) -> anyhow::Result> where Self: Sized + 'static, { diff --git a/src/main.rs b/src/main.rs index 32e6a46..53cc292 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,7 +63,7 @@ fn main() -> Result<(), anyhow::Error> { .map_err(|e| anyhow!("Could not parse config file ({})", e))?; let mut backplane = Backplane::new(); for card in config.cards { - backplane.add_card(card.typ, &card.config)?; + backplane.add_card(card.typ, card.config)?; } let mut symbol_tables = SymbolTables::new(); if let Some(initial_tables) = config.symbol_tables { diff --git a/src/ram.rs b/src/ram.rs index b1efa08..aa49484 100644 --- a/src/ram.rs +++ b/src/ram.rs @@ -21,7 +21,7 @@ pub struct Ram { impl Ram {} impl Card for Ram { - fn new(data: &Mapping) -> anyhow::Result { + fn new(data: Mapping) -> anyhow::Result { let size = data .get("size") .ok_or_else(|| anyhow!("No size value for RAM"))? diff --git a/src/rom.rs b/src/rom.rs index 8d2d811..0833312 100644 --- a/src/rom.rs +++ b/src/rom.rs @@ -23,7 +23,7 @@ pub struct Rom { impl Rom {} impl Card for Rom { - fn new(data: &Mapping) -> anyhow::Result { + fn new(data: Mapping) -> anyhow::Result { let file_name = data .get("image") .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) diff --git a/src/storage.rs b/src/storage.rs index b8a0893..44d1583 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -22,7 +22,7 @@ pub struct Storage { } impl Card for Storage { - fn new(data: &Mapping) -> anyhow::Result { + fn new(data: Mapping) -> anyhow::Result { let file_name = data .get("image") .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) diff --git a/src/term.rs b/src/term.rs index fffb280..903f3ab 100644 --- a/src/term.rs +++ b/src/term.rs @@ -15,7 +15,7 @@ impl Display for Term { } impl Card for Term { - fn new(_data: &Mapping) -> anyhow::Result + fn new(_data: Mapping) -> anyhow::Result where Self: Sized, {