tokio_stream/wrappers/
read_dir.rs

1use crate::Stream;
2use std::io;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5use 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")))]
13pub struct ReadDirStream {
14    inner: ReadDir,
15}
16
17impl ReadDirStream {
18    /// Create a new `ReadDirStream`.
19    pub fn new(read_dir: ReadDir) -> Self {
20        Self { inner: read_dir }
21    }
22
23    /// Get back the inner `ReadDir`.
24    pub fn into_inner(self) -> ReadDir {
25        self.inner
26    }
27}
28
29impl Stream for ReadDirStream {
30    type Item = io::Result<DirEntry>;
31
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
37impl AsRef<ReadDir> for ReadDirStream {
38    fn as_ref(&self) -> &ReadDir {
39        &self.inner
40    }
41}
42
43impl AsMut<ReadDir> for ReadDirStream {
44    fn as_mut(&mut self) -> &mut ReadDir {
45        &mut self.inner
46    }
47}