tower/make/
make_connection.rs

1use crate::sealed::Sealed;
2use std::future::Future;
3use std::task::{Context, Poll};
4use tokio::io::{AsyncRead, AsyncWrite};
5use 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.
12pub 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.
23    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
24
25    /// Connect and return a transport asynchronously
26    fn make_connection(&mut self, target: Target) -> Self::Future;
27}
28
29impl<S, Target> Sealed<(Target,)> for S where S: Service<Target> {}
30
31impl<C, Target> MakeConnection<Target> for C
32where
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
40    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
41        Service::poll_ready(self, cx)
42    }
43
44    fn make_connection(&mut self, target: Target) -> Self::Future {
45        Service::call(self, target)
46    }
47}