tower_web/util/http/
new_service.rs

1use super::HttpService;
2use util::buf_stream::BufStream;
3
4use futures::Future;
5use http::{Request, Response};
6use tower_service::NewService;
7
8/// Creates `HttpService` values.
9///
10/// This is not intended to be implemented directly. Instead, it is a trait
11/// alias of sorts, aliasing `tower_service::NewService` trait with
12/// `http::Request` and `http::Response` types.
13pub trait NewHttpService: sealed::Sealed {
14    /// The HTTP request body handled by the service.
15    type RequestBody: BufStream;
16
17    /// The HTTP response body returned by the service.
18    type ResponseBody: BufStream;
19
20    /// Errors produced by the service
21    type Error;
22
23    /// The `Service` value created by this factory
24    type Service: HttpService<RequestBody = Self::RequestBody,
25                             ResponseBody = Self::ResponseBody,
26                                    Error = Self::Error>;
27
28    /// Errors produced while building a service.
29    type InitError;
30
31    /// The future of the `Service` instance.
32    type Future: Future<Item = Self::Service, Error = Self::InitError>;
33
34    /// Create and return a new service value asynchronously.
35    fn new_http_service(&self) -> Self::Future;
36}
37
38impl<T, B1, B2> NewHttpService for T
39where T: NewService<Request = Request<B1>,
40                   Response = Response<B2>>,
41      B1: BufStream,
42      B2: BufStream
43{
44    type RequestBody = B1;
45    type ResponseBody = B2;
46    type Error = T::Error;
47    type Service = T::Service;
48    type InitError = T::InitError;
49    type Future = T::Future;
50
51    fn new_http_service(&self) -> Self::Future {
52        NewService::new_service(self)
53    }
54}
55
56impl<T, B1, B2> sealed::Sealed for T
57where T: NewService<Request = Request<B1>,
58                   Response = Response<B2>>,
59      B1: BufStream,
60      B2: BufStream
61{}
62
63mod sealed {
64    pub trait Sealed {}
65}