Skip to content

Commit fc8ba9d

Browse files
committed
Use idiomatic rust for the raw_call method
1 parent cbda7a2 commit fc8ba9d

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/api.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ pub trait ElectrumApi {
6666
}
6767

6868
/// Executes the requested API call returning the raw answer.
69-
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error>;
69+
fn raw_call(
70+
&self,
71+
method_name: &str,
72+
params: impl IntoIterator<Item = Param>,
73+
) -> Result<serde_json::Value, Error>;
7074

7175
/// Execute a queue of calls stored in a [`Batch`](../batch/struct.Batch.html) struct. Returns
7276
/// `Ok()` **only if** all of the calls are successful. The order of the JSON `Value`s returned

src/client.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,18 @@ impl Client {
168168

169169
impl ElectrumApi for Client {
170170
#[inline]
171-
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
172-
impl_inner_call!(self, raw_call, call)
171+
fn raw_call(
172+
&self,
173+
method_name: &str,
174+
params: impl IntoIterator<Item = Param>,
175+
) -> Result<serde_json::Value, Error> {
176+
// We can't passthrough this method to the inner client because it would require the
177+
// `params` argument to also be `Copy` (because it's used multiple times for multiple
178+
// retries). To avoid adding this extra trait bound we instead re-direct this call to the internal
179+
// `RawClient::internal_raw_call_with_vec` method.
180+
181+
let vec = params.into_iter().collect::<Vec<Param>>();
182+
impl_inner_call!(self, internal_raw_call_with_vec, method_name, vec.clone());
173183
}
174184

175185
#[inline]

src/raw_client.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! This module contains the definition of the raw client that wraps the transport method
44
55
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
6-
use std::convert::TryFrom;
76
use std::io::{BufRead, BufReader, Read, Write};
87
use std::mem::drop;
98
use std::net::{TcpStream, ToSocketAddrs};
@@ -348,6 +347,8 @@ impl RawClient<ElectrumSslStream> {
348347
validate_domain: bool,
349348
tcp_stream: TcpStream,
350349
) -> Result<Self, Error> {
350+
use std::convert::TryFrom;
351+
351352
let builder = ClientConfig::builder().with_safe_defaults();
352353

353354
let config = if validate_domain {
@@ -658,6 +659,21 @@ impl<S: Read + Write> RawClient<S> {
658659
Ok(())
659660
}
660661

662+
pub(crate) fn internal_raw_call_with_vec(
663+
&self,
664+
method_name: &str,
665+
params: Vec<Param>,
666+
) -> Result<serde_json::Value, Error> {
667+
let req = Request::new_id(
668+
self.last_id.fetch_add(1, Ordering::SeqCst),
669+
&method_name,
670+
params,
671+
);
672+
let result = self.call(req)?;
673+
674+
Ok(result)
675+
}
676+
661677
#[inline]
662678
#[cfg(feature = "debug-calls")]
663679
fn increment_calls(&self) {
@@ -670,15 +686,12 @@ impl<S: Read + Write> RawClient<S> {
670686
}
671687

672688
impl<T: Read + Write> ElectrumApi for RawClient<T> {
673-
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
674-
let req = Request::new_id(
675-
self.last_id.fetch_add(1, Ordering::SeqCst),
676-
&call.0,
677-
call.1.to_vec(),
678-
);
679-
let result = self.call(req)?;
680-
681-
Ok(result)
689+
fn raw_call(
690+
&self,
691+
method_name: &str,
692+
params: impl IntoIterator<Item = Param>,
693+
) -> Result<serde_json::Value, Error> {
694+
self.internal_raw_call_with_vec(method_name, params.into_iter().collect())
682695
}
683696

684697
fn batch_call(&self, batch: &Batch) -> Result<Vec<serde_json::Value>, Error> {
@@ -1312,9 +1325,10 @@ mod test {
13121325
),
13131326
Param::Bool(false),
13141327
];
1315-
let call = ("blockchain.transaction.get".to_string(), params);
13161328

1317-
let resp = client.raw_call(&call).unwrap();
1329+
let resp = client
1330+
.raw_call("blockchain.transaction.get", params)
1331+
.unwrap();
13181332

13191333
assert_eq!(
13201334
resp,

0 commit comments

Comments
 (0)