1 use crate::sealed::Sealed;
2 use futures::{Future, Poll};
3 use tokio_io::{AsyncRead, AsyncWrite};
4 use tower_service::Service;
5 
6 /// The MakeConnection trait is used to create transports
7 ///
8 /// The goal of this service is to allow composable methods for creating
9 /// `AsyncRead + AsyncWrite` transports. This could mean creating a TLS
10 /// based connection or using some other method to authenticate the connection.
11 pub trait MakeConnection<Target>: Sealed<(Target,)> {
12     /// The transport provided by this service
13     type Connection: AsyncRead + AsyncWrite;
14 
15     /// Errors produced by the connecting service
16     type Error;
17 
18     /// The future that eventually produces the transport
19     type Future: Future<Item = Self::Connection, Error = Self::Error>;
20 
21     /// Returns `Ready` when it is able to make more connections.
poll_ready(&mut self) -> Poll<(), Self::Error>22     fn poll_ready(&mut self) -> Poll<(), Self::Error>;
23 
24     /// Connect and return a transport asynchronously
make_connection(&mut self, target: Target) -> Self::Future25     fn make_connection(&mut self, target: Target) -> Self::Future;
26 }
27 
28 impl<S, Target> Sealed<(Target,)> for S where S: Service<Target> {}
29 
30 impl<C, Target> MakeConnection<Target> for C
31 where
32     C: Service<Target>,
33     C::Response: AsyncRead + AsyncWrite,
34 {
35     type Connection = C::Response;
36     type Error = C::Error;
37     type Future = C::Future;
38 
poll_ready(&mut self) -> Poll<(), Self::Error>39     fn poll_ready(&mut self) -> Poll<(), Self::Error> {
40         Service::poll_ready(self)
41     }
42 
make_connection(&mut self, target: Target) -> Self::Future43     fn make_connection(&mut self, target: Target) -> Self::Future {
44         Service::call(self, target)
45     }
46 }
47