tower_http_util/service/
mod.rs

1//! Types and utilities for working with `HttpService`
2
3mod as_service;
4mod into_service;
5
6pub use self::as_service::AsService;
7pub use self::into_service::IntoService;
8
9use futures::{Future, Poll};
10use http::{Request, Response};
11use http_body::Body;
12use tower_service::Service;
13
14use crate::sealed::Sealed;
15
16/// An HTTP service
17///
18/// This is not intended to be implemented directly. Instead, it is a trait
19/// alias of sorts. Implements the `tower_service::Service` trait using
20/// `http::Request` and `http::Response` types.
21pub trait HttpService<RequestBody>: Sealed<RequestBody> {
22    /// Response payload.
23    type ResponseBody: Body;
24
25    /// Errors produced by the service.
26    type Error;
27
28    /// The future response value.
29    type Future: Future<Item = Response<Self::ResponseBody>, Error = Self::Error>;
30
31    /// Returns `Ready` when the service is able to process requests.
32    fn poll_ready(&mut self) -> Poll<(), Self::Error>;
33
34    /// Process the request and return the response asynchronously.
35    fn call(&mut self, request: Request<RequestBody>) -> Self::Future;
36
37    /// Wrap the HttpService so that it implements tower_service::Service
38    /// directly.
39    ///
40    /// Since `HttpService` does not directly implement `Service`, if an
41    /// `HttpService` instance needs to be used where a `T: Service` is
42    /// required, it must be wrapped with a type that provides that
43    /// implementation. `IntoService` does this.
44    fn into_service(self) -> IntoService<Self>
45    where
46        Self: Sized,
47    {
48        IntoService::new(self)
49    }
50
51    /// Same as `into_service` but operates on an HttpService reference.
52    fn as_service(&mut self) -> AsService<Self>
53    where
54        Self: Sized,
55    {
56        AsService::new(self)
57    }
58}
59
60impl<T, B1, B2> HttpService<B1> for T
61where
62    T: Service<Request<B1>, Response = Response<B2>>,
63    B2: Body,
64{
65    type ResponseBody = B2;
66    type Error = T::Error;
67    type Future = T::Future;
68
69    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
70        Service::poll_ready(self)
71    }
72
73    fn call(&mut self, request: Request<B1>) -> Self::Future {
74        Service::call(self, request)
75    }
76}
77
78impl<T, B1, B2> Sealed<B1> for T
79where
80    T: Service<Request<B1>, Response = Response<B2>>,
81    B2: Body,
82{
83}