Debian Codegraph @

third_party/rust/tokio-stream/src/wrappers/read_dir.rs

raw
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`.
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
29 impl 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
37 impl AsRef<ReadDir> for ReadDirStream {
38 fn as_ref(&self) -> &ReadDir {
39 &self.inner
40 }
41 }
42
43 impl AsMut<ReadDir> for ReadDirStream {
44 fn as_mut(&mut self) -> &mut ReadDir {
45 &mut self.inner
46 }
47 }