Make SyncClient only require &self for RPCs.

This commit is contained in:
Tim Kuehn
2017-01-08 22:13:49 -08:00
parent e8f942f463
commit ffea090726
7 changed files with 14 additions and 13 deletions

View File

@@ -70,7 +70,7 @@ impl SyncService for HelloServer {
fn main() {
let addr = "localhost:10000";
HelloServer.listen(addr).unwrap();
let mut client = SyncClient::connect(addr).unwrap();
let client = SyncClient::connect(addr).unwrap();
println!("{}", client.hello("Mom".to_string()).unwrap());
}
```

View File

@@ -3,7 +3,7 @@
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.
#![feature(conservative_impl_trait, plugin, proc_macro)]
#![feature(conservative_impl_trait, plugin)]
#![plugin(tarpc_plugins)]
extern crate futures;
@@ -50,7 +50,7 @@ impl SyncService for HelloServer {
fn main() {
let addr = HelloServer.listen("localhost:10000").unwrap();
let mut client = SyncClient::connect(addr).unwrap();
let client = SyncClient::connect(addr).unwrap();
println!("{}", client.hello("Mom".to_string()).unwrap());
println!("{}", client.hello("".to_string()).unwrap_err());
}

View File

@@ -3,7 +3,7 @@
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.
#![feature(conservative_impl_trait, plugin, proc_macro)]
#![feature(conservative_impl_trait, plugin)]
#![plugin(tarpc_plugins)]
extern crate bincode;

View File

@@ -3,7 +3,7 @@
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.
// required by `FutureClient` (not used in this example)
// required by `FutureClient` (not used directly in this example)
#![feature(conservative_impl_trait, plugin)]
#![plugin(tarpc_plugins)]

View File

@@ -80,7 +80,7 @@ fn main() {
let double = DoubleServer::new(add_client);
let double_addr = double.listen("localhost:0".first_socket_addr()).wait().unwrap();
let mut double_client = double::SyncClient::connect(&double_addr).unwrap();
let double_client = double::SyncClient::connect(&double_addr).unwrap();
for i in 0..5 {
println!("{:?}", double_client.double(i).unwrap());
}

View File

@@ -53,7 +53,7 @@ const CHUNK_SIZE: u32 = 1 << 19;
fn bench_tarpc(target: u64) {
let addr = Server.listen("localhost:0".first_socket_addr()).wait().unwrap();
let mut client = SyncClient::connect(&addr).unwrap();
let client = SyncClient::connect(&addr).unwrap();
let start = time::Instant::now();
let mut nread = 0;
while nread < target {

View File

@@ -611,7 +611,7 @@ macro_rules! service {
#[allow(unused)]
#[derive(Clone, Debug)]
/// The client stub that makes RPC calls to the server. Exposes a blocking interface.
pub struct SyncClient(FutureClient);
pub struct SyncClient(::std::sync::Arc<::std::sync::Mutex<FutureClient>>);
impl $crate::sync::Connect for SyncClient {
fn connect<A>(addr: A) -> ::std::result::Result<Self, ::std::io::Error>
@@ -619,7 +619,8 @@ macro_rules! service {
{
let addr = $crate::util::FirstSocketAddr::try_first_socket_addr(&addr)?;
let client = <FutureClient as $crate::future::Connect>::connect(&addr);
let client = SyncClient($crate::futures::Future::wait(client)?);
let client = $crate::futures::Future::wait(client)?;
let client = SyncClient(::std::sync::Arc::new(::std::sync::Mutex::new(client)));
::std::result::Result::Ok(client)
}
}
@@ -628,10 +629,10 @@ macro_rules! service {
$(
#[allow(unused)]
$(#[$attr])*
pub fn $fn_name(&mut self, $($arg: $in_),*)
pub fn $fn_name(&self, $($arg: $in_),*)
-> ::std::result::Result<$out, $crate::Error<$error>>
{
let rpc = (self.0).$fn_name($($arg),*);
let rpc = self.0.lock().unwrap().$fn_name($($arg),*);
$crate::futures::Future::wait(rpc)
}
)*
@@ -833,7 +834,7 @@ mod functional_test {
fn simple() {
let _ = env_logger::init();
let addr = Server.listen("localhost:0".first_socket_addr()).unwrap();
let mut client = SyncClient::connect(addr).unwrap();
let client = SyncClient::connect(addr).unwrap();
assert_eq!(3, client.add(1, 2).unwrap());
assert_eq!("Hey, Tim.", client.hey("Tim".to_string()).unwrap());
}
@@ -842,7 +843,7 @@ mod functional_test {
fn other_service() {
let _ = env_logger::init();
let addr = Server.listen("localhost:0".first_socket_addr()).unwrap();
let mut client = super::other_service::SyncClient::connect(addr).expect("Could not connect!");
let client = super::other_service::SyncClient::connect(addr).expect("Could not connect!");
match client.foo().err().unwrap() {
::Error::ServerDeserialize(_) => {} // good
bad => panic!("Expected Error::ServerDeserialize but got {}", bad),