Skip to content

Commit 7d6d3fa

Browse files
authored
Add convenient is_ functions to FileType (#1388)
1 parent 62ce4df commit 7d6d3fa

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/fs/constants.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@ use crate::backend;
55
pub use crate::timespec::{Nsecs, Secs, Timespec};
66
pub use backend::fs::types::*;
77

8+
impl FileType {
9+
/// Returns `true` if this `FileType` is a regular file.
10+
pub fn is_file(self) -> bool {
11+
self == Self::RegularFile
12+
}
13+
14+
/// Returns `true` if this `FileType` is a directory.
15+
pub fn is_dir(self) -> bool {
16+
self == Self::Directory
17+
}
18+
19+
/// Returns `true` if this `FileType` is a symlink.
20+
pub fn is_symlink(self) -> bool {
21+
self == Self::Symlink
22+
}
23+
24+
/// Returns `true` if this `FileType` is a fifo.
25+
#[cfg(not(target_os = "wasi"))]
26+
pub fn is_fifo(self) -> bool {
27+
self == Self::Fifo
28+
}
29+
30+
/// Returns `true` if this `FileType` is a socket.
31+
#[cfg(not(target_os = "wasi"))]
32+
pub fn is_socket(self) -> bool {
33+
self == Self::Socket
34+
}
35+
36+
/// Returns `true` if this `FileType` is a character device.
37+
pub fn is_char_device(self) -> bool {
38+
self == Self::CharacterDevice
39+
}
40+
41+
/// Returns `true` if this `FileType` is a block device.
42+
pub fn is_block_device(self) -> bool {
43+
self == Self::BlockDevice
44+
}
45+
}
46+
847
#[cfg(test)]
948
#[allow(unused_imports)]
1049
#[allow(unsafe_code)]

0 commit comments

Comments
 (0)