maybe_fut/api/fs/
dir_entry.rs

1use std::fs::FileType;
2
3use crate::maybe_fut_method;
4
5/// Entries returned by the [`super::ReadDir`] stream.
6///
7/// An instance of DirEntry represents an entry inside of a directory on the filesystem. Each entry can be inspected via methods to learn about the full path or possibly other metadata through per-platform extension traits.
8#[derive(Debug, Unwrap)]
9#[unwrap_types(
10    std(std::fs::DirEntry),
11    tokio(tokio::fs::DirEntry),
12    tokio_gated("tokio-fs")
13)]
14pub struct DirEntry(DirEntryInner);
15
16#[derive(Debug)]
17#[allow(clippy::large_enum_variant)]
18enum DirEntryInner {
19    /// Std variant of file <https://docs.rs/rustc-std-workspace-std/latest/std/fs/struct.DirEntry.html>
20    Std(std::fs::DirEntry),
21    #[cfg(tokio_fs)]
22    #[cfg_attr(docsrs, doc(cfg(feature = "tokio-fs")))]
23    /// Tokio variant of file <https://docs.rs/tokio/latest/tokio/fs/struct.DirEntry.html>
24    Tokio(tokio::fs::DirEntry),
25}
26
27impl From<std::fs::DirEntry> for DirEntry {
28    fn from(inner: std::fs::DirEntry) -> Self {
29        Self(DirEntryInner::Std(inner))
30    }
31}
32
33#[cfg(tokio_fs)]
34#[cfg_attr(docsrs, doc(cfg(feature = "tokio-fs")))]
35impl From<tokio::fs::DirEntry> for DirEntry {
36    fn from(inner: tokio::fs::DirEntry) -> Self {
37        Self(DirEntryInner::Tokio(inner))
38    }
39}
40
41impl DirEntry {
42    /// Returns the file name of this entry.
43    ///
44    /// This is the last component of the path, which may be a file name or a directory name.
45    pub fn file_name(&self) -> std::ffi::OsString {
46        match &self.0 {
47            DirEntryInner::Std(inner) => inner.file_name(),
48            #[cfg(tokio_fs)]
49            DirEntryInner::Tokio(inner) => inner.file_name(),
50        }
51    }
52
53    maybe_fut_method!(
54        /// Returns the file type for the file that this entry points at.
55        ///
56        /// This function will not traverse symlinks if this entry points at a symlink.
57        ///
58        /// # Platform-specific behavior
59        ///
60        /// On Windows and most Unix platforms this function is free (no extra system calls needed),
61        /// but some Unix platforms may require the equivalent call to symlink_metadata to learn about the target file type.
62        file_type() -> std::io::Result<FileType>,
63        DirEntryInner::Std,
64        DirEntryInner::Tokio,
65        tokio_fs
66    );
67
68    #[cfg(unix)]
69    #[cfg_attr(docsrs, doc(cfg(unix)))]
70    /// Returns the underlying d_ino field in the contained dirent structure.
71    pub fn ino(&self) -> u64 {
72        use std::os::unix::fs::DirEntryExt as _;
73
74        match &self.0 {
75            DirEntryInner::Std(inner) => inner.ino(),
76            #[cfg(tokio_fs)]
77            DirEntryInner::Tokio(inner) => inner.ino(),
78        }
79    }
80
81    /// Returns the full path of this entry.
82    pub fn path(&self) -> std::path::PathBuf {
83        match &self.0 {
84            DirEntryInner::Std(inner) => inner.path(),
85            #[cfg(tokio_fs)]
86            DirEntryInner::Tokio(inner) => inner.path(),
87        }
88    }
89
90    maybe_fut_method!(
91        /// Returns the metadata for the file that this entry points at.
92        ///
93        /// This function will not traverse symlinks if this entry points at a symlink.
94        ///
95        /// # Platform-specific behavior
96        ///
97        /// On Windows and most Unix platforms this function is free (no extra system calls needed),
98        /// but some Unix platforms may require the equivalent call to symlink_metadata to learn about the target file type.
99        metadata() -> std::io::Result<std::fs::Metadata>,
100        DirEntryInner::Std,
101        DirEntryInner::Tokio,
102        tokio_fs
103    );
104}