1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use super::HttpService;
use util::buf_stream::BufStream;

use futures::Future;
use http::{Request, Response};
use tower_service::NewService;

/// Creates `HttpService` values.
///
/// This is not intended to be implemented directly. Instead, it is a trait
/// alias of sorts, aliasing `tower_service::NewService` trait with
/// `http::Request` and `http::Response` types.
pub trait NewHttpService: sealed::Sealed {
    /// The HTTP request body handled by the service.
    type RequestBody: BufStream;

    /// The HTTP response body returned by the service.
    type ResponseBody: BufStream;

    /// Errors produced by the service
    type Error;

    /// The `Service` value created by this factory
    type Service: HttpService<RequestBody = Self::RequestBody,
                             ResponseBody = Self::ResponseBody,
                                    Error = Self::Error>;

    /// Errors produced while building a service.
    type InitError;

    /// The future of the `Service` instance.
    type Future: Future<Item = Self::Service, Error = Self::InitError>;

    /// Create and return a new service value asynchronously.
    fn new_http_service(&self) -> Self::Future;
}

impl<T, B1, B2> NewHttpService for T
where T: NewService<Request = Request<B1>,
                   Response = Response<B2>>,
      B1: BufStream,
      B2: BufStream
{
    type RequestBody = B1;
    type ResponseBody = B2;
    type Error = T::Error;
    type Service = T::Service;
    type InitError = T::InitError;
    type Future = T::Future;

    fn new_http_service(&self) -> Self::Future {
        NewService::new_service(self)
    }
}

impl<T, B1, B2> sealed::Sealed for T
where T: NewService<Request = Request<B1>,
                   Response = Response<B2>>,
      B1: BufStream,
      B2: BufStream
{}

mod sealed {
    pub trait Sealed {}
}