refactor(compat): use pub(super) to remove compat_impl modules

This commit is contained in:
Sean McArthur
2018-01-22 10:42:41 -08:00
parent 36e66a5054
commit c93b082c85
7 changed files with 140 additions and 153 deletions

View File

@@ -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<C, B = Body> {
inner: Client<C, B>
}
pub(super) fn client<C, B>(client: Client<C, B>) -> CompatClient<C, B> {
CompatClient { inner: client }
}
impl<C, B> Service for CompatClient<C, B>
where C: Connect,
B: Stream<Error=Error> + 'static,
B::Item: AsRef<[u8]>,
{
type Request = http::Request<B>;
type Response = http::Response<Body>;
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<Body>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Error> {
self.inner.poll()
.map(|a| a.map(|r| r.into()))
}
}

View File

@@ -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<C, B = Body> {
inner: Client<C, B>
}
pub fn client<C, B>(client: Client<C, B>) -> CompatClient<C, B> {
CompatClient { inner: client }
}
impl<C, B> Service for CompatClient<C, B>
where C: Connect,
B: Stream<Error=Error> + 'static,
B::Item: AsRef<[u8]>,
{
type Request = http::Request<B>;
type Response = http::Response<Body>;
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<Body>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Error> {
self.inner.poll()
.map(|a| a.map(|r| r.into()))
}
}

View File

@@ -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<B>) -> 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<C, B> {
self::compat_impl::client(self)
self::compat::client(self)
}
}

View File

@@ -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<F> {
future: F
}
impl<F, Bd> Future for CompatFuture<F>
where F: Future<Item=http::Response<Bd>, Error=Error>
{
type Item = Response<Bd>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
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<S> {
service: S
}
pub(super) fn service<S>(service: S) -> CompatService<S> {
CompatService { service: service }
}
impl<S, Bd> Service for CompatService<S>
where S: Service<Request=http::Request<Body>, Response=http::Response<Bd>, Error=Error>
{
type Request = Request;
type Response = Response<Bd>;
type Error = Error;
type Future = CompatFuture<S::Future>;
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<S> {
new_service: S
}
pub(super) fn new_service<S>(new_service: S) -> NewCompatService<S> {
NewCompatService { new_service: new_service }
}
impl<S, Bd> NewService for NewCompatService<S>
where S: NewService<Request=http::Request<Body>, Response=http::Response<Bd>, Error=Error>
{
type Request = Request;
type Response = Response<Bd>;
type Error = Error;
type Instance = CompatService<S::Instance>;
fn new_service(&self) -> Result<Self::Instance, IoError> {
self.new_service.new_service()
.map(service)
}
}

View File

@@ -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<F> {
future: F
}
impl<F, Bd> Future for CompatFuture<F>
where F: Future<Item=http::Response<Bd>, Error=Error>
{
type Item = Response<Bd>;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
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<S> {
service: S
}
pub fn service<S>(service: S) -> CompatService<S> {
CompatService { service: service }
}
impl<S, Bd> Service for CompatService<S>
where S: Service<Request=http::Request<Body>, Response=http::Response<Bd>, Error=Error>
{
type Request = Request;
type Response = Response<Bd>;
type Error = Error;
type Future = CompatFuture<S::Future>;
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<S> {
new_service: S
}
pub fn new_service<S>(new_service: S) -> NewCompatService<S> {
NewCompatService { new_service: new_service }
}
impl<S, Bd> NewService for NewCompatService<S>
where S: NewService<Request=http::Request<Body>, Response=http::Response<Bd>, Error=Error>
{
type Request = Request;
type Response = Response<Bd>;
type Error = Error;
type Instance = CompatService<S::Instance>;
fn new_service(&self) -> Result<Self::Instance, IoError> {
self.new_service.new_service()
.map(service)
}
}

View File

@@ -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<B: AsRef<[u8]> + 'static> Http<B> {
Send + Sync + 'static,
Bd: Stream<Item=B, Error=::Error>,
{
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`.

View File

@@ -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<B: AsRef<[u8]> + 'static> Http<B> {
@@ -71,7 +71,7 @@ impl<B: AsRef<[u8]> + 'static> Http<B> {
I: AsyncRead + AsyncWrite + 'static,
{
self.bind_server(handle, io, HttpService {
inner: compat_impl::service(service),
inner: compat::service(service),
remote_addr: remote_addr,
})
}