1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use hyper::client::Client;
use hyper::client::connect::{Connect, Connected, Destination};
use std::path::Path;
use std::sync::Arc;
use tokio::prelude::*;
use tokio::net::unix::UnixStream;

#[derive(Clone, Debug)]
pub struct UnixSocketConnector(Arc<Path>);

impl UnixSocketConnector {
    pub fn new<P: AsRef<Path>>(path: P) -> Self {
        let path = Arc::from(path.as_ref());
        UnixSocketConnector(path)
    }

    pub fn client<P: AsRef<Path>>(path: P) -> Client<Self> {
        Client::builder().build(UnixSocketConnector::new(path))
    }
}

impl Connect for UnixSocketConnector {
    type Transport = UnixStream;
    type Error = tokio::io::Error;
    type Future =
        Box<dyn Future<Item=(UnixStream, Connected), Error=tokio::io::Error> + Send>;

    fn connect(&self, _: Destination) -> Self::Future {
        let fut = UnixStream::connect(self.0.clone())
            .map(|skt| (skt, Connected::new()));
        Box::new(fut)
    }
}