mirror of
https://github.com/OMGeeky/tarpc.git
synced 2026-01-04 10:32:24 +01:00
Allow variable args
This commit is contained in:
@@ -4,7 +4,7 @@ extern crate byteorder;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! rpc {
|
||||
($server:ident: $($fn_name:ident($in_:ty) -> $out:ty;)* ) => {
|
||||
($server:ident: $( $fn_name:ident( $( $arg:ident : $in_:ty ),* ) -> $out:ty;)* ) => {
|
||||
mod $server {
|
||||
use rustc_serialize::json;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
@@ -14,7 +14,7 @@ macro_rules! rpc {
|
||||
|
||||
pub trait Service: Clone + Send {
|
||||
$(
|
||||
fn $fn_name(&self, $in_) -> $out;
|
||||
fn $fn_name(&self, $($arg:$in_),*) -> $out;
|
||||
)*
|
||||
|
||||
fn handle_request(self, mut conn: TcpStream) -> Result<(), io::Error> {
|
||||
@@ -26,8 +26,8 @@ macro_rules! rpc {
|
||||
let request: Request = json::decode(&s).unwrap();
|
||||
match request {
|
||||
$(
|
||||
Request::$fn_name(in_) => {
|
||||
let resp = self.$fn_name(in_);
|
||||
Request::$fn_name($($arg),*) => {
|
||||
let resp = self.$fn_name($($arg),*);
|
||||
let resp = json::encode(&resp).unwrap();
|
||||
try!(conn.write_u64::<BigEndian>(resp.len() as u64));
|
||||
try!(conn.write_all(resp.as_bytes()));
|
||||
@@ -42,7 +42,7 @@ macro_rules! rpc {
|
||||
#[derive(Debug, RustcEncodable, RustcDecodable)]
|
||||
enum Request {
|
||||
$(
|
||||
$fn_name($in_),
|
||||
$fn_name($($in_),*),
|
||||
)*
|
||||
}
|
||||
|
||||
@@ -50,9 +50,9 @@ macro_rules! rpc {
|
||||
|
||||
impl Client {
|
||||
$(
|
||||
pub fn $fn_name(&mut self, in_: $in_) -> Result<$out, io::Error> {
|
||||
pub fn $fn_name(&mut self, $($arg: $in_),*) -> Result<$out, io::Error> {
|
||||
let ref mut conn = self.0;
|
||||
let request = Request::$fn_name(in_);
|
||||
let request = Request::$fn_name($($arg),*);
|
||||
let request = json::encode(&request).unwrap();
|
||||
try!(conn.write_u64::<BigEndian>(request.len() as u64));
|
||||
try!(conn.write_all(request.as_bytes()));
|
||||
|
||||
@@ -6,8 +6,8 @@ extern crate byteorder;
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
|
||||
rpc!(my_server:
|
||||
hello(String) -> String;
|
||||
add((i32, i32)) -> i32;
|
||||
add(x: i32, y: i32) -> i32;
|
||||
hello(s: String) -> String;
|
||||
);
|
||||
|
||||
use my_server::*;
|
||||
@@ -17,7 +17,7 @@ impl Service for () {
|
||||
format!("Hello, {}", s)
|
||||
}
|
||||
|
||||
fn add(&self, (x, y): (i32, i32)) -> i32 {
|
||||
fn add(&self, x: i32, y: i32) -> i32 {
|
||||
x + y
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,6 @@ fn main() {
|
||||
});
|
||||
let mut client = Client(TcpStream::connect("127.0.0.1:9000").unwrap());
|
||||
println!("Client running");
|
||||
println!("add((1, 2)) => {}", client.add((1, 2)).unwrap());
|
||||
println!("add(1, 2) => {}", client.add(1, 2).unwrap());
|
||||
println!("hello(\"adam\") => {:?}", client.hello("Adam".into()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user