1 use crate::Stream; 2 use std::io; 3 use std::pin::Pin; 4 use std::task::{Context, Poll}; 5 use tokio::fs::{DirEntry, ReadDir}; 6 7 /// A wrapper around [`tokio::fs::ReadDir`] that implements [`Stream`]. 8 /// 9 /// [`tokio::fs::ReadDir`]: struct@tokio::fs::ReadDir 10 /// [`Stream`]: trait@crate::Stream 11 #[derive(Debug)] 12 #[cfg_attr(docsrs, doc(cfg(feature = "fs")))] 13 pub struct ReadDirStream { 14 inner: ReadDir, 15 } 16 17 impl ReadDirStream { 18 /// Create a new `ReadDirStream`. new(read_dir: ReadDir) -> Self19 pub fn new(read_dir: ReadDir) -> Self { 20 Self { inner: read_dir } 21 } 22 23 /// Get back the inner `ReadDir`. into_inner(self) -> ReadDir24 pub fn into_inner(self) -> ReadDir { 25 self.inner 26 } 27 } 28 29 impl Stream for ReadDirStream { 30 type Item = io::Result<DirEntry>; 31 poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>32 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { 33 self.inner.poll_next_entry(cx).map(Result::transpose) 34 } 35 } 36 37 impl AsRef<ReadDir> for ReadDirStream { as_ref(&self) -> &ReadDir38 fn as_ref(&self) -> &ReadDir { 39 &self.inner 40 } 41 } 42 43 impl AsMut<ReadDir> for ReadDirStream { as_mut(&mut self) -> &mut ReadDir44 fn as_mut(&mut self) -> &mut ReadDir { 45 &mut self.inner 46 } 47 } 48