tower_async/make/
make_connection.rs

1use crate::sealed::Sealed;
2use tokio::io::{AsyncRead, AsyncWrite};
3use tower_async_service::Service;
4
5/// The [`MakeConnection`] trait is used to create transports.
6///
7/// The goal of this service is to allow composable methods for creating
8/// `AsyncRead + AsyncWrite` transports. This could mean creating a TLS
9/// based connection or using some other method to authenticate the connection.
10pub trait MakeConnection<Target>: Sealed<(Target,)> {
11    /// The transport provided by this service
12    type Connection: AsyncRead + AsyncWrite;
13
14    /// Errors produced by the connecting service
15    type Error;
16
17    /// Connect and return a transport asynchronously
18    fn make_connection(
19        &self,
20        target: Target,
21    ) -> impl std::future::Future<Output = Result<Self::Connection, Self::Error>>;
22}
23
24impl<S, Target> Sealed<(Target,)> for S where S: Service<Target> {}
25
26impl<C, Target> MakeConnection<Target> for C
27where
28    C: Service<Target>,
29    C::Response: AsyncRead + AsyncWrite,
30{
31    type Connection = C::Response;
32    type Error = C::Error;
33
34    async fn make_connection(&self, target: Target) -> Result<Self::Connection, Self::Error> {
35        Service::call(self, target).await
36    }
37}