io_tether/
unix.rs

1//! Tether implementations for Unix sockets
2use super::*;
3
4use std::path::Path;
5
6use tokio::net::UnixStream;
7
8/// Wrapper for building [`UnixStream`]s
9pub struct UnixConnector<P>(P);
10
11impl<P> UnixConnector<P> {
12    pub fn new(path: P) -> Self {
13        Self(path)
14    }
15
16    pub fn get_path(&self) -> &P {
17        &self.0
18    }
19
20    pub fn get_path_mut(&mut self) -> &mut P {
21        &mut self.0
22    }
23}
24
25impl<P, R> Tether<UnixConnector<P>, R>
26where
27    R: Resolver<UnixConnector<P>>,
28    P: AsRef<Path>,
29{
30    /// Helper function for building a Unix socket connection
31    pub async fn connect_unix(path: P, resolver: R) -> Result<Self, std::io::Error> {
32        let mut connector = UnixConnector::new(path);
33        let io = connector.connect().await?;
34        Ok(Tether::new(connector, io, resolver))
35    }
36}
37
38impl<P> Io for UnixConnector<P>
39where
40    P: AsRef<Path>,
41{
42    type Output = UnixStream;
43
44    fn connect(&mut self) -> PinFut<Result<Self::Output, std::io::Error>> {
45        let path = self.0.as_ref().to_path_buf();
46        Box::pin(UnixStream::connect(path))
47    }
48}