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
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
8pub struct TcpConnector<A>(A);
9
10impl<A> TcpConnector<A> {
11    pub fn new(address: A) -> Self {
12        Self(address)
13    }
14
15    /// Get a ref to the address
16    #[inline]
17    pub fn get_addr(&self) -> &A {
18        &self.0
19    }
20
21    /// Get a mutable ref to the address
22    #[inline]
23    pub fn get_addr_mut(&mut self) -> &mut A {
24        &mut self.0
25    }
26}
27
28impl<A, R> Tether<TcpConnector<A>, R>
29where
30    R: Resolver<TcpConnector<A>>,
31    A: 'static + ToSocketAddrs + Clone + Send + Sync,
32{
33    /// Helper function for building a TCP connection
34    pub async fn connect_tcp(address: A, resolver: R) -> Result<Self, std::io::Error> {
35        let connector = TcpConnector::new(address);
36        Tether::connect(connector, resolver).await
37    }
38}
39
40impl<A> Connector 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}