From 3f6bc74ae48b984f427a91a98a672aaeedb7b684 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 30 Oct 2024 12:17:44 +1100 Subject: [PATCH] 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. --- bitcoin/src/blockdata/transaction.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 7e90c4425..f0c755e50 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -501,8 +501,8 @@ impl Transaction { /// 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 /// weight. - pub fn script_pubkey_lens(&self) -> impl Iterator + '_ { - self.output.iter().map(|txout| txout.script_pubkey.len()) + pub fn script_pubkey_lens(&self) -> TxOutToScriptPubkeyLengthIter { + TxOutToScriptPubkeyLengthIter { inner: self.output.iter() } } /// 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 { self.inner.next().map(|txout| txout.script_pubkey.len()) } +} + impl Transaction { /// Gets the sigop count. ///