diff --git a/tarpc/src/lib.rs b/tarpc/src/lib.rs index 04d177f..46809fa 100644 --- a/tarpc/src/lib.rs +++ b/tarpc/src/lib.rs @@ -27,7 +27,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) //! } @@ -38,8 +39,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 8a4dd0c..b17b4fd 100644 --- a/tarpc/src/macros.rs +++ b/tarpc/src/macros.rs @@ -198,6 +198,18 @@ macro_rules! rpc { )* } + impl Service for P + where P: Send + Sync + ::std::ops::Deref, + S: Service + { + $( + $(#[$attr])* + fn $fn_name(&self, $($arg:$in_),*) -> $out { + Service::$fn_name(&**self, $($arg),*) + } + )* + } + define_request!($($fn_name($($in_),*))*); #[allow(non_camel_case_types)] @@ -291,7 +303,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) } } @@ -301,11 +314,20 @@ mod test { } } + #[test] + fn serve_arc_server() { + serve("localhost:0", + ::std::sync::Arc::new(Server), + None) + .unwrap() + .shutdown(); + } + #[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() };