Struct FileType

pub struct FileType(pub(in ::fs) FileType);

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

Fields

0: FileType

Implementations

impl FileType

fn is_dir(&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) -> 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(())
}

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(())
}

Trait Implementations

impl AsInner<FileType> for FileType

fn as_inner(&self) -> &FileType

impl Clone for FileType

fn clone(&self) -> FileType

impl Copy for FileType

impl Debug for FileType

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

impl Eq for FileType

fn assert_fields_are_eq(&self)

impl FileTypeExt for FileType

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

impl FileTypeExt for FileType

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

impl FileTypeExt for FileType

impl Hash for FileType

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

impl PartialEq for FileType

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

impl StructuralPartialEq for FileType

impl TrivialClone for FileType

Auto Trait Implementations

impl Freeze for FileType

impl RefUnwindSafe for FileType

impl Send for FileType

impl Sync for FileType

impl Unpin for FileType

impl UnsafeUnpin for FileType

impl UnwindSafe for FileType

Blanket Implementations

impl<T> Any for FileType where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for FileType where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for FileType where T: ?Sized,

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

impl<T> CloneToUninit for FileType where T: Clone,

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

impl<T> From<T> for FileType

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Printable for FileType where T: Copy + Debug,

impl<T> SizeHint for FileType where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for FileType

impl<T> ToOwned for FileType where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for FileType where U: From<T>,

fn into(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<U> for FileType where U: Into<T>,

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

impl<T, U> TryInto<U> for FileType where U: TryFrom<T>,

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