Skip to content

Commit bf2f8ef

Browse files
committed
script_subscribe IntoIterator accepts Borrow<&Script>
So that is callable with `&Vec<Script>` and `Vec<&Script>`
1 parent 156e6fc commit bf2f8ef

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

src/api.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Electrum APIs
22
3+
use std::borrow::Borrow;
34
use std::convert::TryInto;
45

56
use bitcoin::consensus::encode::{deserialize, serialize};
@@ -109,9 +110,12 @@ pub trait ElectrumApi {
109110
/// Batch version of [`script_subscribe`](#method.script_subscribe).
110111
///
111112
/// Takes a list of scripts and returns a list of script status responses.
113+
///
114+
/// Note you should pass a reference to a collection because otherwise an expensive clone is made
112115
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
113116
where
114-
I: IntoIterator<Item = &'s Script> + Clone;
117+
I: IntoIterator + Clone,
118+
I::Item: Borrow<&'s Script>;
115119

116120
/// Subscribes to notifications for activity on a specific *scriptPubKey*.
117121
///

src/batch.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! This module contains definitions and helper functions used when making batch calls.
44
5+
use std::borrow::Borrow;
6+
57
use bitcoin::hashes::hex::ToHex;
68
use bitcoin::{Script, Txid};
79

@@ -44,8 +46,8 @@ impl Batch {
4446
}
4547

4648
/// Add one `blockchain.scripthash.listunspent` request to the batch queue
47-
pub fn script_subscribe(&mut self, script: &Script) {
48-
let params = vec![Param::String(script.to_electrum_scripthash().to_hex())];
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())];
4951
self.calls
5052
.push((String::from("blockchain.scripthash.subscribe"), params));
5153
}

src/client.rs

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

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

@@ -225,7 +225,8 @@ impl ElectrumApi for Client {
225225
#[inline]
226226
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
227227
where
228-
I: IntoIterator<Item = &'s Script> + Clone,
228+
I: IntoIterator + Clone,
229+
I::Item: Borrow<&'s Script>,
229230
{
230231
impl_inner_call!(self, batch_script_subscribe, scripts.clone())
231232
}

src/raw_client.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! This module contains the definition of the raw client that wraps the transport method
44
5+
use std::borrow::Borrow;
56
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
67
use std::io::{BufRead, BufReader, Read, Write};
78
use std::mem::drop;
@@ -850,13 +851,14 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
850851

851852
fn batch_script_subscribe<'s, I>(&self, scripts: I) -> Result<Vec<Option<ScriptStatus>>, Error>
852853
where
853-
I: IntoIterator<Item = &'s Script> + Clone,
854+
I: IntoIterator + Clone,
855+
I::Item: Borrow<&'s Script>,
854856
{
855857
{
856858
let mut script_notifications = self.script_notifications.lock()?;
857859

858-
for script in scripts.clone().into_iter() {
859-
let script_hash = script.to_electrum_scripthash();
860+
for script in scripts.clone() {
861+
let script_hash = script.borrow().to_electrum_scripthash();
860862
if script_notifications.contains_key(&script_hash) {
861863
return Err(Error::AlreadySubscribed(script_hash));
862864
}

0 commit comments

Comments
 (0)