Struct FileType

struct FileType(_)

A structure representing a type of file with accessors for each file type. It is returned by Metadata::file_type method.

Implementations

impl FileType

fn is_dir(self: &Self) -> bool

Tests whether this file type represents a directory. The result is mutually exclusive to the results of is_file and is_symlink; only zero or one of these tests may pass.

Examples

fn main() -> std::io::Result<()> {
    use std::fs;

    let metadata = fs::metadata("foo.txt")?;
    let file_type = metadata.file_type();

    assert_eq!(file_type.is_dir(), false);
    Ok(())
}
fn is_file(self: &Self) -> bool

Tests whether this file type represents a regular file. The result is mutually exclusive to the results of is_dir and is_symlink; only zero or one of these tests may pass.

When the goal is simply to read from (or write to) the source, the most reliable way to test the source can be read (or written to) is to open it. Only using is_file can break workflows like diff <( prog_a ) on a Unix-like system for example. See File::open or OpenOptions::open for more information.

Examples

fn main() -> std::io::Result<()> {
    use std::fs;

    let metadata = fs::metadata("foo.txt")?;
    let file_type = metadata.file_type();

    assert_eq!(file_type.is_file(), true);
    Ok(())
}
fn is_symlink(self: &Self) -> bool

Tests whether this file type represents a symbolic link. The result is mutually exclusive to the results of is_dir and is_file; only zero or one of these tests may pass.

The underlying Metadata struct needs to be retrieved with the fs::symlink_metadata function and not the fs::metadata function. The fs::metadata function follows symbolic links, so is_symlink would always return false for the target file.

Examples

use std::fs;

fn main() -> std::io::Result<()> {
    let metadata = fs::symlink_metadata("foo.txt")?;
    let file_type = metadata.file_type();

    assert_eq!(file_type.is_symlink(), false);
    Ok(())
}

impl Clone for FileType

fn clone(self: &Self) -> FileType

impl Copy for FileType

impl Debug for FileType

fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result

impl Eq for FileType

impl FileTypeExt for fs::FileType

fn is_symlink_dir(self: &Self) -> bool
fn is_symlink_file(self: &Self) -> bool

impl FileTypeExt for fs::FileType

fn is_block_device(self: &Self) -> bool
fn is_char_device(self: &Self) -> bool
fn is_fifo(self: &Self) -> bool
fn is_socket(self: &Self) -> bool

impl FileTypeExt for fs::FileType

fn is_block_device(self: &Self) -> bool
fn is_char_device(self: &Self) -> bool
fn is_socket(self: &Self) -> bool

impl Freeze for FileType

impl Hash for FileType

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl PartialEq for FileType

fn eq(self: &Self, other: &FileType) -> bool

impl RefUnwindSafe for FileType

impl Send for FileType

impl StructuralPartialEq for FileType

impl Sync for FileType

impl Unpin for FileType

impl UnwindSafe for FileType

impl<T> Any for FileType

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for FileType

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for FileType

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for FileType

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for FileType

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for FileType

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for FileType

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for FileType

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for FileType

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>