Skip to content

Commit

Permalink
feat: add SpendBook::entries() to enforce type of Iterator values
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-da committed Jun 24, 2021
1 parent 5bddb4c commit 5a6d20e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
4 changes: 2 additions & 2 deletions examples/mint-repl/mint-repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use serde::{Deserialize, Serialize};
use sn_dbc::{
BlindedOwner, Dbc, DbcContent, DbcTransaction, Hash, Mint, MintSignatures, NodeSignature,
ReissueRequest, ReissueTransaction, SimpleKeyManager as KeyManager, SimpleSigner as Signer,
SimpleSpendBook as SpendBook,
SimpleSpendBook as SpendBook, SpendBook as SpendBookTrait,
};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::iter::FromIterator;
Expand Down Expand Up @@ -360,7 +360,7 @@ fn print_mintinfo_human(mintinfo: &MintInfo) -> Result<()> {
println!("\n");

println!("-- SpendBook --\n");
for (dbchash, _tx) in mintinfo.mintnode()?.spendbook.transactions.iter() {
for (dbchash, _tx) in mintinfo.mintnode()?.spendbook.entries() {
println!(" {}", encode(&dbchash));
}

Expand Down
13 changes: 5 additions & 8 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub type MintSignatures = BTreeMap<DbcContentHash, (PublicKeySet, NodeSignature)

pub const GENESIS_DBC_INPUT: Hash = Hash([0u8; 32]);

pub trait SpendBook: std::fmt::Debug + Clone + IntoIterator {
pub trait SpendBook: std::fmt::Debug + Clone {
type Error: std::error::Error;

fn lookup(&self, dbc_hash: &DbcContentHash) -> Result<Option<&DbcTransaction>, Self::Error>;
Expand All @@ -36,6 +36,8 @@ pub trait SpendBook: std::fmt::Debug + Clone + IntoIterator {
dbc_hash: DbcContentHash,
transaction: DbcTransaction,
) -> Result<(), Self::Error>;

fn entries(&self) -> Box<dyn Iterator<Item = (&DbcContentHash, &DbcTransaction)> + '_>;
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand All @@ -58,14 +60,9 @@ impl SpendBook for SimpleSpendBook {
self.transactions.insert(dbc_hash, transaction);
Ok(())
}
}

impl IntoIterator for SimpleSpendBook {
type Item = (DbcContentHash, DbcTransaction);
type IntoIter = std::collections::btree_map::IntoIter<DbcContentHash, DbcTransaction>;

fn into_iter(self) -> Self::IntoIter {
self.transactions.into_iter()
fn entries(&self) -> Box<dyn Iterator<Item = (&DbcContentHash, &DbcTransaction)> + '_> {
Box::new(self.transactions.iter())
}
}

Expand Down

0 comments on commit 5a6d20e

Please sign in to comment.