aral_runtime_async_std/net/
mod.rs

1use crate::io::{Read, Write};
2use std::{
3    future::Future,
4    io::Result,
5    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
6};
7
8pub trait ToSocketAddrs: async_std::net::ToSocketAddrs {
9    type Iter: Iterator<Item = SocketAddr>;
10
11    fn to_socket_addrs(&self)
12        -> impl Future<Output = Result<<Self as ToSocketAddrs>::Iter>> + Send;
13}
14
15impl ToSocketAddrs for (&str, u16) {
16    type Iter = std::vec::IntoIter<SocketAddr>;
17
18    #[inline]
19    async fn to_socket_addrs(&self) -> Result<std::vec::IntoIter<SocketAddr>> {
20        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
21    }
22}
23
24impl ToSocketAddrs for (IpAddr, u16) {
25    type Iter = std::option::IntoIter<SocketAddr>;
26
27    #[inline]
28    async fn to_socket_addrs(&self) -> Result<std::option::IntoIter<SocketAddr>> {
29        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
30    }
31}
32
33impl ToSocketAddrs for (Ipv4Addr, u16) {
34    type Iter = std::option::IntoIter<SocketAddr>;
35
36    #[inline]
37    async fn to_socket_addrs(&self) -> Result<std::option::IntoIter<SocketAddr>> {
38        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
39    }
40}
41
42impl ToSocketAddrs for (Ipv6Addr, u16) {
43    type Iter = std::option::IntoIter<SocketAddr>;
44
45    #[inline]
46    async fn to_socket_addrs(&self) -> Result<std::option::IntoIter<SocketAddr>> {
47        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
48    }
49}
50
51impl ToSocketAddrs for SocketAddr {
52    type Iter = std::option::IntoIter<SocketAddr>;
53
54    #[inline]
55    async fn to_socket_addrs(&self) -> Result<std::option::IntoIter<SocketAddr>> {
56        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
57    }
58}
59
60impl ToSocketAddrs for str {
61    type Iter = std::vec::IntoIter<SocketAddr>;
62
63    #[inline]
64    async fn to_socket_addrs(&self) -> Result<std::vec::IntoIter<SocketAddr>> {
65        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
66    }
67}
68
69impl ToSocketAddrs for String {
70    type Iter = std::vec::IntoIter<SocketAddr>;
71
72    #[inline]
73    async fn to_socket_addrs(&self) -> Result<std::vec::IntoIter<SocketAddr>> {
74        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
75    }
76}
77
78impl ToSocketAddrs for SocketAddrV4 {
79    type Iter = std::option::IntoIter<SocketAddr>;
80
81    #[inline]
82    async fn to_socket_addrs(&self) -> Result<std::option::IntoIter<SocketAddr>> {
83        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
84    }
85}
86
87impl ToSocketAddrs for SocketAddrV6 {
88    type Iter = std::option::IntoIter<SocketAddr>;
89
90    #[inline]
91    async fn to_socket_addrs(&self) -> Result<std::option::IntoIter<SocketAddr>> {
92        async_std::net::ToSocketAddrs::to_socket_addrs(self).await
93    }
94}
95
96impl<'a> ToSocketAddrs for &'a [SocketAddr] {
97    type Iter = std::iter::Cloned<std::slice::Iter<'a, SocketAddr>>;
98
99    #[inline]
100    async fn to_socket_addrs(&self) -> Result<std::iter::Cloned<std::slice::Iter<'a, SocketAddr>>> {
101        Ok(self.iter().cloned())
102    }
103}
104
105pub struct TcpStream(async_std::net::TcpStream);
106
107impl TcpStream {
108    #[inline]
109    pub async fn connect(addr: impl crate::net::ToSocketAddrs) -> Result<TcpStream> {
110        async_std::net::TcpStream::connect(addr)
111            .await
112            .map(TcpStream)
113    }
114
115    #[inline]
116    pub fn local_addr(&self) -> Result<SocketAddr> {
117        self.0.local_addr()
118    }
119
120    #[inline]
121    pub fn peer_addr(&self) -> Result<SocketAddr> {
122        self.0.peer_addr()
123    }
124
125    #[inline]
126    pub fn nodelay(&self) -> Result<bool> {
127        self.0.nodelay()
128    }
129
130    #[inline]
131    pub async fn peek(&self, buf: &mut [u8]) -> Result<usize> {
132        self.0.peek(buf).await
133    }
134
135    #[inline]
136    pub fn set_nodelay(&self, nodelay: bool) -> Result<()> {
137        self.0.set_nodelay(nodelay)
138    }
139
140    #[inline]
141    pub fn set_ttl(&self, ttl: u32) -> Result<()> {
142        self.0.set_ttl(ttl)
143    }
144
145    #[inline]
146    pub fn ttl(&self) -> Result<u32> {
147        self.0.ttl()
148    }
149}
150
151impl Read for TcpStream {
152    #[inline]
153    async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
154        async_std::io::ReadExt::read(&mut self.0, buf).await
155    }
156}
157
158impl Write for TcpStream {
159    #[inline]
160    async fn write(&mut self, buf: &[u8]) -> Result<usize> {
161        async_std::io::WriteExt::write(&mut self.0, buf).await
162    }
163
164    #[inline]
165    async fn flush(&mut self) -> Result<()> {
166        async_std::io::WriteExt::flush(&mut self.0).await
167    }
168}
169
170pub struct TcpListener(async_std::net::TcpListener);
171
172impl TcpListener {
173    #[inline]
174    pub async fn accept(&self) -> Result<(TcpStream, SocketAddr)> {
175        self.0
176            .accept()
177            .await
178            .map(|(stream, addr)| (TcpStream(stream), addr))
179    }
180
181    #[inline]
182    pub async fn bind(addr: impl crate::net::ToSocketAddrs) -> Result<Self> {
183        async_std::net::TcpListener::bind(addr)
184            .await
185            .map(TcpListener)
186    }
187
188    #[inline]
189    pub fn local_addr(&self) -> Result<SocketAddr> {
190        self.0.local_addr()
191    }
192}
193
194pub struct UdpSocket(async_std::net::UdpSocket);
195
196impl UdpSocket {
197    #[inline]
198    pub async fn bind(addr: impl crate::net::ToSocketAddrs) -> Result<UdpSocket> {
199        async_std::net::UdpSocket::bind(addr).await.map(UdpSocket)
200    }
201
202    #[inline]
203    pub fn broadcast(&self) -> Result<bool> {
204        self.0.broadcast()
205    }
206
207    #[inline]
208    pub async fn connect(&self, addr: impl crate::net::ToSocketAddrs) -> Result<()> {
209        self.0.connect(addr).await
210    }
211
212    #[inline]
213    pub fn join_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> Result<()> {
214        self.0.join_multicast_v4(*multiaddr, *interface)
215    }
216
217    #[inline]
218    pub fn join_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> Result<()> {
219        self.0.join_multicast_v6(multiaddr, interface)
220    }
221
222    #[inline]
223    pub fn leave_multicast_v4(&self, multiaddr: &Ipv4Addr, interface: &Ipv4Addr) -> Result<()> {
224        self.0.leave_multicast_v4(*multiaddr, *interface)
225    }
226
227    #[inline]
228    pub fn leave_multicast_v6(&self, multiaddr: &Ipv6Addr, interface: u32) -> Result<()> {
229        self.0.leave_multicast_v6(multiaddr, interface)
230    }
231
232    #[inline]
233    pub fn local_addr(&self) -> Result<SocketAddr> {
234        self.0.local_addr()
235    }
236
237    #[inline]
238    pub fn multicast_loop_v4(&self) -> Result<bool> {
239        self.0.multicast_loop_v4()
240    }
241
242    #[inline]
243    pub fn multicast_loop_v6(&self) -> Result<bool> {
244        self.0.multicast_loop_v6()
245    }
246
247    #[inline]
248    pub fn multicast_ttl_v4(&self) -> Result<u32> {
249        self.0.multicast_ttl_v4()
250    }
251
252    #[inline]
253    pub async fn peek_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
254        self.0.peek_from(buf).await
255    }
256
257    #[inline]
258    pub fn peer_addr(&self) -> Result<SocketAddr> {
259        self.0.peer_addr()
260    }
261
262    #[inline]
263    pub async fn recv(&self, buf: &mut [u8]) -> Result<usize> {
264        self.0.recv(buf).await
265    }
266
267    #[inline]
268    pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
269        self.0.recv_from(buf).await
270    }
271
272    #[inline]
273    pub async fn send(&self, buf: &[u8]) -> Result<usize> {
274        self.0.send(buf).await
275    }
276
277    #[inline]
278    pub async fn send_to(
279        &self, buf: &[u8], target: impl crate::net::ToSocketAddrs,
280    ) -> Result<usize> {
281        self.0.send_to(buf, target).await
282    }
283
284    #[inline]
285    pub fn set_broadcast(&self, on: bool) -> Result<()> {
286        self.0.set_broadcast(on)
287    }
288
289    #[inline]
290    pub fn set_multicast_loop_v4(&self, on: bool) -> Result<()> {
291        self.0.set_multicast_loop_v4(on)
292    }
293
294    #[inline]
295    pub fn set_multicast_loop_v6(&self, on: bool) -> Result<()> {
296        self.0.set_multicast_loop_v6(on)
297    }
298
299    #[inline]
300    pub fn set_multicast_ttl_v4(&self, ttl: u32) -> Result<()> {
301        self.0.set_multicast_ttl_v4(ttl)
302    }
303
304    #[inline]
305    pub fn set_ttl(&self, ttl: u32) -> Result<()> {
306        self.0.set_ttl(ttl)
307    }
308
309    #[inline]
310    pub fn ttl(&self) -> Result<u32> {
311        self.0.ttl()
312    }
313}