1 use crate::Stream; 2 use std::io; 3 use std::pin::Pin; 4 use std::task::{Context, Poll}; 5 use tokio::net::{UnixListener, UnixStream}; 6 7 /// A wrapper around [`UnixListener`] that implements [`Stream`]. 8 /// 9 /// [`UnixListener`]: struct@tokio::net::UnixListener 10 /// [`Stream`]: trait@crate::Stream 11 #[derive(Debug)] 12 #[cfg_attr(docsrs, doc(cfg(all(unix, feature = "net"))))] 13 pub struct UnixListenerStream { 14 inner: UnixListener, 15 } 16 17 impl UnixListenerStream { 18 /// Create a new `UnixListenerStream`. new(listener: UnixListener) -> Self19 pub fn new(listener: UnixListener) -> Self { 20 Self { inner: listener } 21 } 22 23 /// Get back the inner `UnixListener`. into_inner(self) -> UnixListener24 pub fn into_inner(self) -> UnixListener { 25 self.inner 26 } 27 } 28 29 impl Stream for UnixListenerStream { 30 type Item = io::Result<UnixStream>; 31 poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<io::Result<UnixStream>>>32 fn poll_next( 33 self: Pin<&mut Self>, 34 cx: &mut Context<'_>, 35 ) -> Poll<Option<io::Result<UnixStream>>> { 36 match self.inner.poll_accept(cx) { 37 Poll::Ready(Ok((stream, _))) => Poll::Ready(Some(Ok(stream))), 38 Poll::Ready(Err(err)) => Poll::Ready(Some(Err(err))), 39 Poll::Pending => Poll::Pending, 40 } 41 } 42 } 43 44 impl AsRef<UnixListener> for UnixListenerStream { as_ref(&self) -> &UnixListener45 fn as_ref(&self) -> &UnixListener { 46 &self.inner 47 } 48 } 49 50 impl AsMut<UnixListener> for UnixListenerStream { as_mut(&mut self) -> &mut UnixListener51 fn as_mut(&mut self) -> &mut UnixListener { 52 &mut self.inner 53 } 54 } 55