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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
use std::pin::Pin; use async_std::io; use async_std::net; use async_std::prelude::*; use async_std::task::{Context, Poll}; #[cfg(unix)] use async_std::os::unix::net as unix; use crate::SocketAddr; #[derive(Debug)] pub enum Stream { Inet(net::TcpStream), #[cfg(unix)] Unix(unix::UnixStream) } impl From<net::TcpStream> for Stream { fn from(s: net::TcpStream) -> Stream { Stream::Inet(s) } } #[cfg(unix)] impl From<unix::UnixStream> for Stream { fn from(s: unix::UnixStream) -> Stream { Stream::Unix(s) } } impl Stream { pub async fn connect(s: &SocketAddr) -> io::Result<Self> { match s { SocketAddr::Inet(s) => net::TcpStream::connect(s).await.map(Stream::Inet), #[cfg(unix)] SocketAddr::Unix(s) => unix::UnixStream::connect(s).await.map(Stream::Unix), } } pub fn local_addr(&self) -> io::Result<SocketAddr> { match self { Stream::Inet(s) => s.local_addr().map(SocketAddr::Inet), #[cfg(unix)] Stream::Unix(s) => s.local_addr().map(|e| e.into()) } } pub fn peer_addr(&self) -> io::Result<SocketAddr> { match self { Stream::Inet(s) => s.peer_addr().map(SocketAddr::Inet), #[cfg(unix)] Stream::Unix(s) => s.peer_addr().map(|e| e.into()) } } pub fn shutdown(&self, t: net::Shutdown) -> io::Result<()> { match self { Stream::Inet(s) => s.shutdown(t), #[cfg(unix)] Stream::Unix(s) => s.shutdown(t) } } } impl io::Read for &Stream { fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<io::Result<usize>> { match Pin::into_inner(self) { Stream::Inet(s) => { (&mut (&*s)).read(buf); Pin::new(&mut &*s).poll_read(cx, buf) }, #[cfg(unix)] Stream::Unix(s) => { (&mut (&*s)).read(buf); Pin::new(&mut &*s).poll_read(cx, buf) }, } } } impl io::Write for &Stream { fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<io::Result<usize>> { match Pin::into_inner(self) { Stream::Inet(s) => { (&mut (&*s)).write(buf); Pin::new(&mut &*s).poll_write(cx, buf) }, #[cfg(unix)] Stream::Unix(s) => { (&mut (&*s)).write(buf); Pin::new(&mut &*s).poll_write(cx, buf) }, } } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { match Pin::into_inner(self) { Stream::Inet(s) => { (&mut (&*s)).flush(); Pin::new(&mut &*s).poll_flush(cx) }, #[cfg(unix)] Stream::Unix(s) => { (&mut (&*s)).flush(); Pin::new(&mut &*s).poll_flush(cx) }, } } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { match Pin::into_inner(self) { Stream::Inet(s) => { (&mut (&*s)).flush(); Pin::new(&mut &*s).poll_close(cx) }, #[cfg(unix)] Stream::Unix(s) => { (&mut (&*s)).flush(); Pin::new(&mut &*s).poll_close(cx) }, } } } impl io::Read for Stream { fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<io::Result<usize>> { Pin::new(&mut &*self).poll_read(cx, buf) } } impl io::Write for Stream { fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<io::Result<usize>> { Pin::new(&mut &*self).poll_write(cx, buf) } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { Pin::new(&mut &*self).poll_flush(cx) } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { Pin::new(&mut &*self).poll_close(cx) } }