cap_async_std/os/unix/net/
incoming.rs

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