Change Card::new to take the mapping by ownership
This commit is contained in:
parent
ec7db304b8
commit
9ee6045d6f
@ -34,7 +34,7 @@ impl Backplane {
|
||||
&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 {
|
||||
return Err(CardAddError::BackplaneFull.into());
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use std::fmt::{Debug, Display};
|
||||
|
||||
pub struct Type {
|
||||
pub name: &'static str,
|
||||
new: fn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>,
|
||||
new: fn(data: Mapping) -> anyhow::Result<Box<dyn Card>>,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -24,10 +24,10 @@ impl Type {
|
||||
inventory::collect!(Type);
|
||||
|
||||
pub trait Card: Debug + Display {
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self>
|
||||
fn new(data: Mapping) -> anyhow::Result<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
fn new_dyn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>
|
||||
fn new_dyn(data: Mapping) -> anyhow::Result<Box<dyn Card>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
|
@ -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 {
|
||||
|
@ -21,7 +21,7 @@ pub struct Ram {
|
||||
impl Ram {}
|
||||
|
||||
impl Card for Ram {
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self> {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let size = data
|
||||
.get("size")
|
||||
.ok_or_else(|| anyhow!("No size value for RAM"))?
|
||||
|
@ -23,7 +23,7 @@ pub struct Rom {
|
||||
impl Rom {}
|
||||
|
||||
impl Card for Rom {
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self> {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = data
|
||||
.get("image")
|
||||
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))
|
||||
|
@ -22,7 +22,7 @@ pub struct Storage {
|
||||
}
|
||||
|
||||
impl Card for Storage {
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self> {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = data
|
||||
.get("image")
|
||||
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))
|
||||
|
@ -15,7 +15,7 @@ impl Display for Term {
|
||||
}
|
||||
|
||||
impl Card for Term {
|
||||
fn new(_data: &Mapping) -> anyhow::Result<Self>
|
||||
fn new(_data: Mapping) -> anyhow::Result<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user