cap_std/net/
incoming.rs

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