diff --git a/src/api.rs b/src/api.rs index 1ab7240..32d076f 100644 --- a/src/api.rs +++ b/src/api.rs @@ -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, Error> where - I: IntoIterator + 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(&self, heights: I) -> Result, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow, { 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>, 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, Error> where - I: IntoIterator + Clone; + I: IntoIterator + Clone, + I::Item: Borrow<&'s Script>; /// Returns the history for a *scriptPubKey* fn script_get_history(&self, script: &Script) -> Result, 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>, Error> where - I: IntoIterator + 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, Error>; @@ -159,7 +163,8 @@ pub trait ElectrumApi { scripts: I, ) -> Result>, Error> where - I: IntoIterator + 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, 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>, Error> where - I: IntoIterator + 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(&self, heights: I) -> Result>, Error> where - I: IntoIterator + Clone; + I: IntoIterator + Clone, + I::Item: Borrow; /// 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(&self, numbers: I) -> Result, Error> where - I: IntoIterator + Clone; + I: IntoIterator + Clone, + I::Item: Borrow; /// Broadcasts the raw bytes of a transaction to the network. fn transaction_broadcast_raw(&self, raw_tx: &[u8]) -> Result; diff --git a/src/batch.rs b/src/batch.rs index 424b981..9ec3694 100644 --- a/src/batch.rs +++ b/src/batch.rs @@ -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)); } diff --git a/src/client.rs b/src/client.rs index 84732d3..2ec9992 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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, Error> where - I: IntoIterator + 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>, Error> where - I: IntoIterator + 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>, Error> where - I: IntoIterator + 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>, Error> where - I: IntoIterator + 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>, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow, { 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, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow, { impl_inner_call!(self, batch_estimate_fee, numbers.clone()) } diff --git a/src/raw_client.rs b/src/raw_client.rs index 6c82f5f..a4a23df 100644 --- a/src/raw_client.rs +++ b/src/raw_client.rs @@ -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 ElectrumApi for RawClient { } fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow<&'s Script>, { impl_batch_call!(self, scripts, script_get_balance) } @@ -929,7 +938,8 @@ impl ElectrumApi for RawClient { } fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result>, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow<&'s Script>, { impl_batch_call!(self, scripts, script_get_history) } @@ -957,7 +967,8 @@ impl ElectrumApi for RawClient { scripts: I, ) -> Result>, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow<&'s Script>, { impl_batch_call!(self, scripts, script_list_unspent) } @@ -980,7 +991,8 @@ impl ElectrumApi for RawClient { fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result>, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow<&'t Txid>, { let txs_string: Result, Error> = impl_batch_call!(self, txids, transaction_get); txs_string? @@ -991,10 +1003,11 @@ impl ElectrumApi for RawClient { fn batch_block_header_raw<'s, I>(&self, heights: I) -> Result>, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow, { let headers_string: Result, Error> = - impl_batch_call!(self, heights, block_header); + impl_batch_call!(self, heights, block_header, apply_deref); headers_string? .iter() .map(|s| Ok(Vec::::from_hex(s)?)) @@ -1003,9 +1016,10 @@ impl ElectrumApi for RawClient { fn batch_estimate_fee<'s, I>(&self, numbers: I) -> Result, Error> where - I: IntoIterator + Clone, + I: IntoIterator + Clone, + I::Item: Borrow, { - 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 {