Skip to content

Commit d711053

Browse files
committed
Merge branch 'dont-clone-transport'
2 parents 6cced4a + 0684981 commit d711053

File tree

3 files changed

+33
-40
lines changed

3 files changed

+33
-40
lines changed

core/src/lib.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<T> Future for RpcRequest<T> {
126126

127127
/// Trait for types acting as a transport layer for the JSON-RPC 2.0 clients generated by the
128128
/// `jsonrpc_client` macro.
129-
pub trait Transport: Clone + Send + 'static {
129+
pub trait Transport {
130130
/// The type of error that this transport emits if it fails.
131131
type Error: ::std::error::Error + Send + 'static;
132132

@@ -147,7 +147,7 @@ pub trait Transport: Clone + Send + 'static {
147147
/// # Not intended for direct use
148148
/// This is being called from the client structs generated by the `jsonrpc_client` macro. This
149149
/// function is not intended to be used directly, only the generated structs should call this.
150-
pub fn call_method<T, P, R>(mut transport: T, method: String, params: P) -> RpcRequest<R>
150+
pub fn call_method<T, P, R>(transport: &mut T, method: String, params: P) -> RpcRequest<R>
151151
where
152152
T: Transport,
153153
P: serde::Serialize,
@@ -160,31 +160,30 @@ where
160160
method,
161161
raw_id
162162
);
163-
let request = serialize_request(id.clone(), method.clone(), params)
163+
let request_serialization_result = serialize_request(id.clone(), method.clone(), params)
164164
.chain_err(|| ErrorKind::SerializeError);
165-
let method_copy1 = method.clone();
166-
let method_copy2 = method.clone();
167-
168-
let future = futures::future::result(request)
169-
.and_then(move |request_raw| {
165+
match request_serialization_result {
166+
Err(e) => RpcRequest(Box::new(futures::future::err(e))),
167+
Ok(request_raw) => {
170168
trace!(
171169
"Sending call to method \"{}\" with id {} to transport",
172-
method_copy1,
170+
method,
173171
raw_id
174172
);
175-
transport
173+
let future = transport
176174
.send(request_raw)
177175
.map_err(|e| Error::with_chain(e, ErrorKind::TransportError))
178-
})
179-
.and_then(move |response_raw: Vec<u8>| {
180-
trace!(
181-
"Deserializing response to method \"{}\" with id {}",
182-
method_copy2,
183-
raw_id
184-
);
185-
response::parse::<R>(&response_raw, id)
186-
});
187-
RpcRequest(Box::new(future))
176+
.and_then(move |response_raw: Vec<u8>| {
177+
trace!(
178+
"Deserializing response to method \"{}\" with id {}",
179+
method,
180+
raw_id
181+
);
182+
response::parse::<R>(&response_raw, id)
183+
});
184+
RpcRequest(Box::new(future))
185+
}
186+
}
188187
}
189188

190189

@@ -205,7 +204,7 @@ where
205204
};
206205
let method_call = MethodCall {
207206
jsonrpc: Some(Version::V2),
208-
method: method.to_owned(),
207+
method,
209208
params: serialized_params,
210209
id,
211210
};

core/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ macro_rules! jsonrpc_client {
3636
{
3737
let method = String::from(stringify!($method));
3838
let params = expand_params!($($arg_name,)*);
39-
$crate::call_method($selff.transport.clone(), method, params)
39+
$crate::call_method(&mut $selff.transport, method, params)
4040
}
4141
)*
4242
}

http/src/lib.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,12 @@ extern crate native_tls;
8080

8181
use futures::{future, Future, Stream};
8282
use futures::sync::{mpsc, oneshot};
83-
8483
use hyper::{Client, Request, StatusCode, Uri};
85-
8684
use jsonrpc_client_core::{BoxFuture, Transport};
87-
8885
use std::str::FromStr;
8986
use std::sync::Arc;
9087
use std::sync::atomic::{AtomicUsize, Ordering};
9188
use std::thread;
92-
9389
use tokio_core::reactor::Core;
9490
pub use tokio_core::reactor::Handle;
9591

@@ -221,19 +217,17 @@ impl HttpTransport {
221217

222218
fn standalone_internal<C: ClientCreator>(client_creator: C) -> Result<HttpTransport> {
223219
let (tx, rx) = ::std::sync::mpsc::channel();
224-
thread::spawn(move || {
225-
match create_standalone_core(client_creator) {
226-
Err(e) => {
227-
tx.send(Err(e)).unwrap();
228-
}
229-
Ok((mut core, request_tx, future)) => {
230-
tx.send(Ok(HttpTransport::new_internal(request_tx)))
231-
.unwrap();
232-
if let Err(_) = core.run(future) {
233-
error!("JSON-RPC processing thread had an error");
234-
}
235-
debug!("Standalone HttpTransport thread exiting");
220+
thread::spawn(move || match create_standalone_core(client_creator) {
221+
Err(e) => {
222+
tx.send(Err(e)).unwrap();
223+
}
224+
Ok((mut core, request_tx, future)) => {
225+
tx.send(Ok(HttpTransport::new_internal(request_tx)))
226+
.unwrap();
227+
if let Err(_) = core.run(future) {
228+
error!("JSON-RPC processing thread had an error");
236229
}
230+
debug!("Standalone HttpTransport thread exiting");
237231
}
238232
});
239233

@@ -398,8 +392,8 @@ mod tests {
398392
#[test]
399393
fn failing_client_creator() {
400394
let error = HttpTransport::with_client(|_: &Handle| {
401-
Err(io::Error::new(io::ErrorKind::Other, "Dummy error")) as
402-
::std::result::Result<Client<HttpConnector, hyper::Body>, io::Error>
395+
Err(io::Error::new(io::ErrorKind::Other, "Dummy error"))
396+
as ::std::result::Result<Client<HttpConnector, hyper::Body>, io::Error>
403397
}).unwrap_err();
404398
match error.kind() {
405399
&ErrorKind::ClientCreatorError => (),

0 commit comments

Comments
 (0)