Struct nix::dir::Dir

source ·
pub struct Dir(_);
Available on crate feature dir only.
Expand description

An open directory.

This is a lower-level interface than std::fs::ReadDir. Notable differences:

  • can be opened from a file descriptor (as returned by openat, perhaps before knowing if the path represents a file or directory).
  • implements AsRawFd, so it can be passed to fstat, openat, etc. The file descriptor continues to be owned by the Dir, so callers must not keep a RawFd after the Dir is dropped.
  • can be iterated through multiple times without closing and reopening the file descriptor. Each iteration rewinds when finished.
  • returns entries for . (current directory) and .. (parent directory).
  • returns entries’ names as a CStr (no allocation or conversion beyond whatever libc does).

Implementations§

Opens the given path as with fcntl::open.

Opens the given path as with fcntl::openat.

Converts from a descriptor-based object, closing the descriptor on success or failure.

Converts from a file descriptor, closing it on success or failure.

Returns an iterator of Result<Entry> which rewinds when finished.

Trait Implementations§

Extracts the raw file descriptor. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Creates a owning iterator, that is, one that takes ownership of the Dir. The Dir cannot be used after calling this. This can be useful when you have a function that both creates a Dir instance and returns an Iterator.

Example:

use nix::{dir::Dir, fcntl::OFlag, sys::stat::Mode};
use std::{iter::Iterator, string::String};

fn ls_upper(dirname: &str) -> impl Iterator<Item=String> {
    let d = Dir::open(dirname, OFlag::O_DIRECTORY, Mode::S_IXUSR).unwrap();
    d.into_iter().map(|x| x.unwrap().file_name().as_ref().to_string_lossy().to_ascii_uppercase())
}
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.