From c3be60565b42c5ff6103b692d0aea7ea9e28ce50 Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Sun, 24 Jan 2016 16:35:20 -0800 Subject: [PATCH 1/2] Add an umbrella impl for Service for types that Deref to Service. This means clients can only impl Service for types they define themselves, e.g. no longer for () --- tarpc/src/lib.rs | 9 ++++++--- tarpc/src/macros.rs | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tarpc/src/lib.rs b/tarpc/src/lib.rs index 3de785b..9822ecf 100644 --- a/tarpc/src/lib.rs +++ b/tarpc/src/lib.rs @@ -19,7 +19,8 @@ //! use self::my_server::*; //! use std::time::Duration; //! -//! impl my_server::Service for () { +//! struct Server; +//! impl my_server::Service for Server { //! fn hello(&self, s: String) -> String { //! format!("Hello, {}!", s) //! } @@ -30,8 +31,10 @@ //! //! fn main() { //! let addr = "127.0.0.1:9000"; -//! let shutdown = my_server::serve(addr, (), -//! Some(Duration::from_secs(30))).unwrap(); +//! let shutdown = my_server::serve(addr, +//! Server, +//! Some(Duration::from_secs(30))) +//! .unwrap(); //! let client = Client::new(addr, None).unwrap(); //! assert_eq!(3, client.add(1, 2).unwrap()); //! assert_eq!("Hello, Mom!".to_string(), diff --git a/tarpc/src/macros.rs b/tarpc/src/macros.rs index 4670030..18070a5 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -190,6 +190,17 @@ macro_rules! rpc { )* } + impl

Service for P + where P: Send + Sync + ::std::ops::Deref + { + $( + $(#[$attr])* + fn $fn_name(&self, $($arg:$in_),*) -> $out { + Service::$fn_name(&**self, $($arg),*) + } + )* + } + define_request!($($fn_name($($in_),*))*); #[allow(non_camel_case_types)] @@ -283,7 +294,8 @@ mod test { use self::my_server::*; - impl Service for () { + struct Server; + impl Service for Server { fn hello(&self, s: Foo) -> Foo { Foo { message: format!("Hello, {}", &s.message) } } @@ -297,7 +309,7 @@ mod test { fn simple_test() { println!("Starting"); let addr = "127.0.0.1:9000"; - let shutdown = my_server::serve(addr, (), test_timeout()).unwrap(); + let shutdown = my_server::serve(addr, Server, test_timeout()).unwrap(); let client = Client::new(addr, None).unwrap(); assert_eq!(3, client.add(1, 2).unwrap()); let foo = Foo { message: "Adam".into() }; From 68445824af9ce70542fc084fae2e82bd58dd564f Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Sun, 24 Jan 2016 17:17:59 -0800 Subject: [PATCH 2/2] Add a test, and change umbrella impl... ...to be for Deref where S: Service instead of Deref. --- tarpc/src/macros.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tarpc/src/macros.rs b/tarpc/src/macros.rs index 11a405f..b17b4fd 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -198,8 +198,9 @@ macro_rules! rpc { )* } - impl

Service for P - where P: Send + Sync + ::std::ops::Deref + impl Service for P + where P: Send + Sync + ::std::ops::Deref, + S: Service { $( $(#[$attr])* @@ -313,6 +314,15 @@ mod test { } } + #[test] + fn serve_arc_server() { + serve("localhost:0", + ::std::sync::Arc::new(Server), + None) + .unwrap() + .shutdown(); + } + #[test] fn simple_test() { println!("Starting");