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