2023-03-01 11:09:08 +01:00
|
|
|
use bitcoin::{Block, OutPoint, Transaction, TxOut};
|
|
|
|
|
|
|
|
/// Trait to do something with every txout contained in a structure.
|
|
|
|
///
|
|
|
|
/// We would prefer just work with things that can give us a `Iterator<Item=(OutPoint, &TxOut)>`
|
|
|
|
/// here but rust's type system makes it extremely hard to do this (without trait objects).
|
|
|
|
pub trait ForEachTxOut {
|
|
|
|
/// The provided closure `f` will called with each `outpoint/txout` pair.
|
|
|
|
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut)));
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ForEachTxOut for Block {
|
|
|
|
fn for_each_txout(&self, mut f: impl FnMut((OutPoint, &TxOut))) {
|
|
|
|
for tx in self.txdata.iter() {
|
|
|
|
tx.for_each_txout(&mut f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:39:25 +13:00
|
|
|
impl ForEachTxOut for Transaction {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn for_each_txout(&self, mut f: impl FnMut((OutPoint, &TxOut))) {
|
2023-03-08 11:39:25 +13:00
|
|
|
let txid = self.txid();
|
|
|
|
for (i, txout) in self.output.iter().enumerate() {
|
2023-03-01 11:09:08 +01:00
|
|
|
f((
|
|
|
|
OutPoint {
|
|
|
|
txid,
|
|
|
|
vout: i as u32,
|
|
|
|
},
|
|
|
|
txout,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|