diff --git a/src/lib.rs b/src/lib.rs index 9af0538..61cfa9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,9 +47,40 @@ extern crate serde_json; #[macro_use] extern crate log; +use std::io; +use std::convert::From; + /// Provides the tarpc client and server, which implements the tarpc protocol. The protocol /// is defined by the implementation. pub mod protocol; /// Provides the macro used for constructing rpc services and client stubs. pub mod macros; + +/// An error that occurred while processing an RPC request +#[derive(Debug)] +pub enum Error { + #[doc="An IO error occurred."] + Io(io::Error), + + #[doc="An unexpected internal error. Typically a bug in the server impl."] + InternalError, +} + +impl From for Error { + fn from(err: io::Error) -> Error { + Error::Io(err) + } +} + +impl From for Error { + fn from(err: protocol::Error) -> Error { + match err { + protocol::Error::Io(err) => Error::Io(err), + _ => Error::InternalError, + } + } +} + +///The result of an RPC call; either the successful result or the error +pub type Result = ::std::result::Result; diff --git a/src/macros.rs b/src/macros.rs index 77f3a59..3fa6959 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -6,19 +6,19 @@ macro_rules! as_item { ($i:item) => {$i} } #[macro_export] macro_rules! request_fns { ($fn_name:ident( $( $arg:ident : $in_:ty ),* ) -> $out:ty) => ( - pub fn $fn_name(&self, $($arg: $in_),*) -> Result<$out> { + pub fn $fn_name(&self, $($arg: $in_),*) -> $crate::Result<$out> { let reply = try!((self.0).rpc(&request_variant!($fn_name $($arg),*))); let Reply::$fn_name(reply) = reply; Ok(reply) } ); ($( $fn_name:ident( $( $arg:ident : $in_:ty ),* ) -> $out:ty)*) => ( $( - pub fn $fn_name(&self, $($arg: $in_),*) -> Result<$out> { + pub fn $fn_name(&self, $($arg: $in_),*) -> $crate::Result<$out> { let reply = try!((self.0).rpc(&request_variant!($fn_name $($arg),*))); if let Reply::$fn_name(reply) = reply { Ok(reply) } else { - Err(Error::InternalError) + Err($crate::Error::InternalError) } } )*); @@ -53,7 +53,6 @@ macro_rules! rpc_service { ($server:ident: #[doc="A module containing an rpc service and client stub."] pub mod $server { use std::net::ToSocketAddrs; - use std::io; use std::sync::Arc; use $crate::protocol::{ self, @@ -61,34 +60,6 @@ macro_rules! rpc_service { ($server:ident: serve_async, }; - #[doc="An RPC error that occurred during serving an RPC request."] - #[derive(Debug)] - pub enum Error { - #[doc="An IO error occurred."] - Io(io::Error), - - #[doc="An unexpected internal error. Typically a bug in the server impl."] - InternalError, - } - - impl ::std::convert::From for Error { - fn from(err: protocol::Error) -> Error { - match err { - protocol::Error::Io(err) => Error::Io(err), - _ => Error::InternalError, - } - } - } - - impl ::std::convert::From for Error { - fn from(err: io::Error) -> Error { - Error::Io(err) - } - } - - #[doc="The result of an RPC call; either the successful result or the error."] - pub type Result = ::std::result::Result; - #[doc="The provided RPC service."] pub trait Service: Send + Sync { $( @@ -111,7 +82,7 @@ macro_rules! rpc_service { ($server:ident: impl Client { #[doc="Create a new client that connects to the given address."] - pub fn new(addr: A) -> Result + pub fn new(addr: A) -> $crate::Result where A: ToSocketAddrs, { let inner = try!(protocol::Client::new(addr)); @@ -137,7 +108,7 @@ macro_rules! rpc_service { ($server:ident: } #[doc="Start a running service."] - pub fn serve(addr: A, service: S) -> Result + pub fn serve(addr: A, service: S) -> $crate::Result where A: ToSocketAddrs, S: 'static + Service {