cap_async_std/net/
incoming.rs

1use crate::net::TcpStream;
2use async_std::stream::Stream;
3use async_std::task::{Context, Poll};
4use async_std::{io, net};
5use std::fmt;
6use std::pin::Pin;
7
8/// An iterator that infinitely `accept`s connections on a [`TcpListener`].
9///
10/// This corresponds to [`async_std::net::Incoming`].
11///
12/// [`TcpListener`]: struct.TcpListener.html
13pub struct Incoming<'a> {
14    std: net::Incoming<'a>,
15}
16
17impl<'a> Incoming<'a> {
18    /// Constructs a new instance of `Self` from the given
19    /// `async_std::net::Incoming`.
20    ///
21    /// This grants access the resources the `async_std::net::Incoming`
22    /// instance already has access to.
23    #[inline]
24    pub fn from_std(std: net::Incoming<'a>) -> Self {
25        Self { std }
26    }
27}
28
29impl<'a> Stream for Incoming<'a> {
30    type Item = io::Result<TcpStream>;
31
32    #[inline]
33    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
34        Stream::poll_next(Pin::new(&mut self.std), cx).map(|poll| {
35            poll.map(|result| {
36                let tcp_stream = result?;
37                Ok(TcpStream::from_std(tcp_stream))
38            })
39        })
40    }
41
42    #[inline]
43    fn size_hint(&self) -> (usize, Option<usize>) {
44        self.std.size_hint()
45    }
46}
47
48impl<'a> fmt::Debug for Incoming<'a> {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        self.std.fmt(f)
51    }
52}