Use Borrow for all items in IntoIterator

This commit is contained in:
Riccardo Casatta 2023-03-22 17:02:40 +01:00
parent bf2f8ef2e5
commit 40cecd5010
No known key found for this signature in database
GPG Key ID: FD986A969E450397
4 changed files with 55 additions and 27 deletions

View File

@ -39,7 +39,8 @@ pub trait ElectrumApi {
/// Takes a list of `txids` and returns a list of transactions.
fn batch_transaction_get<'t, I>(&self, txids: I) -> Result<Vec<Transaction>, Error>
where
I: IntoIterator<Item = &'t Txid> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'t Txid>,
{
self.batch_transaction_get_raw(txids)?
.iter()
@ -52,7 +53,8 @@ pub trait ElectrumApi {
/// Takes a list of `heights` of blocks and returns a list of headers.
fn batch_block_header<I>(&self, heights: I) -> Result<Vec<BlockHeader>, Error>
where
I: IntoIterator<Item = u32> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<u32>,
{
self.batch_block_header_raw(heights)?
.iter()
@ -110,7 +112,7 @@ pub trait ElectrumApi {
/// Batch version of [`script_subscribe`](#method.script_subscribe).
///
/// Takes a list of scripts and returns a list of script status responses.
///
///
/// Note you should pass a reference to a collection because otherwise an expensive clone is made
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
where
@ -136,7 +138,8 @@ pub trait ElectrumApi {
/// Takes a list of scripts and returns a list of balance responses.
fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result<Vec<GetBalanceRes>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone;
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>;
/// Returns the history for a *scriptPubKey*
fn script_get_history(&self, script: &Script) -> Result<Vec<GetHistoryRes>, Error>;
@ -146,7 +149,8 @@ pub trait ElectrumApi {
/// Takes a list of scripts and returns a list of history responses.
fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result<Vec<Vec<GetHistoryRes>>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone;
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>;
/// Returns the list of unspent outputs for a *scriptPubKey*
fn script_list_unspent(&self, script: &Script) -> Result<Vec<ListUnspentRes>, Error>;
@ -159,7 +163,8 @@ pub trait ElectrumApi {
scripts: I,
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone;
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>;
/// Gets the raw bytes of a transaction with `txid`. Returns an error if not found.
fn transaction_get_raw(&self, txid: &Txid) -> Result<Vec<u8>, Error>;
@ -169,14 +174,16 @@ pub trait ElectrumApi {
/// Takes a list of `txids` and returns a list of transactions raw bytes.
fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result<Vec<Vec<u8>>, Error>
where
I: IntoIterator<Item = &'t Txid> + Clone;
I: IntoIterator + Clone,
I::Item: Borrow<&'t Txid>;
/// Batch version of [`block_header_raw`](#method.block_header_raw).
///
/// Takes a list of `heights` of blocks and returns a list of block header raw bytes.
fn batch_block_header_raw<I>(&self, heights: I) -> Result<Vec<Vec<u8>>, Error>
where
I: IntoIterator<Item = u32> + Clone;
I: IntoIterator + Clone,
I::Item: Borrow<u32>;
/// Batch version of [`estimate_fee`](#method.estimate_fee).
///
@ -184,7 +191,8 @@ pub trait ElectrumApi {
/// **Satoshis per kilobyte** to confirm a transaction in the given number of blocks.
fn batch_estimate_fee<I>(&self, numbers: I) -> Result<Vec<f64>, Error>
where
I: IntoIterator<Item = usize> + Clone;
I: IntoIterator + Clone,
I::Item: Borrow<usize>;
/// Broadcasts the raw bytes of a transaction to the network.
fn transaction_broadcast_raw(&self, raw_tx: &[u8]) -> Result<Txid, Error>;

View File

@ -46,8 +46,8 @@ impl Batch {
}
/// Add one `blockchain.scripthash.listunspent` request to the batch queue
pub fn script_subscribe<'a, B: Borrow<&'a Script>>(&mut self, script: B) {
let params = vec![Param::String(script.borrow().to_electrum_scripthash().to_hex())];
pub fn script_subscribe(&mut self, script: &Script) {
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
self.calls
.push((String::from("blockchain.scripthash.subscribe"), params));
}

View File

@ -1,6 +1,6 @@
//! Electrum Client
use std::{sync::RwLock, borrow::Borrow};
use std::{borrow::Borrow, sync::RwLock};
use log::{info, warn};
@ -249,7 +249,8 @@ impl ElectrumApi for Client {
#[inline]
fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result<Vec<GetBalanceRes>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>,
{
impl_inner_call!(self, batch_script_get_balance, scripts.clone())
}
@ -262,7 +263,8 @@ impl ElectrumApi for Client {
#[inline]
fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result<Vec<Vec<GetHistoryRes>>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>,
{
impl_inner_call!(self, batch_script_get_history, scripts.clone())
}
@ -278,7 +280,8 @@ impl ElectrumApi for Client {
scripts: I,
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>,
{
impl_inner_call!(self, batch_script_list_unspent, scripts.clone())
}
@ -291,7 +294,8 @@ impl ElectrumApi for Client {
#[inline]
fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result<Vec<Vec<u8>>, Error>
where
I: IntoIterator<Item = &'t Txid> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'t Txid>,
{
impl_inner_call!(self, batch_transaction_get_raw, txids.clone())
}
@ -299,7 +303,8 @@ impl ElectrumApi for Client {
#[inline]
fn batch_block_header_raw<'s, I>(&self, heights: I) -> Result<Vec<Vec<u8>>, Error>
where
I: IntoIterator<Item = u32> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<u32>,
{
impl_inner_call!(self, batch_block_header_raw, heights.clone())
}
@ -307,7 +312,8 @@ impl ElectrumApi for Client {
#[inline]
fn batch_estimate_fee<'s, I>(&self, numbers: I) -> Result<Vec<f64>, Error>
where
I: IntoIterator<Item = usize> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<usize>,
{
impl_inner_call!(self, batch_estimate_fee, numbers.clone())
}

View File

@ -40,9 +40,17 @@ use types::*;
macro_rules! impl_batch_call {
( $self:expr, $data:expr, $call:ident ) => {{
impl_batch_call!($self, $data, $call, )
}};
( $self:expr, $data:expr, $call:ident, apply_deref ) => {{
impl_batch_call!($self, $data, $call, *)
}};
( $self:expr, $data:expr, $call:ident, $($apply_deref:tt)? ) => {{
let mut batch = Batch::default();
for i in $data {
batch.$call(i);
batch.$call($($apply_deref)* i.borrow());
}
let resp = $self.batch_call(&batch)?;
@ -911,7 +919,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
}
fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result<Vec<GetBalanceRes>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>,
{
impl_batch_call!(self, scripts, script_get_balance)
}
@ -929,7 +938,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
}
fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result<Vec<Vec<GetHistoryRes>>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>,
{
impl_batch_call!(self, scripts, script_get_history)
}
@ -957,7 +967,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
scripts: I,
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
where
I: IntoIterator<Item = &'s Script> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'s Script>,
{
impl_batch_call!(self, scripts, script_list_unspent)
}
@ -980,7 +991,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result<Vec<Vec<u8>>, Error>
where
I: IntoIterator<Item = &'t Txid> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<&'t Txid>,
{
let txs_string: Result<Vec<String>, Error> = impl_batch_call!(self, txids, transaction_get);
txs_string?
@ -991,10 +1003,11 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
fn batch_block_header_raw<'s, I>(&self, heights: I) -> Result<Vec<Vec<u8>>, Error>
where
I: IntoIterator<Item = u32> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<u32>,
{
let headers_string: Result<Vec<String>, Error> =
impl_batch_call!(self, heights, block_header);
impl_batch_call!(self, heights, block_header, apply_deref);
headers_string?
.iter()
.map(|s| Ok(Vec::<u8>::from_hex(s)?))
@ -1003,9 +1016,10 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
fn batch_estimate_fee<'s, I>(&self, numbers: I) -> Result<Vec<f64>, Error>
where
I: IntoIterator<Item = usize> + Clone,
I: IntoIterator + Clone,
I::Item: Borrow<usize>,
{
impl_batch_call!(self, numbers, estimate_fee)
impl_batch_call!(self, numbers, estimate_fee, apply_deref)
}
fn transaction_broadcast_raw(&self, raw_tx: &[u8]) -> Result<Txid, Error> {