Skip to content

Commit 2694c22

Browse files
committed
Generalize the interface
1 parent 3ef1a6d commit 2694c22

File tree

2 files changed

+47
-58
lines changed

2 files changed

+47
-58
lines changed

src/client.rs

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ use rustls::{ClientConfig, ClientSession, StreamOwned};
2828
#[cfg(any(feature = "default", feature = "proxy"))]
2929
use socks::{Socks5Stream, ToTargetAddr};
3030

31-
#[cfg(any(
32-
feature = "default",
33-
feature = "use-rustls",
34-
feature = "use-openssl",
35-
feature = "proxy"
36-
))]
3731
use stream::ClonableStream;
3832

3933
use batch::Batch;
@@ -88,6 +82,25 @@ where
8882
calls: usize,
8983
}
9084

85+
impl<S> From<S> for Client<ClonableStream<S>>
86+
where
87+
S: Read + Write,
88+
{
89+
fn from(stream: S) -> Self {
90+
let stream: ClonableStream<_> = stream.into();
91+
92+
Self {
93+
buf_reader: BufReader::new(stream.clone()),
94+
stream,
95+
headers: VecDeque::new(),
96+
script_notifications: BTreeMap::new(),
97+
98+
#[cfg(feature = "debug-calls")]
99+
calls: 0,
100+
}
101+
}
102+
}
103+
91104
/// Transport type used to establish a plaintext TCP connection with the server
92105
pub type ElectrumPlaintextStream = TcpStream;
93106
impl Client<ElectrumPlaintextStream> {
@@ -128,19 +141,8 @@ impl Client<ElectrumSslStream> {
128141
let stream = connector
129142
.connect(domain.unwrap_or("not.validated"), stream)
130143
.map_err(Error::SslHandshakeError)?;
131-
let stream: ClonableStream<_> = stream.into();
132-
133-
let buf_reader = BufReader::new(stream.clone());
134144

135-
Ok(Self {
136-
stream,
137-
buf_reader,
138-
headers: VecDeque::new(),
139-
script_notifications: BTreeMap::new(),
140-
141-
#[cfg(feature = "debug-calls")]
142-
calls: 0,
143-
})
145+
Ok(stream.into())
144146
}
145147
}
146148

@@ -200,19 +202,8 @@ impl Client<ElectrumSslStream> {
200202
.map_err(|_| Error::InvalidDNSNameError(domain.unwrap_or("<NONE>").to_string()))?,
201203
);
202204
let stream = StreamOwned::new(session, tcp_stream);
203-
let stream: ClonableStream<_> = stream.into();
204-
205-
let buf_reader = BufReader::new(stream.clone());
206205

207-
Ok(Self {
208-
stream,
209-
buf_reader,
210-
headers: VecDeque::new(),
211-
script_notifications: BTreeMap::new(),
212-
213-
#[cfg(feature = "debug-calls")]
214-
calls: 0,
215-
})
206+
Ok(stream.into())
216207
}
217208
}
218209

@@ -230,19 +221,8 @@ impl Client<ElectrumProxyStream> {
230221
) -> Result<Self, Error> {
231222
// TODO: support proxy credentials
232223
let stream = Socks5Stream::connect(proxy_addr, target_addr)?;
233-
let stream: ClonableStream<_> = stream.into();
234-
235-
let buf_reader = BufReader::new(stream.clone());
236-
237-
Ok(Self {
238-
stream,
239-
buf_reader,
240-
headers: VecDeque::new(),
241-
script_notifications: BTreeMap::new(),
242224

243-
#[cfg(feature = "debug-calls")]
244-
calls: 0,
245-
})
225+
Ok(stream.into())
246226
}
247227
}
248228

@@ -541,10 +521,13 @@ impl<S: Read + Write> Client<S> {
541521
/// Batch version of [`script_get_balance`](#method.script_get_balance).
542522
///
543523
/// Takes a list of scripts and returns a list of balance responses.
544-
pub fn batch_script_get_balance(
524+
pub fn batch_script_get_balance<'s, I>(
545525
&mut self,
546-
scripts: Vec<&Script>,
547-
) -> Result<Vec<GetBalanceRes>, Error> {
526+
scripts: I,
527+
) -> Result<Vec<GetBalanceRes>, Error>
528+
where
529+
I: IntoIterator<Item = &'s Script>,
530+
{
548531
impl_batch_call!(self, scripts, script_get_balance)
549532
}
550533

@@ -559,10 +542,13 @@ impl<S: Read + Write> Client<S> {
559542
/// Batch version of [`script_get_history`](#method.script_get_history).
560543
///
561544
/// Takes a list of scripts and returns a list of history responses.
562-
pub fn batch_script_get_history(
545+
pub fn batch_script_get_history<'s, I>(
563546
&mut self,
564-
scripts: Vec<&Script>,
565-
) -> Result<Vec<Vec<GetHistoryRes>>, Error> {
547+
scripts: I,
548+
) -> Result<Vec<Vec<GetHistoryRes>>, Error>
549+
where
550+
I: IntoIterator<Item = &'s Script>,
551+
{
566552
impl_batch_call!(self, scripts, script_get_history)
567553
}
568554

@@ -577,10 +563,13 @@ impl<S: Read + Write> Client<S> {
577563
/// Batch version of [`script_list_unspent`](#method.script_list_unspent).
578564
///
579565
/// Takes a list of scripts and returns a list of a list of utxos.
580-
pub fn batch_script_list_unspent(
566+
pub fn batch_script_list_unspent<'s, I>(
581567
&mut self,
582-
scripts: Vec<&Script>,
583-
) -> Result<Vec<Vec<ListUnspentRes>>, Error> {
568+
scripts: I,
569+
) -> Result<Vec<Vec<ListUnspentRes>>, Error>
570+
where
571+
I: IntoIterator<Item = &'s Script>,
572+
{
584573
impl_batch_call!(self, scripts, script_list_unspent)
585574
}
586575

@@ -599,7 +588,13 @@ impl<S: Read + Write> Client<S> {
599588
/// Batch version of [`transaction_get`](#method.transaction_get).
600589
///
601590
/// Takes a list of `txids` and returns a list of transactions.
602-
pub fn batch_transaction_get(&mut self, txids: Vec<&Txid>) -> Result<Vec<Transaction>, Error> {
591+
pub fn batch_transaction_get<'t, I>(
592+
&mut self,
593+
txids: Vec<&Txid>,
594+
) -> Result<Vec<Transaction>, Error>
595+
where
596+
I: IntoIterator<Item = &'t Txid>,
597+
{
603598
impl_batch_call!(self, txids, transaction_get)
604599
}
605600

src/lib.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ extern crate webpki_roots;
3939

4040
pub mod batch;
4141
pub mod client;
42-
#[cfg(any(
43-
feature = "default",
44-
feature = "use-rustls",
45-
feature = "use-openssl",
46-
feature = "proxy"
47-
))]
4842
mod stream;
4943
#[cfg(test)]
5044
mod test_stream;

0 commit comments

Comments
 (0)