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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use super::imports::*;
use crate::local_socket::{self as sync, ToLocalSocketName};
use std::{
io,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
#[derive(Debug)]
pub struct LocalSocketListener {
inner: Arc<sync::LocalSocketListener>,
}
impl LocalSocketListener {
pub async fn bind<'a>(name: impl ToLocalSocketName<'_> + Send + 'static) -> io::Result<Self> {
Ok(Self {
inner: Arc::new(unblock(move || sync::LocalSocketListener::bind(name)).await?),
})
}
pub async fn accept(&self) -> io::Result<LocalSocketStream> {
let s = self.inner.clone();
Ok(LocalSocketStream {
inner: Unblock::new(unblock(move || s.accept()).await?),
})
}
pub fn incoming(&self) -> Incoming {
Incoming {
inner: Unblock::new(SyncArcIncoming {
inner: Arc::clone(&self.inner),
}),
}
}
}
#[derive(Debug)]
pub struct Incoming {
inner: Unblock<SyncArcIncoming>,
}
#[cfg(feature = "nonblocking")]
impl Stream for Incoming {
type Item = Result<LocalSocketStream, io::Error>;
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let poll = <Unblock<_> as Stream>::poll_next(Pin::new(&mut self.inner), ctx);
match poll {
Poll::Ready(val) => {
let val = val.map(|val| match val {
Ok(inner) => Ok(LocalSocketStream {
inner: Unblock::new(inner),
}),
Err(error) => Err(error),
});
Poll::Ready(val)
}
Poll::Pending => Poll::Pending,
}
}
}
#[cfg(feature = "nonblocking")]
impl FusedStream for Incoming {
fn is_terminated(&self) -> bool {
false
}
}
#[derive(Debug)]
struct SyncArcIncoming {
inner: Arc<sync::LocalSocketListener>,
}
impl Iterator for SyncArcIncoming {
type Item = Result<sync::LocalSocketStream, io::Error>;
fn next(&mut self) -> Option<Self::Item> {
Some(self.inner.accept())
}
}
#[derive(Debug)]
pub struct LocalSocketStream {
inner: Unblock<sync::LocalSocketStream>,
}
impl LocalSocketStream {
pub async fn connect<'a>(
name: impl ToLocalSocketName<'a> + Send + 'static,
) -> io::Result<Self> {
Ok(Self {
inner: Unblock::new(unblock(move || sync::LocalSocketStream::connect(name)).await?),
})
}
}
#[cfg(feature = "nonblocking")]
impl AsyncRead for LocalSocketStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize, io::Error>> {
AsyncRead::poll_read(Pin::new(&mut self.inner), cx, buf)
}
}
#[cfg(feature = "nonblocking")]
impl AsyncWrite for LocalSocketStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
AsyncWrite::poll_write(Pin::new(&mut self.inner), cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_flush(Pin::new(&mut self.inner), cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_close(Pin::new(&mut self.inner), cx)
}
}