tower_http_util/
connection.rs

1//! Contains all Http Connection utilities.
2//!
3//! This module provides a `HttpMakeConnection`, this trait provides a
4//! HTTP aware connection. This is for use with libraries like `tower-hyper`.
5
6use futures::{Future, Poll};
7use http_connection::HttpConnection;
8use tokio_io::{AsyncRead, AsyncWrite};
9use tower_service::Service;
10
11/// A Http aware connection creator.
12///
13/// This type is a trait alias that produces `HttpConnection` aware
14/// connections.
15pub trait HttpMakeConnection<Target>: sealed::Sealed<Target> {
16    /// The transport provided by this service that is HTTP aware.
17    type Connection: HttpConnection + AsyncRead + AsyncWrite;
18
19    /// Errors produced by the connecting service
20    type Error;
21
22    /// The future that eventually produces the transport
23    type Future: Future<Item = Self::Connection, Error = Self::Error>;
24
25    /// Returns `Ready` when it is able to make more connections.
26    fn poll_ready(&mut self) -> Poll<(), Self::Error>;
27
28    /// Connect and return a transport asynchronously
29    fn make_connection(&mut self, target: Target) -> Self::Future;
30}
31
32impl<C, Target> sealed::Sealed<Target> for C where C: Service<Target> {}
33
34impl<C, Target> HttpMakeConnection<Target> for C
35where
36    C: Service<Target>,
37    C::Response: HttpConnection + AsyncRead + AsyncWrite,
38{
39    type Connection = C::Response;
40    type Error = C::Error;
41    type Future = C::Future;
42
43    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
44        Service::poll_ready(self)
45    }
46
47    fn make_connection(&mut self, target: Target) -> Self::Future {
48        Service::call(self, target)
49    }
50}
51
52mod sealed {
53    pub trait Sealed<Target> {}
54}