From b8babb57dd69622dc3c71e4678341adf6472ec96 Mon Sep 17 00:00:00 2001 From: Adam Wright Date: Sun, 14 Feb 2016 18:11:04 -0800 Subject: [PATCH] Add TryClone to Client/AsyncClient --- tarpc/src/macros.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tarpc/src/macros.rs b/tarpc/src/macros.rs index 6f62da1..ae441d0 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -265,6 +265,11 @@ macro_rules! service_inner { $fn_name($($arg: $in_),*) -> $out )* ); + + #[doc="Attempt to clone the client object. This might fail if the underlying TcpStream clone fails"] + pub fn try_clone(&self) -> ::std::io::Result { + Ok(Client(try!(self.0.try_clone()))) + } } #[doc="The client stub that makes asynchronous RPC calls to the server."] @@ -286,6 +291,11 @@ macro_rules! service_inner { $fn_name($($arg: $in_),*) -> $out )* ); + + #[doc="Attempt to clone the client object. This might fail if the underlying TcpStream clone fails"] + pub fn try_clone(&self) -> ::std::io::Result { + Ok(AsyncClient(try!(self.0.try_clone()))) + } } struct __Server(S); @@ -386,6 +396,24 @@ mod functional_test { handle.shutdown(); } + #[test] + fn try_clone() { + let handle = serve( "localhost:0", Server, test_timeout()).unwrap(); + let client1 = Client::new(handle.local_addr(), None).unwrap(); + let client2 = client1.try_clone().unwrap(); + assert_eq!(3, client1.add(1, 2).unwrap()); + assert_eq!(3, client2.add(1, 2).unwrap()); + } + + #[test] + fn async_try_clone() { + let handle = serve("localhost:0", Server, test_timeout()).unwrap(); + let client1 = AsyncClient::new(handle.local_addr(), None).unwrap(); + let client2 = client1.try_clone().unwrap(); + assert_eq!(3, client1.add(1, 2).get().unwrap()); + assert_eq!(3, client2.add(1, 2).get().unwrap()); + } + // Tests that a server can be wrapped in an Arc; no need to run, just compile #[allow(dead_code)] fn serve_arc_server() {