Trait mfio_rt::DirHandle

source ·
pub trait DirHandle: Sized {
    type FileHandle: FileHandle;
    type OpenFileFuture<'a>: Future<Output = MfioResult<Self::FileHandle>> + 'a
       where Self: 'a;
    type PathFuture<'a>: Future<Output = MfioResult<PathBuf>> + 'a
       where Self: 'a;
    type OpenDirFuture<'a>: Future<Output = MfioResult<Self>> + 'a
       where Self: 'a;
    type ReadDir<'a>: Stream<Item = MfioResult<DirEntry>> + 'a
       where Self: 'a;
    type ReadDirFuture<'a>: Future<Output = MfioResult<Self::ReadDir<'a>>> + 'a
       where Self: 'a;
    type MetadataFuture<'a>: Future<Output = MfioResult<Metadata>> + 'a
       where Self: 'a;
    type OpFuture<'a>: Future<Output = MfioResult<()>> + 'a
       where Self: 'a;

    // Required methods
    fn path(&self) -> Self::PathFuture<'_>;
    fn read_dir(&self) -> Self::ReadDirFuture<'_>;
    fn open_file<'a, P: AsRef<Path> + ?Sized>(
        &'a self,
        path: &'a P,
        options: OpenOptions
    ) -> Self::OpenFileFuture<'a>;
    fn open_dir<'a, P: AsRef<Path> + ?Sized>(
        &'a self,
        path: &'a P
    ) -> Self::OpenDirFuture<'a>;
    fn metadata<'a, P: AsRef<Path> + ?Sized>(
        &'a self,
        path: &'a P
    ) -> Self::MetadataFuture<'a>;
    fn do_op<'a, P: AsRef<Path> + ?Sized>(
        &'a self,
        operation: DirOp<&'a P>
    ) -> Self::OpFuture<'a>;
}
Expand description

Represents a location in filesystem operations are performed from.

Directory handles may refer to fixed directory entries throughout time, even if said entry is unlinked from the filesystem. So long as the handle is held, it may be valid. However, this behavior is implementation-specific, and, for instance, NativeRt does not follow it, because directory handles are simply stored as paths, rather than dir FDs/handles.

Required Associated Types§

source

type FileHandle: FileHandle

source

type OpenFileFuture<'a>: Future<Output = MfioResult<Self::FileHandle>> + 'a where Self: 'a

source

type PathFuture<'a>: Future<Output = MfioResult<PathBuf>> + 'a where Self: 'a

source

type OpenDirFuture<'a>: Future<Output = MfioResult<Self>> + 'a where Self: 'a

source

type ReadDir<'a>: Stream<Item = MfioResult<DirEntry>> + 'a where Self: 'a

source

type ReadDirFuture<'a>: Future<Output = MfioResult<Self::ReadDir<'a>>> + 'a where Self: 'a

source

type MetadataFuture<'a>: Future<Output = MfioResult<Metadata>> + 'a where Self: 'a

source

type OpFuture<'a>: Future<Output = MfioResult<()>> + 'a where Self: 'a

Required Methods§

source

fn path(&self) -> Self::PathFuture<'_>

Gets the absolute path to this DirHandle.

Examples
use mfio_rt::{DirHandle, Fs};
// On no_std mfio_rt re-exports typed_path::UnixPath as Path
use mfio_rt::Path;

let dir = fs.current_dir();

let path = dir.path().await.unwrap();

assert_ne!(path, Path::new("/dev"));
source

fn read_dir(&self) -> Self::ReadDirFuture<'_>

Reads the directory contents.

This function returns a stream that can be used to list files and subdirectories within this dir. Any errors will be propagated through the stream.

Examples
use futures::StreamExt;
use mfio::error::Error;
use mfio_rt::{DirHandle, Fs};

let dir = fs.current_dir();

let mut entries = dir
    .read_dir()
    .await
    .unwrap()
    .filter_map(|res| async { res.ok() })
    .map(|res| res.name)
    .collect::<Vec<_>>()
    .await;

assert!(entries.contains(&"Cargo.toml".to_string()));

// The order in which `read_dir` returns entries is not guaranteed. If reproducible
// ordering is required the entries should be explicitly sorted.

entries.sort();

// The entries have now been sorted by their path.
source

fn open_file<'a, P: AsRef<Path> + ?Sized>( &'a self, path: &'a P, options: OpenOptions ) -> Self::OpenFileFuture<'a>

Opens a file.

This function accepts an absolute or relative path to a file for reading. If the path is relative, it is opened relative to this DirHandle.

Examples
use mfio::traits::IoRead;
use mfio_rt::{DirHandle, Fs, OpenOptions};

let dir = fs.current_dir();

let fh = dir
    .open_file("Cargo.toml", OpenOptions::new().read(true))
    .await
    .unwrap();

// Now you may do file operations, like reading it

let mut bytes = vec![];
fh.read_to_end(0, &mut bytes).await.unwrap();
let s = String::from_utf8(bytes).unwrap();

assert!(s.contains("mfio"));
source

fn open_dir<'a, P: AsRef<Path> + ?Sized>( &'a self, path: &'a P ) -> Self::OpenDirFuture<'a>

Opens a directory.

This function accepts an absolute or relative path to a directory for reading. If the path is relative, it is opened relative to this DirHandle.

Examples
use futures::StreamExt;
use mfio::traits::IoRead;
use mfio_rt::{DirHandle, Fs, OpenOptions};

let dir = fs.current_dir();

let subdir = dir.open_dir("src").await.unwrap();

assert_ne!(dir.path().await.unwrap(), subdir.path().await.unwrap());

// Now you may do directory operations, like listing it

let mut entries = subdir
    .read_dir()
    .await
    .unwrap()
    .filter_map(|res| async { res.ok() })
    .map(|res| res.name)
    .collect::<Vec<_>>()
    .await;

assert!(entries.contains(&"lib.rs".to_string()));
source

fn metadata<'a, P: AsRef<Path> + ?Sized>( &'a self, path: &'a P ) -> Self::MetadataFuture<'a>

Retrieves file metadata.

Examples
source

fn do_op<'a, P: AsRef<Path> + ?Sized>( &'a self, operation: DirOp<&'a P> ) -> Self::OpFuture<'a>

Do an operation.

This function performs an operation from the DirOp enum.

Examples

Object Safety§

This trait is not object safe.

Implementors§