Change Card::new to take the mapping by ownership

This commit is contained in:
pjht 2022-11-11 20:34:41 -06:00
parent ec7db304b8
commit 9ee6045d6f
7 changed files with 10 additions and 10 deletions

View File

@ -34,7 +34,7 @@ impl Backplane {
&mut self.cards &mut self.cards
} }
pub fn add_card(&mut self, type_name: &str, config: &Mapping) -> anyhow::Result<usize> { pub fn add_card(&mut self, type_name: &str, config: Mapping) -> anyhow::Result<usize> {
if self.cards.len() >= 255 { if self.cards.len() >= 255 {
return Err(CardAddError::BackplaneFull.into()); return Err(CardAddError::BackplaneFull.into());
} }

View File

@ -5,7 +5,7 @@ use std::fmt::{Debug, Display};
pub struct Type { pub struct Type {
pub name: &'static str, pub name: &'static str,
new: fn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>, new: fn(data: Mapping) -> anyhow::Result<Box<dyn Card>>,
} }
impl Type { impl Type {
@ -16,7 +16,7 @@ impl Type {
} }
} }
pub fn new_card(&self, data: &Mapping) -> anyhow::Result<Box<dyn Card>> { pub fn new_card(&self, data: Mapping) -> anyhow::Result<Box<dyn Card>> {
(self.new)(data) (self.new)(data)
} }
} }
@ -24,10 +24,10 @@ impl Type {
inventory::collect!(Type); inventory::collect!(Type);
pub trait Card: Debug + Display { pub trait Card: Debug + Display {
fn new(data: &Mapping) -> anyhow::Result<Self> fn new(data: Mapping) -> anyhow::Result<Self>
where where
Self: Sized; Self: Sized;
fn new_dyn(data: &Mapping) -> anyhow::Result<Box<dyn Card>> fn new_dyn(data: Mapping) -> anyhow::Result<Box<dyn Card>>
where where
Self: Sized + 'static, Self: Sized + 'static,
{ {

View File

@ -63,7 +63,7 @@ fn main() -> Result<(), anyhow::Error> {
.map_err(|e| anyhow!("Could not parse config file ({})", e))?; .map_err(|e| anyhow!("Could not parse config file ({})", e))?;
let mut backplane = Backplane::new(); let mut backplane = Backplane::new();
for card in config.cards { 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(); let mut symbol_tables = SymbolTables::new();
if let Some(initial_tables) = config.symbol_tables { if let Some(initial_tables) = config.symbol_tables {

View File

@ -21,7 +21,7 @@ pub struct Ram {
impl Ram {} impl Ram {}
impl Card for Ram { impl Card for Ram {
fn new(data: &Mapping) -> anyhow::Result<Self> { fn new(data: Mapping) -> anyhow::Result<Self> {
let size = data let size = data
.get("size") .get("size")
.ok_or_else(|| anyhow!("No size value for RAM"))? .ok_or_else(|| anyhow!("No size value for RAM"))?

View File

@ -23,7 +23,7 @@ pub struct Rom {
impl Rom {} impl Rom {}
impl Card for Rom { impl Card for Rom {
fn new(data: &Mapping) -> anyhow::Result<Self> { fn new(data: Mapping) -> anyhow::Result<Self> {
let file_name = data let file_name = data
.get("image") .get("image")
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))

View File

@ -22,7 +22,7 @@ pub struct Storage {
} }
impl Card for Storage { impl Card for Storage {
fn new(data: &Mapping) -> anyhow::Result<Self> { fn new(data: Mapping) -> anyhow::Result<Self> {
let file_name = data let file_name = data
.get("image") .get("image")
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))

View File

@ -15,7 +15,7 @@ impl Display for Term {
} }
impl Card for Term { impl Card for Term {
fn new(_data: &Mapping) -> anyhow::Result<Self> fn new(_data: Mapping) -> anyhow::Result<Self>
where where
Self: Sized, Self: Sized,
{ {