Skip to content

Commit 40cecd5

Browse files
committed
Use Borrow for all items in IntoIterator
1 parent bf2f8ef commit 40cecd5

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

src/api.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ pub trait ElectrumApi {
3939
/// Takes a list of `txids` and returns a list of transactions.
4040
fn batch_transaction_get<'t, I>(&self, txids: I) -> Result<Vec<Transaction>, Error>
4141
where
42-
I: IntoIterator<Item = &'t Txid> + Clone,
42+
I: IntoIterator + Clone,
43+
I::Item: Borrow<&'t Txid>,
4344
{
4445
self.batch_transaction_get_raw(txids)?
4546
.iter()
@@ -52,7 +53,8 @@ pub trait ElectrumApi {
5253
/// Takes a list of `heights` of blocks and returns a list of headers.
5354
fn batch_block_header<I>(&self, heights: I) -> Result<Vec<BlockHeader>, Error>
5455
where
55-
I: IntoIterator<Item = u32> + Clone,
56+
I: IntoIterator + Clone,
57+
I::Item: Borrow<u32>,
5658
{
5759
self.batch_block_header_raw(heights)?
5860
.iter()
@@ -110,7 +112,7 @@ pub trait ElectrumApi {
110112
/// Batch version of [`script_subscribe`](#method.script_subscribe).
111113
///
112114
/// Takes a list of scripts and returns a list of script status responses.
113-
///
115+
///
114116
/// Note you should pass a reference to a collection because otherwise an expensive clone is made
115117
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
116118
where
@@ -136,7 +138,8 @@ pub trait ElectrumApi {
136138
/// Takes a list of scripts and returns a list of balance responses.
137139
fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result<Vec<GetBalanceRes>, Error>
138140
where
139-
I: IntoIterator<Item = &'s Script> + Clone;
141+
I: IntoIterator + Clone,
142+
I::Item: Borrow<&'s Script>;
140143

141144
/// Returns the history for a *scriptPubKey*
142145
fn script_get_history(&self, script: &Script) -> Result<Vec<GetHistoryRes>, Error>;
@@ -146,7 +149,8 @@ pub trait ElectrumApi {
146149
/// Takes a list of scripts and returns a list of history responses.
147150
fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result<Vec<Vec<GetHistoryRes>>, Error>
148151
where
149-
I: IntoIterator<Item = &'s Script> + Clone;
152+
I: IntoIterator + Clone,
153+
I::Item: Borrow<&'s Script>;
150154

151155
/// Returns the list of unspent outputs for a *scriptPubKey*
152156
fn script_list_unspent(&self, script: &Script) -> Result<Vec<ListUnspentRes>, Error>;
@@ -159,7 +163,8 @@ pub trait ElectrumApi {
159163
scripts: I,
160164
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
161165
where
162-
I: IntoIterator<Item = &'s Script> + Clone;
166+
I: IntoIterator + Clone,
167+
I::Item: Borrow<&'s Script>;
163168

164169
/// Gets the raw bytes of a transaction with `txid`. Returns an error if not found.
165170
fn transaction_get_raw(&self, txid: &Txid) -> Result<Vec<u8>, Error>;
@@ -169,22 +174,25 @@ pub trait ElectrumApi {
169174
/// Takes a list of `txids` and returns a list of transactions raw bytes.
170175
fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result<Vec<Vec<u8>>, Error>
171176
where
172-
I: IntoIterator<Item = &'t Txid> + Clone;
177+
I: IntoIterator + Clone,
178+
I::Item: Borrow<&'t Txid>;
173179

174180
/// Batch version of [`block_header_raw`](#method.block_header_raw).
175181
///
176182
/// Takes a list of `heights` of blocks and returns a list of block header raw bytes.
177183
fn batch_block_header_raw<I>(&self, heights: I) -> Result<Vec<Vec<u8>>, Error>
178184
where
179-
I: IntoIterator<Item = u32> + Clone;
185+
I: IntoIterator + Clone,
186+
I::Item: Borrow<u32>;
180187

181188
/// Batch version of [`estimate_fee`](#method.estimate_fee).
182189
///
183190
/// Takes a list of `numbers` of blocks and returns a list of fee required in
184191
/// **Satoshis per kilobyte** to confirm a transaction in the given number of blocks.
185192
fn batch_estimate_fee<I>(&self, numbers: I) -> Result<Vec<f64>, Error>
186193
where
187-
I: IntoIterator<Item = usize> + Clone;
194+
I: IntoIterator + Clone,
195+
I::Item: Borrow<usize>;
188196

189197
/// Broadcasts the raw bytes of a transaction to the network.
190198
fn transaction_broadcast_raw(&self, raw_tx: &[u8]) -> Result<Txid, Error>;

src/batch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl Batch {
4646
}
4747

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

src/client.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Electrum Client
22
3-
use std::{sync::RwLock, borrow::Borrow};
3+
use std::{borrow::Borrow, sync::RwLock};
44

55
use log::{info, warn};
66

@@ -249,7 +249,8 @@ impl ElectrumApi for Client {
249249
#[inline]
250250
fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result<Vec<GetBalanceRes>, Error>
251251
where
252-
I: IntoIterator<Item = &'s Script> + Clone,
252+
I: IntoIterator + Clone,
253+
I::Item: Borrow<&'s Script>,
253254
{
254255
impl_inner_call!(self, batch_script_get_balance, scripts.clone())
255256
}
@@ -262,7 +263,8 @@ impl ElectrumApi for Client {
262263
#[inline]
263264
fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result<Vec<Vec<GetHistoryRes>>, Error>
264265
where
265-
I: IntoIterator<Item = &'s Script> + Clone,
266+
I: IntoIterator + Clone,
267+
I::Item: Borrow<&'s Script>,
266268
{
267269
impl_inner_call!(self, batch_script_get_history, scripts.clone())
268270
}
@@ -278,7 +280,8 @@ impl ElectrumApi for Client {
278280
scripts: I,
279281
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
280282
where
281-
I: IntoIterator<Item = &'s Script> + Clone,
283+
I: IntoIterator + Clone,
284+
I::Item: Borrow<&'s Script>,
282285
{
283286
impl_inner_call!(self, batch_script_list_unspent, scripts.clone())
284287
}
@@ -291,23 +294,26 @@ impl ElectrumApi for Client {
291294
#[inline]
292295
fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result<Vec<Vec<u8>>, Error>
293296
where
294-
I: IntoIterator<Item = &'t Txid> + Clone,
297+
I: IntoIterator + Clone,
298+
I::Item: Borrow<&'t Txid>,
295299
{
296300
impl_inner_call!(self, batch_transaction_get_raw, txids.clone())
297301
}
298302

299303
#[inline]
300304
fn batch_block_header_raw<'s, I>(&self, heights: I) -> Result<Vec<Vec<u8>>, Error>
301305
where
302-
I: IntoIterator<Item = u32> + Clone,
306+
I: IntoIterator + Clone,
307+
I::Item: Borrow<u32>,
303308
{
304309
impl_inner_call!(self, batch_block_header_raw, heights.clone())
305310
}
306311

307312
#[inline]
308313
fn batch_estimate_fee<'s, I>(&self, numbers: I) -> Result<Vec<f64>, Error>
309314
where
310-
I: IntoIterator<Item = usize> + Clone,
315+
I: IntoIterator + Clone,
316+
I::Item: Borrow<usize>,
311317
{
312318
impl_inner_call!(self, batch_estimate_fee, numbers.clone())
313319
}

src/raw_client.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ use types::*;
4040

4141
macro_rules! impl_batch_call {
4242
( $self:expr, $data:expr, $call:ident ) => {{
43+
impl_batch_call!($self, $data, $call, )
44+
}};
45+
46+
( $self:expr, $data:expr, $call:ident, apply_deref ) => {{
47+
impl_batch_call!($self, $data, $call, *)
48+
}};
49+
50+
( $self:expr, $data:expr, $call:ident, $($apply_deref:tt)? ) => {{
4351
let mut batch = Batch::default();
4452
for i in $data {
45-
batch.$call(i);
53+
batch.$call($($apply_deref)* i.borrow());
4654
}
4755

4856
let resp = $self.batch_call(&batch)?;
@@ -911,7 +919,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
911919
}
912920
fn batch_script_get_balance<'s, I>(&self, scripts: I) -> Result<Vec<GetBalanceRes>, Error>
913921
where
914-
I: IntoIterator<Item = &'s Script> + Clone,
922+
I: IntoIterator + Clone,
923+
I::Item: Borrow<&'s Script>,
915924
{
916925
impl_batch_call!(self, scripts, script_get_balance)
917926
}
@@ -929,7 +938,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
929938
}
930939
fn batch_script_get_history<'s, I>(&self, scripts: I) -> Result<Vec<Vec<GetHistoryRes>>, Error>
931940
where
932-
I: IntoIterator<Item = &'s Script> + Clone,
941+
I: IntoIterator + Clone,
942+
I::Item: Borrow<&'s Script>,
933943
{
934944
impl_batch_call!(self, scripts, script_get_history)
935945
}
@@ -957,7 +967,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
957967
scripts: I,
958968
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
959969
where
960-
I: IntoIterator<Item = &'s Script> + Clone,
970+
I: IntoIterator + Clone,
971+
I::Item: Borrow<&'s Script>,
961972
{
962973
impl_batch_call!(self, scripts, script_list_unspent)
963974
}
@@ -980,7 +991,8 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
980991

981992
fn batch_transaction_get_raw<'t, I>(&self, txids: I) -> Result<Vec<Vec<u8>>, Error>
982993
where
983-
I: IntoIterator<Item = &'t Txid> + Clone,
994+
I: IntoIterator + Clone,
995+
I::Item: Borrow<&'t Txid>,
984996
{
985997
let txs_string: Result<Vec<String>, Error> = impl_batch_call!(self, txids, transaction_get);
986998
txs_string?
@@ -991,10 +1003,11 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
9911003

9921004
fn batch_block_header_raw<'s, I>(&self, heights: I) -> Result<Vec<Vec<u8>>, Error>
9931005
where
994-
I: IntoIterator<Item = u32> + Clone,
1006+
I: IntoIterator + Clone,
1007+
I::Item: Borrow<u32>,
9951008
{
9961009
let headers_string: Result<Vec<String>, Error> =
997-
impl_batch_call!(self, heights, block_header);
1010+
impl_batch_call!(self, heights, block_header, apply_deref);
9981011
headers_string?
9991012
.iter()
10001013
.map(|s| Ok(Vec::<u8>::from_hex(s)?))
@@ -1003,9 +1016,10 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
10031016

10041017
fn batch_estimate_fee<'s, I>(&self, numbers: I) -> Result<Vec<f64>, Error>
10051018
where
1006-
I: IntoIterator<Item = usize> + Clone,
1019+
I: IntoIterator + Clone,
1020+
I::Item: Borrow<usize>,
10071021
{
1008-
impl_batch_call!(self, numbers, estimate_fee)
1022+
impl_batch_call!(self, numbers, estimate_fee, apply_deref)
10091023
}
10101024

10111025
fn transaction_broadcast_raw(&self, raw_tx: &[u8]) -> Result<Txid, Error> {

0 commit comments

Comments
 (0)