async-rt 0.2.0

A small library designed to utilize async executors through an common API while extending features.
Documentation
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use super::Network;
use futures::Stream;
use std::fmt::{Debug, Formatter};
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{Context, Poll};

/// A TCP socket that can be configured before connecting or listening.
pub struct TcpSocket {
    inner: TcpSocketInner,
}

enum TcpSocketInner {
    #[cfg(feature = "tokio")]
    Tokio(::tokio::net::TcpSocket),
    #[cfg(feature = "smol")]
    Smol(super::smol::SmolTcpSocket),
    #[cfg(feature = "compio")]
    Compio(::compio::net::TcpSocket),
}

impl TcpSocket {
    /// Creates a new IPv4 TCP socket.
    pub async fn new_v4() -> io::Result<Self> {
        super::current().new_tcp_socket_v4().await
    }

    /// Creates a new IPv6 TCP socket.
    pub async fn new_v6() -> io::Result<Self> {
        super::current().new_tcp_socket_v6().await
    }

    #[cfg(feature = "tokio")]
    pub(super) fn from_tokio(socket: ::tokio::net::TcpSocket) -> Self {
        Self {
            inner: TcpSocketInner::Tokio(socket),
        }
    }

    #[cfg(feature = "smol")]
    pub(super) fn from_smol(socket: super::smol::SmolTcpSocket) -> Self {
        Self {
            inner: TcpSocketInner::Smol(socket),
        }
    }

    #[cfg(feature = "compio")]
    pub(super) fn from_compio(socket: ::compio::net::TcpSocket) -> Self {
        Self {
            inner: TcpSocketInner::Compio(socket),
        }
    }

    /// Binds the socket to an address.
    pub async fn bind(&self, address: SocketAddr) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.bind(address),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.bind(address),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.bind(address).await,
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Connects the socket to a remote address.
    pub async fn connect(self, address: SocketAddr) -> io::Result<TcpStream> {
        match self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => {
                socket.connect(address).await.map(TcpStream::from_tokio)
            }
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.connect(address).await.map(TcpStream::from_smol),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => {
                socket.connect(address).await.map(TcpStream::from_compio)
            }
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Converts the socket into a TCP listener.
    pub async fn listen(self, backlog: u32) -> io::Result<TcpListener> {
        match self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.listen(backlog).map(TcpListener::from_tokio),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.listen(backlog).map(TcpListener::from_smol),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => {
                let backlog = i32::try_from(backlog).map_err(|_| {
                    io::Error::new(io::ErrorKind::InvalidInput, "backlog is too large")
                })?;
                socket.listen(backlog).await.map(TcpListener::from_compio)
            }
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns the local address of this socket.
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.local_addr(),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.local_addr(),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.local_addr(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns whether keepalive is enabled.
    pub fn keepalive(&self) -> io::Result<bool> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.keepalive(),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.keepalive(),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.keepalive(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Sets whether keepalive is enabled.
    pub fn set_keepalive(&self, keepalive: bool) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.set_keepalive(keepalive),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.set_keepalive(keepalive),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.set_keepalive(keepalive),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns whether address reuse is enabled.
    pub fn reuseaddr(&self) -> io::Result<bool> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.reuseaddr(),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.reuseaddr(),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.reuseaddr(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Sets whether address reuse is enabled.
    pub fn set_reuseaddr(&self, reuseaddr: bool) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.set_reuseaddr(reuseaddr),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.set_reuseaddr(reuseaddr),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.set_reuseaddr(reuseaddr),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns whether `TCP_NODELAY` is enabled.
    pub fn nodelay(&self) -> io::Result<bool> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.nodelay(),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.nodelay(),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.nodelay(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Sets the value of `TCP_NODELAY`.
    pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpSocketInner::Tokio(socket) => socket.set_nodelay(nodelay),
            #[cfg(feature = "smol")]
            TcpSocketInner::Smol(socket) => socket.set_nodelay(nodelay),
            #[cfg(feature = "compio")]
            TcpSocketInner::Compio(socket) => socket.set_nodelay(nodelay),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }
}

impl Debug for TcpSocket {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TcpSocket")
            .field("local_addr", &self.local_addr())
            .finish()
    }
}

/// A TCP stream using one of the built-in networking implementations.
pub struct TcpStream {
    pub(super) inner: TcpStreamInner,
}

pub(super) enum TcpStreamInner {
    #[cfg(feature = "tokio")]
    Tokio(::tokio::net::TcpStream),
    #[cfg(feature = "smol")]
    Smol(::smol::net::TcpStream),
    #[cfg(feature = "compio")]
    Compio(Pin<Box<::compio::io::compat::AsyncStream<::compio::net::TcpStream>>>),
}

impl TcpStream {
    /// Connects to a remote address.
    pub async fn connect(address: SocketAddr) -> io::Result<Self> {
        super::current().connect_tcp(address).await
    }

    #[cfg(feature = "tokio")]
    pub(super) fn from_tokio(stream: ::tokio::net::TcpStream) -> Self {
        Self {
            inner: TcpStreamInner::Tokio(stream),
        }
    }

    #[cfg(feature = "smol")]
    pub(super) fn from_smol(stream: ::smol::net::TcpStream) -> Self {
        Self {
            inner: TcpStreamInner::Smol(stream),
        }
    }

    #[cfg(feature = "compio")]
    pub(super) fn from_compio(stream: ::compio::net::TcpStream) -> Self {
        Self {
            inner: TcpStreamInner::Compio(Box::pin(::compio::io::compat::AsyncStream::new(stream))),
        }
    }

    /// Returns the local address of this stream.
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpStreamInner::Tokio(stream) => stream.local_addr(),
            #[cfg(feature = "smol")]
            TcpStreamInner::Smol(stream) => stream.local_addr(),
            #[cfg(feature = "compio")]
            TcpStreamInner::Compio(stream) => compio_stream(stream).local_addr(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns the remote address of this stream.
    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpStreamInner::Tokio(stream) => stream.peer_addr(),
            #[cfg(feature = "smol")]
            TcpStreamInner::Smol(stream) => stream.peer_addr(),
            #[cfg(feature = "compio")]
            TcpStreamInner::Compio(stream) => compio_stream(stream).peer_addr(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns whether `TCP_NODELAY` is enabled.
    pub fn nodelay(&self) -> io::Result<bool> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpStreamInner::Tokio(stream) => stream.nodelay(),
            #[cfg(feature = "smol")]
            TcpStreamInner::Smol(stream) => stream.nodelay(),
            #[cfg(feature = "compio")]
            TcpStreamInner::Compio(stream) => compio_stream(stream).nodelay(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Sets the value of `TCP_NODELAY`.
    pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpStreamInner::Tokio(stream) => stream.set_nodelay(nodelay),
            #[cfg(feature = "smol")]
            TcpStreamInner::Smol(stream) => stream.set_nodelay(nodelay),
            #[cfg(feature = "compio")]
            TcpStreamInner::Compio(stream) => compio_stream(stream).set_nodelay(nodelay),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns the time-to-live value used by this stream.
    pub fn ttl(&self) -> io::Result<u32> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpStreamInner::Tokio(stream) => stream.ttl(),
            #[cfg(feature = "smol")]
            TcpStreamInner::Smol(stream) => stream.ttl(),
            #[cfg(feature = "compio")]
            TcpStreamInner::Compio(stream) => compio_stream(stream).ttl_v4(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Sets the time-to-live value used by this stream.
    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpStreamInner::Tokio(stream) => stream.set_ttl(ttl),
            #[cfg(feature = "smol")]
            TcpStreamInner::Smol(stream) => stream.set_ttl(ttl),
            #[cfg(feature = "compio")]
            TcpStreamInner::Compio(stream) => compio_stream(stream).set_ttl_v4(ttl),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Reads bytes from the stream.
    pub async fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
        futures::io::AsyncReadExt::read(self, buffer).await
    }

    /// Reads enough bytes to fill the buffer.
    pub async fn read_exact(&mut self, buffer: &mut [u8]) -> io::Result<()> {
        futures::io::AsyncReadExt::read_exact(self, buffer).await
    }

    /// Writes bytes to the stream.
    pub async fn write(&mut self, buffer: &[u8]) -> io::Result<usize> {
        futures::io::AsyncWriteExt::write(self, buffer).await
    }

    /// Writes an entire buffer to the stream.
    pub async fn write_all(&mut self, buffer: &[u8]) -> io::Result<()> {
        futures::io::AsyncWriteExt::write_all(self, buffer).await
    }

    /// Flushes buffered data to the stream.
    pub async fn flush(&mut self) -> io::Result<()> {
        futures::io::AsyncWriteExt::flush(self).await
    }

    /// Shuts down the write side of the stream.
    pub async fn shutdown(&mut self) -> io::Result<()> {
        futures::io::AsyncWriteExt::close(self).await
    }
}

#[cfg(feature = "compio")]
fn compio_stream(
    stream: &Pin<Box<::compio::io::compat::AsyncStream<::compio::net::TcpStream>>>,
) -> &::compio::net::TcpStream {
    stream.as_ref().get_ref().get_ref().0
}

impl Debug for TcpStream {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TcpStream")
            .field("local_addr", &self.local_addr())
            .field("peer_addr", &self.peer_addr())
            .finish()
    }
}

/// A TCP listener using one of the built-in networking implementations.
pub struct TcpListener {
    inner: TcpListenerInner,
}

enum TcpListenerInner {
    #[cfg(feature = "tokio")]
    Tokio(::tokio::net::TcpListener),
    #[cfg(feature = "smol")]
    Smol(::smol::net::TcpListener),
    #[cfg(feature = "compio")]
    Compio(::compio::net::TcpListener),
}

impl TcpListener {
    /// Binds a listener to an address.
    pub async fn bind(address: SocketAddr) -> io::Result<Self> {
        super::current().bind_tcp(address).await
    }

    #[cfg(feature = "tokio")]
    pub(super) fn from_tokio(listener: ::tokio::net::TcpListener) -> Self {
        Self {
            inner: TcpListenerInner::Tokio(listener),
        }
    }

    #[cfg(feature = "smol")]
    pub(super) fn from_smol(listener: ::smol::net::TcpListener) -> Self {
        Self {
            inner: TcpListenerInner::Smol(listener),
        }
    }

    #[cfg(feature = "compio")]
    pub(super) fn from_compio(listener: ::compio::net::TcpListener) -> Self {
        Self {
            inner: TcpListenerInner::Compio(listener),
        }
    }

    /// Accepts a new connection.
    pub async fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpListenerInner::Tokio(listener) => {
                let (stream, address) = listener.accept().await?;
                Ok((TcpStream::from_tokio(stream), address))
            }
            #[cfg(feature = "smol")]
            TcpListenerInner::Smol(listener) => {
                let (stream, address) = listener.accept().await?;
                Ok((TcpStream::from_smol(stream), address))
            }
            #[cfg(feature = "compio")]
            TcpListenerInner::Compio(listener) => {
                let (stream, address) = listener.accept().await?;
                Ok((TcpStream::from_compio(stream), address))
            }
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns a stream of incoming connections.
    pub fn incoming(&self) -> Incoming<'_> {
        let inner = match &self.inner {
            #[cfg(feature = "tokio")]
            TcpListenerInner::Tokio(listener) => IncomingInner::Tokio(listener),
            #[cfg(feature = "smol")]
            TcpListenerInner::Smol(listener) => IncomingInner::Smol(listener.incoming()),
            #[cfg(feature = "compio")]
            TcpListenerInner::Compio(listener) => IncomingInner::Compio(listener.incoming()),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        };
        Incoming { inner }
    }

    /// Returns the local address of this listener.
    pub fn local_addr(&self) -> io::Result<SocketAddr> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpListenerInner::Tokio(listener) => listener.local_addr(),
            #[cfg(feature = "smol")]
            TcpListenerInner::Smol(listener) => listener.local_addr(),
            #[cfg(feature = "compio")]
            TcpListenerInner::Compio(listener) => listener.local_addr(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Returns the time-to-live value used by this listener.
    pub fn ttl(&self) -> io::Result<u32> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpListenerInner::Tokio(listener) => listener.ttl(),
            #[cfg(feature = "smol")]
            TcpListenerInner::Smol(listener) => listener.ttl(),
            #[cfg(feature = "compio")]
            TcpListenerInner::Compio(listener) => listener.ttl_v4(),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }

    /// Sets the time-to-live value used by this listener.
    pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
        match &self.inner {
            #[cfg(feature = "tokio")]
            TcpListenerInner::Tokio(listener) => listener.set_ttl(ttl),
            #[cfg(feature = "smol")]
            TcpListenerInner::Smol(listener) => listener.set_ttl(ttl),
            #[cfg(feature = "compio")]
            TcpListenerInner::Compio(listener) => listener.set_ttl_v4(ttl),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }
}

impl Debug for TcpListener {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TcpListener")
            .field("local_addr", &self.local_addr())
            .finish()
    }
}

/// A stream of incoming TCP connections.
pub struct Incoming<'a> {
    inner: IncomingInner<'a>,
}

enum IncomingInner<'a> {
    #[cfg(feature = "tokio")]
    Tokio(&'a ::tokio::net::TcpListener),
    #[cfg(feature = "smol")]
    Smol(::smol::net::Incoming<'a>),
    #[cfg(feature = "compio")]
    Compio(::compio::net::TcpIncoming<'a>),
}

impl Stream for Incoming<'_> {
    type Item = io::Result<TcpStream>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        match &mut self.get_mut().inner {
            #[cfg(feature = "tokio")]
            IncomingInner::Tokio(listener) => listener
                .poll_accept(cx)
                .map(|result| Some(result.map(|(stream, _)| TcpStream::from_tokio(stream)))),
            #[cfg(feature = "smol")]
            IncomingInner::Smol(incoming) => Pin::new(incoming)
                .poll_next(cx)
                .map(|item| item.map(|result| result.map(TcpStream::from_smol))),
            #[cfg(feature = "compio")]
            IncomingInner::Compio(incoming) => Pin::new(incoming)
                .poll_next(cx)
                .map(|item| item.map(|result| result.map(TcpStream::from_compio))),
            #[cfg(not(any(feature = "tokio", feature = "smol", feature = "compio")))]
            _ => unreachable!(),
        }
    }
}