From a1072c8c06503ac6e782aebe9153c25944ec1fdf Mon Sep 17 00:00:00 2001 From: compressed Date: Thu, 2 Feb 2017 16:36:19 -0500 Subject: [PATCH 1/2] Upgrade serde to 0.9 (#94) * feat(serde): upgrade serde to 0.9 If you are using `#[derive(Serialize, Deserialize)]` or implementing your own (de)serialization behavior for your RPC types you will need to ensure you are using serde 0.9.x. [breaking-change] * chore(byteorder): upgrade byteorder to 1.0 --- Cargo.toml | 8 ++++---- src/macros.rs | 31 ++++++++++++++++++++----------- src/util.rs | 4 ++-- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 37f546c..477290c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,8 @@ readme = "README.md" description = "An RPC framework for Rust with a focus on ease of use." [dependencies] -bincode = "0.6" -byteorder = "0.5" +bincode = "1.0.0-alpha" +byteorder = "1.0" cfg-if = "0.1.0" bytes = "0.3" futures = "0.1.7" @@ -20,8 +20,8 @@ lazy_static = "0.2" log = "0.3" native-tls = { version = "0.1.1", optional = true } scoped-pool = "1.0" -serde = "0.8" -serde_derive = "0.8" +serde = "0.9" +serde_derive = "0.9" tarpc-plugins = { path = "src/plugins" } take = "0.1" tokio-service = "0.1" diff --git a/src/macros.rs b/src/macros.rs index 6c6103b..b5e9edd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -15,8 +15,8 @@ macro_rules! impl_serialize { ($impler:ident, { $($lifetime:tt)* }, $(@($name:ident $n:expr))* -- #($n_:expr) ) => { as_item! { impl$($lifetime)* $crate::serde::Serialize for $impler$($lifetime)* { - fn serialize(&self, impl_serialize_serializer__: &mut S) - -> ::std::result::Result<(), S::Error> + fn serialize(&self, impl_serialize_serializer__: S) + -> ::std::result::Result where S: $crate::serde::Serializer { match *self { @@ -59,7 +59,7 @@ macro_rules! impl_deserialize { impl $crate::serde::Deserialize for $impler { #[allow(non_camel_case_types)] fn deserialize( - impl_deserialize_deserializer__: &mut impl_deserialize_D__) + impl_deserialize_deserializer__: impl_deserialize_D__) -> ::std::result::Result<$impler, impl_deserialize_D__::Error> where impl_deserialize_D__: $crate::serde::Deserializer { @@ -69,7 +69,7 @@ macro_rules! impl_deserialize { } impl $crate::serde::Deserialize for impl_deserialize_Field__ { - fn deserialize(impl_deserialize_deserializer__: &mut D) + fn deserialize(impl_deserialize_deserializer__: D) -> ::std::result::Result where D: $crate::serde::Deserializer { @@ -77,7 +77,11 @@ macro_rules! impl_deserialize { impl $crate::serde::de::Visitor for impl_deserialize_FieldVisitor__ { type Value = impl_deserialize_Field__; - fn visit_usize(&mut self, impl_deserialize_value__: usize) + fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + formatter.write_str("an unsigned integer") + } + + fn visit_u64(self, impl_deserialize_value__: u64) -> ::std::result::Result where E: $crate::serde::de::Error, { @@ -100,18 +104,23 @@ macro_rules! impl_deserialize { } struct Visitor; - impl $crate::serde::de::EnumVisitor for Visitor { + impl $crate::serde::de::Visitor for Visitor { type Value = $impler; - fn visit(&mut self, mut tarpc_enum_visitor__: V) - -> ::std::result::Result<$impler, V::Error> - where V: $crate::serde::de::VariantVisitor + fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + formatter.write_str("an enum variant") + } + + fn visit_enum(self, tarpc_enum_visitor__: V) + -> ::std::result::Result + where V: $crate::serde::de::EnumVisitor { + use $crate::serde::de::VariantVisitor; match tarpc_enum_visitor__.visit_variant()? { $( - impl_deserialize_Field__::$name => { + (impl_deserialize_Field__::$name, variant) => { ::std::result::Result::Ok( - $impler::$name(tarpc_enum_visitor__.visit_newtype()?)) + $impler::$name(variant.visit_newtype()?)) } ),* } diff --git a/src/util.rs b/src/util.rs index 534a4eb..0eb1f75 100644 --- a/src/util.rs +++ b/src/util.rs @@ -52,7 +52,7 @@ impl Stream for Never { } impl Serialize for Never { - fn serialize(&self, _: &mut S) -> Result<(), S::Error> + fn serialize(&self, _: S) -> Result where S: Serializer { self.0 @@ -61,7 +61,7 @@ impl Serialize for Never { // Please don't try to deserialize this. :( impl Deserialize for Never { - fn deserialize(_: &mut D) -> Result + fn deserialize(_: D) -> Result where D: Deserializer { panic!("Never cannot be instantiated!"); From 338c91d393d04f0d0e41e519546366b1c96e9291 Mon Sep 17 00:00:00 2001 From: compressed Date: Sun, 12 Feb 2017 17:52:38 -0500 Subject: [PATCH 2/2] fix(bincode): updates to support bincode 1.0.0-alpha2 (#100) Removed: - `Error::ClientDeserialize` variant - `Error::ServerDeserialize` variant - `WireError::ServerDeserialize` variant --- Cargo.toml | 2 +- src/client.rs | 6 +++--- src/errors.rs | 30 ++++++------------------------ src/macros.rs | 18 ++++++------------ src/protocol.rs | 12 ++++++------ src/server.rs | 6 +++--- 6 files changed, 25 insertions(+), 49 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 477290c..1a79256 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ readme = "README.md" description = "An RPC framework for Rust with a focus on ease of use." [dependencies] -bincode = "1.0.0-alpha" +bincode = "1.0.0-alpha2" byteorder = "1.0" cfg-if = "0.1.0" bytes = "0.3" diff --git a/src/client.rs b/src/client.rs index a1a2b19..f7689c1 100644 --- a/src/client.rs +++ b/src/client.rs @@ -4,7 +4,7 @@ // This file may not be copied, modified, or distributed except according to those terms. use {Reactor, WireError}; -use bincode::serde::DeserializeError; +use bincode; use futures::{self, Future}; use protocol::Proto; #[cfg(feature = "tls")] @@ -18,7 +18,7 @@ use tokio_proto::BindClient as ProtoBindClient; use tokio_proto::multiplex::Multiplex; use tokio_service::Service; -type WireResponse = Result>, DeserializeError>; +type WireResponse = Result>, bincode::Error>; type ResponseFuture = futures::Map< as Service>::Future, fn(WireResponse) -> Result>>; type BindClient = >> as @@ -117,7 +117,7 @@ impl Client fn map_err(resp: WireResponse) -> Result> { resp.map(|r| r.map_err(::Error::from)) - .map_err(::Error::ClientDeserialize) + .map_err(::Error::ClientSerialize) .and_then(|r| r) } } diff --git a/src/errors.rs b/src/errors.rs index 3bb4189..cc172d2 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -3,7 +3,6 @@ // Licensed under the MIT License, . // This file may not be copied, modified, or distributed except according to those terms. -use bincode; use serde::{Deserialize, Serialize}; use std::{fmt, io}; use std::error::Error as StdError; @@ -13,23 +12,15 @@ use std::error::Error as StdError; pub enum Error { /// Any IO error. Io(io::Error), - /// Error in deserializing a server response. + /// Error serializing the client request or deserializing the server response. /// /// Typically this indicates a faulty implementation of `serde::Serialize` or /// `serde::Deserialize`. - ClientDeserialize(bincode::serde::DeserializeError), - /// Error in serializing a client request. - /// - /// Typically this indicates a faulty implementation of `serde::Serialize`. - ClientSerialize(bincode::serde::SerializeError), - /// Error in deserializing a client request. + ClientSerialize(::bincode::Error), + /// Error serializing the server response or deserializing the client request. /// /// Typically this indicates a faulty implementation of `serde::Serialize` or /// `serde::Deserialize`. - ServerDeserialize(String), - /// Error in serializing a server response. - /// - /// Typically this indicates a faulty implementation of `serde::Serialize`. ServerSerialize(String), /// The server was unable to reply to the rpc for some reason. /// @@ -41,9 +32,7 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Error::ClientDeserialize(ref e) => write!(f, r#"{}: "{}""#, self.description(), e), Error::ClientSerialize(ref e) => write!(f, r#"{}: "{}""#, self.description(), e), - Error::ServerDeserialize(ref e) => write!(f, r#"{}: "{}""#, self.description(), e), Error::ServerSerialize(ref e) => write!(f, r#"{}: "{}""#, self.description(), e), Error::App(ref e) => fmt::Display::fmt(e, f), Error::Io(ref e) => fmt::Display::fmt(e, f), @@ -54,10 +43,8 @@ impl fmt::Display for Er impl StdError for Error { fn description(&self) -> &str { match *self { - Error::ClientDeserialize(_) => "The client failed to deserialize the server response.", - Error::ClientSerialize(_) => "The client failed to serialize the request.", - Error::ServerDeserialize(_) => "The server failed to deserialize the request.", - Error::ServerSerialize(_) => "The server failed to serialize the response.", + Error::ClientSerialize(_) => "The client failed to serialize the request or deserialize the response.", + Error::ServerSerialize(_) => "The server failed to serialize the response or deserialize the request.", Error::App(ref e) => e.description(), Error::Io(ref e) => e.description(), } @@ -65,9 +52,7 @@ impl StdError for Error< fn cause(&self) -> Option<&StdError> { match *self { - Error::ClientDeserialize(ref e) => e.cause(), Error::ClientSerialize(ref e) => e.cause(), - Error::ServerDeserialize(_) | Error::ServerSerialize(_) | Error::App(_) => None, Error::Io(ref e) => e.cause(), @@ -84,7 +69,6 @@ impl From for Error { impl From> for Error { fn from(err: WireError) -> Self { match err { - WireError::ServerDeserialize(s) => Error::ServerDeserialize(s), WireError::ServerSerialize(s) => Error::ServerSerialize(s), WireError::App(e) => Error::App(e), } @@ -95,9 +79,7 @@ impl From> for Error { #[doc(hidden)] #[derive(Deserialize, Serialize, Clone, Debug)] pub enum WireError { - /// Error in deserializing a client request. - ServerDeserialize(String), - /// Error in serializing server response. + /// Error in serializing the server response or deserializing the client request. ServerSerialize(String), /// The server was unable to reply to the rpc for some reason. App(E), diff --git a/src/macros.rs b/src/macros.rs index b5e9edd..7233269 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -408,7 +408,7 @@ macro_rules! service { where tarpc_service_S__: FutureService { type Request = ::std::result::Result; + $crate::bincode::Error>; type Response = $crate::server::Response; type Error = ::std::io::Error; @@ -421,7 +421,7 @@ macro_rules! service { return tarpc_service_FutureReply__::DeserializeError( $crate::futures::finished( ::std::result::Result::Err( - $crate::WireError::ServerDeserialize( + $crate::WireError::ServerSerialize( ::std::string::ToString::to_string( &tarpc_service_deserialize_err__))))); } @@ -667,15 +667,9 @@ macro_rules! service { unreachable!() } } - $crate::Error::ServerDeserialize(tarpc_service_err__) => { - $crate::Error::ServerDeserialize(tarpc_service_err__) - } $crate::Error::ServerSerialize(tarpc_service_err__) => { $crate::Error::ServerSerialize(tarpc_service_err__) } - $crate::Error::ClientDeserialize(tarpc_service_err__) => { - $crate::Error::ClientDeserialize(tarpc_service_err__) - } $crate::Error::ClientSerialize(tarpc_service_err__) => { $crate::Error::ClientSerialize(tarpc_service_err__) } @@ -913,8 +907,8 @@ mod functional_test { Server>(Server); let client = client.expect("Could not connect!"); match client.foo().err().expect("failed unwrap") { - ::Error::ServerDeserialize(_) => {} // good - bad => panic!("Expected Error::ServerDeserialize but got {}", bad), + ::Error::ServerSerialize(_) => {} // good + bad => panic!("Expected Error::ServerSerialize but got {}", bad), } } } @@ -968,8 +962,8 @@ mod functional_test { start_server_with_async_client::(Server); match client.foo().wait().err().unwrap() { - ::Error::ServerDeserialize(_) => {} // good - bad => panic!(r#"Expected Error::ServerDeserialize but got "{}""#, bad), + ::Error::ServerSerialize(_) => {} // good + bad => panic!(r#"Expected Error::ServerSerialize but got "{}""#, bad), } } diff --git a/src/protocol.rs b/src/protocol.rs index b973b38..7d38df8 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -4,7 +4,7 @@ // This file may not be copied, modified, or distributed except according to those terms. use {serde, tokio_core}; -use bincode::{SizeLimit, serde as bincode}; +use bincode::{self, SizeLimit}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use std::io::{self, Cursor}; use std::marker::PhantomData; @@ -40,7 +40,7 @@ impl tokio_core::io::Codec for Codec Decode: serde::Deserialize { type Out = (RequestId, Encode); - type In = (RequestId, Result); + type In = (RequestId, Result); fn encode(&mut self, (id, message): Self::Out, buf: &mut Vec) -> io::Result<()> { buf.write_u64::(id).unwrap(); @@ -121,7 +121,7 @@ impl ServerProto for Proto Decode: serde::Deserialize + 'static { type Response = Encode; - type Request = Result; + type Request = Result; type Transport = Framed>; type BindTransport = Result; @@ -135,7 +135,7 @@ impl ClientProto for Proto Encode: serde::Serialize + 'static, Decode: serde::Deserialize + 'static { - type Response = Result; + type Response = Result; type Request = Encode; type Transport = Framed>; type BindTransport = Result; @@ -158,8 +158,8 @@ fn serialize() { let mut codec: Codec<(char, char, char), (char, char, char)> = Codec::new(); codec.encode(MSG, &mut vec).unwrap(); buf.get_mut().append(&mut vec); - let actual: Result)>, - io::Error> = codec.decode(&mut buf); + let actual: Result)>, io::Error> = + codec.decode(&mut buf); match actual { Ok(Some((id, ref v))) if id == MSG.0 && *v.as_ref().unwrap() == MSG.1 => {} diff --git a/src/server.rs b/src/server.rs index 2739c46..e8314de 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,7 +4,7 @@ // This file may not be copied, modified, or distributed except according to those terms. use {REMOTE, Reactor}; -use bincode::serde::DeserializeError; +use bincode; use errors::WireError; use futures::{self, Async, Future, Stream, future}; use net2; @@ -67,7 +67,7 @@ pub type Response = Result>; #[doc(hidden)] pub fn listen(new_service: S, addr: SocketAddr, options: Options) -> ListenFuture - where S: NewService, + where S: NewService, Response = Response, Error = io::Error> + Send + 'static, Req: Deserialize + 'static, @@ -116,7 +116,7 @@ fn listen_with(new_service: S, handle: Handle, _acceptor: Acceptor) -> io::Result - where S: NewService, + where S: NewService, Response = Response, Error = io::Error> + Send + 'static, Req: Deserialize + 'static,