1use crate::sealed::Sealed;
2use std::future::Future;
3use std::task::{Context, Poll};
4use tokio::io::{AsyncRead, AsyncWrite};
5use tower_service::Service;
67/// 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.
12pub trait MakeConnection<Target>: Sealed<(Target,)> {
13/// The transport provided by this service
14type Connection: AsyncRead + AsyncWrite;
1516/// Errors produced by the connecting service
17type Error;
1819/// The future that eventually produces the transport
20type Future: Future<Output = Result<Self::Connection, Self::Error>>;
2122/// Returns `Poll::Ready(Ok(()))` when it is able to make more connections.
23fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
2425/// Connect and return a transport asynchronously
26fn make_connection(&mut self, target: Target) -> Self::Future;
27}
2829impl<S, Target> Sealed<(Target,)> for S where S: Service<Target> {}
3031impl<C, Target> MakeConnection<Target> for C
32where
33C: Service<Target>,
34 C::Response: AsyncRead + AsyncWrite,
35{
36type Connection = C::Response;
37type Error = C::Error;
38type Future = C::Future;
3940fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
41 Service::poll_ready(self, cx)
42 }
4344fn make_connection(&mut self, target: Target) -> Self::Future {
45 Service::call(self, target)
46 }
47}