Struct nix::dir::Dir

pub struct Dir(_);
Documentation

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

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

Opens the given path as with fcntl::open.

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

Opens the given path as with fcntl::openat.

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

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

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

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

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

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

Trait Implementations

impl AsRawFd for Dir
fn as_raw_fd(&Self) -> RawFd
impl Debug for Dir
fn fmt(&Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result
impl Drop for Dir
fn drop(&mut Self)
impl Eq for Dir
impl Hash for Dir
fn hash<__H: $crate::hash::Hasher>(&Self, state: &mut __H) -> ()
impl IntoIterator for Dir
type Item = Result<Entry, Errno>
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())
}
impl PartialEq<Dir> for Dir
fn eq(&Self, other: &Dir) -> bool
fn ne(&Self, other: &Dir) -> bool
impl StructuralEq for Dir

Auto Trait Implementations

impl !Sync for Dir
impl RefUnwindSafe for Dir
impl Send for Dir
impl Unpin for Dir
impl UnwindSafe for Dir

Blanket Implementations

impl<T> Any for T
where
    T: 'static + ?Sized,
fn type_id(&Self) -> TypeId
impl<T> Borrow<T> for T
where
    T: ?Sized,
fn borrow(&Self) -> &T
impl<T> BorrowMut<T> for T
where
    T: ?Sized,
fn borrow_mut(&mut Self) -> &mut T
impl<T> From<T> for T
fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into<U> for T
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 T
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 T
where
    U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn try_into(Self) -> Result<U, <U as TryFrom<T>>::Error>