async_uninet/
stream.rs

1use std::pin::Pin;
2use async_std::io;
3use async_std::net;
4use async_std::prelude::*;
5use async_std::task::{Context, Poll};
6#[cfg(unix)]
7use async_std::os::unix::net as unix;
8use crate::SocketAddr;
9
10#[derive(Debug)]
11pub enum Stream {
12    Inet(net::TcpStream),
13    #[cfg(unix)]
14    Unix(unix::UnixStream)
15}
16
17impl From<net::TcpStream> for Stream {
18    fn from(s: net::TcpStream) -> Self {
19        Stream::Inet(s)
20    }
21}
22
23#[cfg(unix)]
24impl From<unix::UnixStream> for Stream {
25    fn from(s: unix::UnixStream) -> Self {
26        Stream::Unix(s)
27    }
28}
29
30impl Stream {
31    pub async fn connect(s: &SocketAddr) -> io::Result<Self> {
32        match s {
33            SocketAddr::Inet(s) => net::TcpStream::connect(s).await.map(Stream::Inet),
34            #[cfg(unix)]
35            SocketAddr::Unix(s) => unix::UnixStream::connect(s).await.map(Stream::Unix),
36        }
37    }
38
39    pub fn local_addr(&self) -> io::Result<SocketAddr> {
40        match self {
41            Stream::Inet(s) => s.local_addr().map(SocketAddr::Inet),
42            #[cfg(unix)]
43            Stream::Unix(s) => s.local_addr().map(|e| e.into())
44        }
45    }
46
47    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
48        match self {
49            Stream::Inet(s) => s.peer_addr().map(SocketAddr::Inet),
50            #[cfg(unix)]
51            Stream::Unix(s) => s.peer_addr().map(|e| e.into())
52        }
53    }
54
55    pub fn shutdown(&self, t: net::Shutdown) -> io::Result<()> {
56        match self {
57            Stream::Inet(s) => s.shutdown(t),
58            #[cfg(unix)]
59            Stream::Unix(s) => s.shutdown(t)
60        }
61    }
62}
63
64impl io::Read for &Stream {
65
66    fn poll_read(
67        self: Pin<&mut Self>,
68        cx: &mut Context<'_>,
69        buf: &mut [u8],
70    ) -> Poll<io::Result<usize>> {
71        match Pin::into_inner(self) {
72            Stream::Inet(s) => {
73                (&mut (&*s)).read(buf);
74                Pin::new(&mut &*s).poll_read(cx, buf)
75            },
76            #[cfg(unix)]
77            Stream::Unix(s) => {
78                (&mut (&*s)).read(buf);
79                Pin::new(&mut &*s).poll_read(cx, buf)
80            },
81        }
82    }
83}
84
85impl io::Read for Stream {
86
87    fn poll_read(
88        self: Pin<&mut Self>,
89        cx: &mut Context<'_>,
90        buf: &mut [u8],
91    ) -> Poll<io::Result<usize>> {
92        Pin::new(&mut &*self).poll_read(cx, buf)
93    }
94}
95
96impl io::Write for &Stream {
97
98    fn poll_write(
99        self: Pin<&mut Self>,
100        cx: &mut Context<'_>,
101        buf: &[u8],
102    ) -> Poll<io::Result<usize>> {
103        match Pin::into_inner(self) {
104            Stream::Inet(s) => {
105                (&mut (&*s)).write(buf);
106                Pin::new(&mut &*s).poll_write(cx, buf)
107            },
108            #[cfg(unix)]
109            Stream::Unix(s) => {
110                (&mut (&*s)).write(buf);
111                Pin::new(&mut &*s).poll_write(cx, buf)
112            },
113        }
114    }
115
116    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
117        match Pin::into_inner(self) {
118            Stream::Inet(s) => {
119                (&mut (&*s)).flush();
120                Pin::new(&mut &*s).poll_flush(cx)
121            },
122            #[cfg(unix)]
123            Stream::Unix(s) => {
124                (&mut (&*s)).flush();
125                Pin::new(&mut &*s).poll_flush(cx)
126            },
127        }
128    }
129
130    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
131        match Pin::into_inner(self) {
132            Stream::Inet(s) => {
133                (&mut (&*s)).flush();
134                Pin::new(&mut &*s).poll_close(cx)
135            },
136            #[cfg(unix)]
137            Stream::Unix(s) => {
138                (&mut (&*s)).flush();
139                Pin::new(&mut &*s).poll_close(cx)
140            },
141        }
142    }
143}
144
145impl io::Write for Stream {
146
147    fn poll_write(
148        self: Pin<&mut Self>,
149        cx: &mut Context<'_>,
150        buf: &[u8],
151    ) -> Poll<io::Result<usize>> {
152        Pin::new(&mut &*self).poll_write(cx, buf)
153    }
154
155    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
156        Pin::new(&mut &*self).poll_flush(cx)
157    }
158
159    fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
160        Pin::new(&mut &*self).poll_close(cx)
161    }
162}