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 connector = TcpConnector::new(address);
35        Tether::connect(connector, resolver).await
36    }
37}
38
39impl<A> Io for TcpConnector<A>
40where
41    A: 'static + ToSocketAddrs + Clone + Send + Sync,
42{
43    type Output = TcpStream;
44
45    fn connect(&mut self) -> PinFut<Result<Self::Output, std::io::Error>> {
46        let address = self.0.clone();
47        Box::pin(TcpStream::connect(address))
48    }
49}