aral_runtime_async_std/os/unix/net/
mod.rs

1use crate::io::{Read, Write};
2use async_std::{
3    io::{ReadExt, WriteExt},
4    os::unix::net::{
5        UnixDatagram as AsyncStdUnixDatagram, UnixListener as AsyncStdUnixListener,
6        UnixStream as AsyncStdUnixStream,
7    },
8};
9use std::{io::Result, net::Shutdown, path::Path};
10
11pub struct SocketAddr(async_std::os::unix::net::SocketAddr);
12
13impl SocketAddr {
14    #[inline]
15    pub fn as_pathname(&self) -> Option<&Path> {
16        self.0.as_pathname()
17    }
18
19    #[inline]
20    pub fn is_unnamed(&self) -> bool {
21        self.0.is_unnamed()
22    }
23}
24
25pub struct UnixDatagram(AsyncStdUnixDatagram);
26
27impl UnixDatagram {
28    pub async fn bind(path: impl AsRef<Path>) -> Result<Self> {
29        AsyncStdUnixDatagram::bind(path.as_ref())
30            .await
31            .map(UnixDatagram)
32    }
33
34    #[inline]
35    pub async fn connect(&self, path: impl AsRef<Path>) -> Result<()> {
36        self.0.connect(path.as_ref()).await
37    }
38
39    #[inline]
40    pub fn local_addr(&self) -> Result<SocketAddr> {
41        self.0.local_addr().map(SocketAddr)
42    }
43
44    pub fn pair() -> Result<(UnixDatagram, UnixDatagram)> {
45        AsyncStdUnixDatagram::pair().map(|(a, b)| (UnixDatagram(a), UnixDatagram(b)))
46    }
47
48    #[inline]
49    pub fn peer_addr(&self) -> Result<SocketAddr> {
50        self.0.peer_addr().map(SocketAddr)
51    }
52
53    #[inline]
54    pub async fn recv(&self, buf: &mut [u8]) -> Result<usize> {
55        self.0.recv(buf).await
56    }
57
58    #[inline]
59    pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
60        self.0
61            .recv_from(buf)
62            .await
63            .map(|(n, addr)| (n, SocketAddr(addr)))
64    }
65
66    #[inline]
67    pub async fn send(&self, buf: &[u8]) -> Result<usize> {
68        self.0.send(buf).await
69    }
70
71    #[inline]
72    pub async fn send_to(&self, buf: &[u8], path: impl AsRef<Path>) -> Result<usize> {
73        self.0.send_to(buf, path.as_ref()).await
74    }
75
76    #[inline]
77    pub fn shutdown(&self, how: Shutdown) -> Result<()> {
78        self.0.shutdown(how)
79    }
80
81    #[inline]
82    pub fn unbound() -> Result<UnixDatagram> {
83        AsyncStdUnixDatagram::unbound().map(UnixDatagram)
84    }
85}
86
87pub struct UnixStream(AsyncStdUnixStream);
88
89impl UnixStream {
90    pub async fn connect(path: impl AsRef<Path>) -> Result<UnixStream> {
91        AsyncStdUnixStream::connect(path.as_ref())
92            .await
93            .map(UnixStream)
94    }
95
96    #[inline]
97    pub fn local_addr(&self) -> Result<SocketAddr> {
98        self.0.local_addr().map(SocketAddr)
99    }
100
101    #[inline]
102    pub fn pair() -> Result<(UnixStream, UnixStream)> {
103        AsyncStdUnixStream::pair().map(|(a, b)| (UnixStream(a), UnixStream(b)))
104    }
105
106    #[inline]
107    pub fn peer_addr(&self) -> Result<SocketAddr> {
108        self.0.peer_addr().map(SocketAddr)
109    }
110}
111
112impl Read for UnixStream {
113    #[inline]
114    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
115        ReadExt::read(&mut self.0, buf).await
116    }
117}
118
119impl Write for UnixStream {
120    #[inline]
121    async fn write(&mut self, buf: &[u8]) -> Result<usize> {
122        WriteExt::write(&mut self.0, buf).await
123    }
124
125    #[inline]
126    async fn flush(&mut self) -> Result<()> {
127        WriteExt::flush(&mut self.0).await
128    }
129}
130
131pub struct UnixListener(AsyncStdUnixListener);
132
133impl UnixListener {
134    pub async fn accept(&self) -> Result<(UnixStream, SocketAddr)> {
135        self.0
136            .accept()
137            .await
138            .map(|(stream, addr)| (UnixStream(stream), SocketAddr(addr)))
139    }
140
141    pub async fn bind(path: impl AsRef<Path>) -> Result<UnixListener> {
142        AsyncStdUnixListener::bind(path.as_ref())
143            .await
144            .map(UnixListener)
145    }
146
147    #[inline]
148    pub fn local_addr(&self) -> Result<SocketAddr> {
149        self.0.local_addr().map(SocketAddr)
150    }
151}