//! Wrappers to build compatibility with the `http` crate. 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) } }