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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::{io, path::Path};
use hyper::server::{Builder, Server};
use conn::SocketIncoming;
pub(crate) mod conn {
use futures_util::ready;
use hyper::server::accept::Accept;
use pin_project::pin_project;
use std::{
io,
path::Path,
pin::Pin,
task::{Context, Poll},
};
use tokio::net::{UnixListener, UnixStream};
#[pin_project]
#[derive(Debug)]
pub struct SocketIncoming {
listener: UnixListener,
}
impl SocketIncoming {
pub fn bind(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
let listener = UnixListener::bind(path)?;
Ok(Self { listener })
}
pub fn from_listener(listener: UnixListener) -> Self {
Self { listener }
}
}
impl Accept for SocketIncoming {
type Conn = UnixStream;
type Error = io::Error;
fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
let conn = ready!(self.listener.poll_accept(cx))?.0;
Poll::Ready(Some(Ok(conn)))
}
}
impl From<UnixListener> for SocketIncoming {
fn from(listener: UnixListener) -> Self {
Self::from_listener(listener)
}
}
}
pub trait UnixServerExt {
fn bind_unix(path: impl AsRef<Path>) -> Result<Builder<SocketIncoming>, io::Error>;
}
impl UnixServerExt for Server<SocketIncoming, ()> {
fn bind_unix(path: impl AsRef<Path>) -> Result<Builder<SocketIncoming>, io::Error> {
let incoming = SocketIncoming::bind(path)?;
Ok(Server::builder(incoming))
}
}