async_net.rs

 1#[cfg(not(target_os = "windows"))]
 2pub use smol::net::unix::{UnixListener, UnixStream};
 3
 4#[cfg(target_os = "windows")]
 5pub use windows::{UnixListener, UnixStream};
 6
 7#[cfg(target_os = "windows")]
 8pub mod windows {
 9    use std::{
10        io::Result,
11        path::Path,
12        pin::Pin,
13        task::{Context, Poll},
14    };
15
16    use smol::{
17        Async,
18        io::{AsyncRead, AsyncWrite},
19    };
20
21    pub struct UnixListener(Async<crate::UnixListener>);
22
23    impl UnixListener {
24        pub fn bind<P: AsRef<Path>>(path: P) -> Result<Self> {
25            Ok(UnixListener(Async::new(crate::UnixListener::bind(path)?)?))
26        }
27
28        pub async fn accept(&self) -> Result<(UnixStream, ())> {
29            let (sock, _) = self.0.read_with(|listener| listener.accept()).await?;
30            Ok((UnixStream(Async::new(sock)?), ()))
31        }
32    }
33
34    pub struct UnixStream(Async<crate::UnixStream>);
35
36    impl UnixStream {
37        pub async fn connect<P: AsRef<Path>>(path: P) -> Result<Self> {
38            Ok(UnixStream(Async::new(crate::UnixStream::connect(path)?)?))
39        }
40    }
41
42    impl AsyncRead for UnixStream {
43        fn poll_read(
44            mut self: Pin<&mut Self>,
45            cx: &mut Context<'_>,
46            buf: &mut [u8],
47        ) -> Poll<Result<usize>> {
48            Pin::new(&mut self.0).poll_read(cx, buf)
49        }
50    }
51
52    impl AsyncWrite for UnixStream {
53        fn poll_write(
54            mut self: Pin<&mut Self>,
55            cx: &mut Context<'_>,
56            buf: &[u8],
57        ) -> Poll<Result<usize>> {
58            Pin::new(&mut self.0).poll_write(cx, buf)
59        }
60
61        fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
62            Pin::new(&mut self.0).poll_flush(cx)
63        }
64
65        fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
66            Pin::new(&mut self.0).poll_close(cx)
67        }
68    }
69}