cap_std/os/unix/net/
incoming.rs

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