Introduce an iterator type for script_pubkey_lens

Currently `script_pubkey_lens` returns a generic `Iterator` using `impl`
syntax. This syntax is not supported in traits and we want to move the
function to the soon-to-be-added `TransactionExt` trait.

Add a struct to hold the iterator returned by `Map`, this is ugly but
its the least ugly thing I could come up with.
This commit is contained in:
Tobin C. Harding 2024-10-30 12:17:44 +11:00
parent 7196992d58
commit 3f6bc74ae4
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 14 additions and 2 deletions

View File

@ -501,8 +501,8 @@ impl Transaction {
/// This is useful in combination with [`predict_weight`] if you have the transaction already /// This is useful in combination with [`predict_weight`] if you have the transaction already
/// constructed with a dummy value in the fee output which you'll adjust after calculating the /// constructed with a dummy value in the fee output which you'll adjust after calculating the
/// weight. /// weight.
pub fn script_pubkey_lens(&self) -> impl Iterator<Item = usize> + '_ { pub fn script_pubkey_lens(&self) -> TxOutToScriptPubkeyLengthIter {
self.output.iter().map(|txout| txout.script_pubkey.len()) TxOutToScriptPubkeyLengthIter { inner: self.output.iter() }
} }
/// Counts the total number of sigops. /// Counts the total number of sigops.
@ -545,6 +545,18 @@ impl Transaction {
} }
} }
/// Iterates over transaction outputs and for each output yields the length of the scriptPubkey.
// This exists to hardcode the type of the closure crated by `map`.
pub struct TxOutToScriptPubkeyLengthIter<'a> {
inner: core::slice::Iter<'a, TxOut>,
}
impl Iterator for TxOutToScriptPubkeyLengthIter<'_> {
type Item = usize;
fn next(&mut self) -> Option<usize> { self.inner.next().map(|txout| txout.script_pubkey.len()) }
}
impl Transaction { impl Transaction {
/// Gets the sigop count. /// Gets the sigop count.
/// ///