io_tether/
tcp.rs

1//! Tether implementations for TCP sockets
2use super::*;
3
4use tokio::net::{TcpStream, ToSocketAddrs};
5
6/// Wrapper for building [`TcpStream`]s
7pub struct TcpConnector<A>(A);
8
9impl<A> TcpConnector<A> {
10    pub fn new(address: A) -> Self {
11        Self(address)
12    }
13
14    /// Get a ref to the address
15    #[inline]
16    pub fn get_addr(&self) -> &A {
17        &self.0
18    }
19
20    /// Get a mutable ref to the address
21    #[inline]
22    pub fn get_addr_mut(&mut self) -> &mut A {
23        &mut self.0
24    }
25}
26
27impl<A, R> Tether<TcpConnector<A>, R>
28where
29    R: Resolver<TcpConnector<A>>,
30    A: 'static + ToSocketAddrs + Clone + Send + Sync,
31{
32    /// Helper function for building a TCP connection
33    pub async fn connect_tcp(address: A, resolver: R) -> Result<Self, std::io::Error> {
34        let mut connector = TcpConnector::new(address);
35        let io = connector.connect().await?;
36        Ok(Tether::new(connector, io, resolver))
37    }
38}
39
40impl<A> Io for TcpConnector<A>
41where
42    A: 'static + ToSocketAddrs + Clone + Send + Sync,
43{
44    type Output = TcpStream;
45
46    fn connect(&mut self) -> PinFut<Result<Self::Output, std::io::Error>> {
47        let address = self.0.clone();
48        Box::pin(TcpStream::connect(address))
49    }
50}