From c93b082c858f5a3693e55f0dee4c9cd2c87fa291 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 22 Jan 2018 10:42:41 -0800 Subject: [PATCH] refactor(compat): use pub(super) to remove compat_impl modules --- src/client/compat.rs | 57 +++++++++++++++++++++++-- src/client/compat_impl.rs | 53 ----------------------- src/client/mod.rs | 6 +-- src/server/compat.rs | 87 +++++++++++++++++++++++++++++++++++--- src/server/compat_impl.rs | 82 ----------------------------------- src/server/mod.rs | 4 +- src/server/server_proto.rs | 4 +- 7 files changed, 140 insertions(+), 153 deletions(-) delete mode 100644 src/client/compat_impl.rs delete mode 100644 src/server/compat_impl.rs diff --git a/src/client/compat.rs b/src/client/compat.rs index 277d7fee..26399e82 100644 --- a/src/client/compat.rs +++ b/src/client/compat.rs @@ -1,6 +1,55 @@ //! Wrappers to build compatibility with the `http` crate. -pub use super::compat_impl::{ - CompatClient, - CompatFutureResponse -}; +use futures::{Future, Poll, Stream}; +use http; +use tokio_service::Service; + +use client::{Connect, Client, FutureResponse}; +use error::Error; +use proto::Body; + +/// A Client to make outgoing HTTP requests. +#[derive(Debug)] +pub struct CompatClient { + inner: Client +} + +pub(super) fn client(client: Client) -> CompatClient { + CompatClient { inner: client } +} + +impl Service for CompatClient +where C: Connect, + B: Stream + 'static, + B::Item: AsRef<[u8]>, +{ + type Request = http::Request; + type Response = http::Response; + type Error = Error; + type Future = CompatFutureResponse; + + fn call(&self, req: Self::Request) -> Self::Future { + future(self.inner.call(req.into())) + } +} + +/// A `Future` that will resolve to an `http::Response`. +#[must_use = "futures do nothing unless polled"] +#[derive(Debug)] +pub struct CompatFutureResponse { + inner: FutureResponse +} + +pub(super) fn future(fut: FutureResponse) -> CompatFutureResponse { + CompatFutureResponse { inner: fut } +} + +impl Future for CompatFutureResponse { + type Item = http::Response; + type Error = Error; + + fn poll(&mut self) -> Poll { + self.inner.poll() + .map(|a| a.map(|r| r.into())) + } +} diff --git a/src/client/compat_impl.rs b/src/client/compat_impl.rs deleted file mode 100644 index a8a8a038..00000000 --- a/src/client/compat_impl.rs +++ /dev/null @@ -1,53 +0,0 @@ -use futures::{Future, Poll, Stream}; -use http; -use tokio_service::Service; - -use client::{Connect, Client, FutureResponse}; -use error::Error; -use proto::Body; - -/// A Client to make outgoing HTTP requests. -#[derive(Debug)] -pub struct CompatClient { - inner: Client -} - -pub fn client(client: Client) -> CompatClient { - CompatClient { inner: client } -} - -impl Service for CompatClient -where C: Connect, - B: Stream + 'static, - B::Item: AsRef<[u8]>, -{ - type Request = http::Request; - type Response = http::Response; - type Error = Error; - type Future = CompatFutureResponse; - - fn call(&self, req: Self::Request) -> Self::Future { - future(self.inner.call(req.into())) - } -} - -/// A `Future` that will resolve to an `http::Response`. -#[must_use = "futures do nothing unless polled"] -#[derive(Debug)] -pub struct CompatFutureResponse { - inner: FutureResponse -} - -pub fn future(fut: FutureResponse) -> CompatFutureResponse { - CompatFutureResponse { inner: fut } -} - -impl Future for CompatFutureResponse { - type Item = http::Response; - type Error = Error; - - fn poll(&mut self) -> Poll { - self.inner.poll() - .map(|a| a.map(|r| r.into())) - } -} diff --git a/src/client/mod.rs b/src/client/mod.rs index 8a0928e4..ececc3e9 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -32,8 +32,6 @@ mod connect; mod dns; mod pool; #[cfg(feature = "compat")] -mod compat_impl; -#[cfg(feature = "compat")] pub mod compat; /// A Client to make outgoing HTTP requests. @@ -121,13 +119,13 @@ where C: Connect, #[inline] #[cfg(feature = "compat")] pub fn request_compat(&self, req: http::Request) -> compat::CompatFutureResponse { - self::compat_impl::future(self.call(req.into())) + self::compat::future(self.call(req.into())) } /// Convert into a client accepting `http::Request`. #[cfg(feature = "compat")] pub fn into_compat(self) -> compat::CompatClient { - self::compat_impl::client(self) + self::compat::client(self) } } diff --git a/src/server/compat.rs b/src/server/compat.rs index 933e02fb..3a44bd25 100644 --- a/src/server/compat.rs +++ b/src/server/compat.rs @@ -1,7 +1,84 @@ //! Wrappers to build compatibility with the `http` crate. -pub use super::compat_impl::{ - CompatFuture, - CompatService, - NewCompatService -}; +use std::io::{Error as IoError}; + +use futures::{Future, Poll}; +use http; +use tokio_service::{NewService, Service}; + +use error::Error; +use proto::Body; +use proto::request::Request; +use proto::response::Response; + +/// Wraps a `Future` returning an `http::Response` into +/// a `Future` returning a `hyper::server::Response`. +#[derive(Debug)] +pub struct CompatFuture { + future: F +} + +impl Future for CompatFuture + where F: Future, Error=Error> +{ + type Item = Response; + type Error = Error; + + fn poll(&mut self) -> Poll { + self.future.poll() + .map(|a| a.map(|res| res.into())) + } +} + +/// Wraps a `Service` taking an `http::Request` and returning +/// an `http::Response` into a `Service` taking a `hyper::server::Request`, +/// and returning a `hyper::server::Response`. +#[derive(Debug)] +pub struct CompatService { + service: S +} + +pub(super) fn service(service: S) -> CompatService { + CompatService { service: service } +} + +impl Service for CompatService + where S: Service, Response=http::Response, Error=Error> +{ + type Request = Request; + type Response = Response; + type Error = Error; + type Future = CompatFuture; + + fn call(&self, req: Self::Request) -> Self::Future { + CompatFuture { + future: self.service.call(req.into()) + } + } +} + +/// Wraps a `NewService` taking an `http::Request` and returning +/// an `http::Response` into a `NewService` taking a `hyper::server::Request`, +/// and returning a `hyper::server::Response`. +#[derive(Debug)] +pub struct NewCompatService { + new_service: S +} + +pub(super) fn new_service(new_service: S) -> NewCompatService { + NewCompatService { new_service: new_service } +} + +impl NewService for NewCompatService + where S: NewService, Response=http::Response, Error=Error> +{ + type Request = Request; + type Response = Response; + type Error = Error; + type Instance = CompatService; + + fn new_service(&self) -> Result { + self.new_service.new_service() + .map(service) + } +} diff --git a/src/server/compat_impl.rs b/src/server/compat_impl.rs deleted file mode 100644 index d8067a1a..00000000 --- a/src/server/compat_impl.rs +++ /dev/null @@ -1,82 +0,0 @@ -use std::io::{Error as IoError}; - -use futures::{Future, Poll}; -use http; -use tokio_service::{NewService, Service}; - -use error::Error; -use proto::Body; -use proto::request::Request; -use proto::response::Response; - -/// Wraps a `Future` returning an `http::Response` into -/// a `Future` returning a `hyper::server::Response`. -#[derive(Debug)] -pub struct CompatFuture { - future: F -} - -impl Future for CompatFuture - where F: Future, Error=Error> -{ - type Item = Response; - type Error = Error; - - fn poll(&mut self) -> Poll { - self.future.poll() - .map(|a| a.map(|res| res.into())) - } -} - -/// Wraps a `Service` taking an `http::Request` and returning -/// an `http::Response` into a `Service` taking a `hyper::server::Request`, -/// and returning a `hyper::server::Response`. -#[derive(Debug)] -pub struct CompatService { - service: S -} - -pub fn service(service: S) -> CompatService { - CompatService { service: service } -} - -impl Service for CompatService - where S: Service, Response=http::Response, Error=Error> -{ - type Request = Request; - type Response = Response; - type Error = Error; - type Future = CompatFuture; - - fn call(&self, req: Self::Request) -> Self::Future { - CompatFuture { - future: self.service.call(req.into()) - } - } -} - -/// Wraps a `NewService` taking an `http::Request` and returning -/// an `http::Response` into a `NewService` taking a `hyper::server::Request`, -/// and returning a `hyper::server::Response`. -#[derive(Debug)] -pub struct NewCompatService { - new_service: S -} - -pub fn new_service(new_service: S) -> NewCompatService { - NewCompatService { new_service: new_service } -} - -impl NewService for NewCompatService - where S: NewService, Response=http::Response, Error=Error> -{ - type Request = Request; - type Response = Response; - type Error = Error; - type Instance = CompatService; - - fn new_service(&self) -> Result { - self.new_service.new_service() - .map(service) - } -} diff --git a/src/server/mod.rs b/src/server/mod.rs index 9e377324..dae0d9b9 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -3,8 +3,6 @@ //! A `Server` is created to listen on a port, parse HTTP requests, and hand //! them off to a `Service`. -#[cfg(feature = "compat")] -mod compat_impl; #[cfg(feature = "compat")] pub mod compat; mod service; @@ -191,7 +189,7 @@ impl + 'static> Http { Send + Sync + 'static, Bd: Stream, { - self.bind(addr, self::compat_impl::new_service(new_service)) + self.bind(addr, self::compat::new_service(new_service)) } /// Bind the provided `addr` and return a server with a shared `Core`. diff --git a/src/server/server_proto.rs b/src/server/server_proto.rs index 956a3666..5afa347b 100644 --- a/src/server/server_proto.rs +++ b/src/server/server_proto.rs @@ -23,7 +23,7 @@ use proto::{self, request, response}; #[cfg(feature = "compat")] use proto::Body; #[cfg(feature = "compat")] -use super::compat_impl; +use super::compat; use super::Http; impl + 'static> Http { @@ -71,7 +71,7 @@ impl + 'static> Http { I: AsyncRead + AsyncWrite + 'static, { self.bind_server(handle, io, HttpService { - inner: compat_impl::service(service), + inner: compat::service(service), remote_addr: remote_addr, }) }