Struct nix::dir::Dir

source ·
pub struct Dir(/* private fields */);
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§

source§

impl Dir

source

pub fn open<P: ?Sized + NixPath>( path: &P, oflag: OFlag, mode: Mode ) -> Result<Self>

Opens the given path as with fcntl::open.

source

pub fn openat<P: ?Sized + NixPath>( dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode ) -> Result<Self>

Opens the given path as with fcntl::openat.

source

pub fn from<F: IntoRawFd>(fd: F) -> Result<Self>

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

source

pub fn from_fd(fd: RawFd) -> Result<Self>

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

source

pub fn iter(&mut self) -> Iter<'_>

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

Trait Implementations§

source§

impl AsRawFd for Dir

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for Dir

source§

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

Formats the value using the given formatter. Read more
source§

impl Drop for Dir

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Hash for Dir

source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl IntoIterator for Dir

source§

fn into_iter(self) -> Self::IntoIter

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

type Item = Result<Entry, Errno>

The type of the elements being iterated over.
§

type IntoIter = OwningIter

Which kind of iterator are we turning this into?
source§

impl PartialEq for Dir

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Dir

source§

impl Send for Dir

source§

impl StructuralPartialEq for Dir

Auto Trait Implementations§

§

impl Freeze for Dir

§

impl RefUnwindSafe for Dir

§

impl !Sync for Dir

§

impl Unpin for Dir

§

impl UnwindSafe for Dir

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.