From e22210bfd859b5bd9cf1effd0091e425c0cdcd81 Mon Sep 17 00:00:00 2001 From: Tim Kuehn Date: Mon, 26 Dec 2016 14:50:54 -0500 Subject: [PATCH] Rename module framed -> protocol, and clarify some type parameters. --- src/client.rs | 12 ++++++---- src/lib.rs | 6 ++--- src/{framed.rs => protocol.rs} | 44 +++++++++++++++++----------------- src/server.rs | 2 +- 4 files changed, 33 insertions(+), 31 deletions(-) rename src/{framed.rs => protocol.rs} (82%) diff --git a/src/client.rs b/src/client.rs index 9b200f7..d175576 100644 --- a/src/client.rs +++ b/src/client.rs @@ -3,9 +3,10 @@ // Licensed under the MIT License, . // This file may not be copied, modified, or distributed except according to those terms. -use {WireError, framed}; +use WireError; use bincode::serde::DeserializeError; use futures::{self, Future}; +use protocol::Proto; use serde::{Deserialize, Serialize}; use std::fmt; use std::io; @@ -18,7 +19,7 @@ type WireResponse = Result>, DeserializeError type ResponseFuture = futures::Map< as Service>::Future, fn(WireResponse) -> Result>>; type BindClient = - >> as ProtoBindClient>::BindClient; + >> as ProtoBindClient>::BindClient; /// A client that impls `tokio_service::Service` that writes and reads bytes. /// @@ -81,8 +82,9 @@ impl fmt::Debug for Client /// Exposes a trait for connecting asynchronously to servers. pub mod future { - use {REMOTE, framed}; + use REMOTE; use futures::{self, Async, Future}; + use protocol::Proto; use serde::{Deserialize, Serialize}; use std::io; use std::marker::PhantomData; @@ -175,7 +177,7 @@ pub mod future { type Output = Client; extern "rust-call" fn call_once(self, (tcp,): (TcpStream,)) -> Client { - Client::new(framed::Proto::new().bind_client(self.0, tcp)) + Client::new(Proto::new().bind_client(self.0, tcp)) } } @@ -193,7 +195,7 @@ pub mod future { remote.spawn(move |handle| { let handle2 = handle.clone(); TcpStream::connect(&addr, handle) - .map(move |tcp| Client::new(framed::Proto::new().bind_client(&handle2, tcp))) + .map(move |tcp| Client::new(Proto::new().bind_client(&handle2, tcp))) .then(move |result| { tx.complete(result); Ok(()) diff --git a/src/lib.rs b/src/lib.rs index 9a6593e..f8d7626 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,9 +109,9 @@ mod macros; mod client; /// Provides the base server boilerplate used by service implementations. mod server; -/// Provides an implementation of `FramedIo` that implements the tarpc protocol. -/// The tarpc protocol is defined by the `FramedIo` implementation. -mod framed; +/// Provides implementations of `ClientProto` and `ServerProto` that implement the tarpc protocol. +/// The tarpc protocol is a length-delimited, bincode-serialized payload. +mod protocol; /// Provides a few different error types. mod errors; diff --git a/src/framed.rs b/src/protocol.rs similarity index 82% rename from src/framed.rs rename to src/protocol.rs index b0bc9c2..430ed93 100644 --- a/src/framed.rs +++ b/src/protocol.rs @@ -15,9 +15,9 @@ use tokio_proto::multiplex::{ClientProto, ServerProto}; use util::Debugger; // `T` is the type that `Codec` parses. -pub struct Codec { +pub struct Codec { state: CodecState, - _phantom_data: PhantomData<(Req, Resp)>, + _phantom_data: PhantomData<(Encode, Decode)>, } enum CodecState { @@ -26,7 +26,7 @@ enum CodecState { Payload { id: u64, len: u64 }, } -impl Codec { +impl Codec { fn new() -> Self { Codec { state: CodecState::Id, @@ -35,12 +35,12 @@ impl Codec { } } -impl tokio_core::io::Codec for Codec - where Req: serde::Deserialize, - Resp: serde::Serialize, +impl tokio_core::io::Codec for Codec + where Encode: serde::Serialize, + Decode: serde::Deserialize, { - type Out = (RequestId, Resp); - type In = (RequestId, Result); + type Out = (RequestId, Encode); + type In = (RequestId, Result); fn encode(&mut self, (id, message): Self::Out, buf: &mut Vec) -> io::Result<()> { buf.write_u64::(id).unwrap(); @@ -105,24 +105,24 @@ impl tokio_core::io::Codec for Codec } /// Implements the `multiplex::ServerProto` trait. -pub struct Proto(PhantomData<(Req, Resp)>); +pub struct Proto(PhantomData<(Encode, Decode)>); -impl Proto { +impl Proto { /// Returns a new `Proto`. pub fn new() -> Self { Proto(PhantomData) } } -impl ServerProto for Proto +impl ServerProto for Proto where T: Io + 'static, - Req: serde::Deserialize + 'static, - Resp: serde::Serialize + 'static, + Encode: serde::Serialize + 'static, + Decode: serde::Deserialize + 'static, { - type Response = Resp; - type Request = Result; + type Response = Encode; + type Request = Result; type Error = io::Error; - type Transport = Framed>; + type Transport = Framed>; type BindTransport = Result; fn bind_transport(&self, io: T) -> Self::BindTransport { @@ -130,15 +130,15 @@ impl ServerProto for Proto } } -impl ClientProto for Proto +impl ClientProto for Proto where T: Io + 'static, - Req: serde::Serialize + 'static, - Resp: serde::Deserialize + 'static, + Encode: serde::Serialize + 'static, + Decode: serde::Deserialize + 'static, { - type Response = Result; - type Request = Req; + type Response = Result; + type Request = Encode; type Error = io::Error; - type Transport = Framed>; + type Transport = Framed>; type BindTransport = Result; fn bind_transport(&self, io: T) -> Self::BindTransport { diff --git a/src/server.rs b/src/server.rs index de2506e..5c8c96b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -6,7 +6,7 @@ use {REMOTE, net2}; use bincode::serde::DeserializeError; use errors::WireError; -use framed::Proto; +use protocol::Proto; use futures::{self, Async, Future, Stream}; use serde::{Deserialize, Serialize}; use std::io;