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§
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§
sourcefn path(&self) -> Self::PathFuture<'_>
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"));
sourcefn read_dir(&self) -> Self::ReadDirFuture<'_>
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.
sourcefn open_file<'a, P: AsRef<Path> + ?Sized>(
&'a self,
path: &'a P,
options: OpenOptions
) -> Self::OpenFileFuture<'a>
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"));
sourcefn open_dir<'a, P: AsRef<Path> + ?Sized>(
&'a self,
path: &'a P
) -> Self::OpenDirFuture<'a>
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()));
Object Safety§
Implementors§
source§impl DirHandle for NativeRtDir
Available on crate feature native
only.
impl DirHandle for NativeRtDir
native
only.type FileHandle = Seekable<NativeFile, u64>
type OpenFileFuture<'a> = OpenFileFuture<'a>
type PathFuture<'a> = Ready<Result<PathBuf, Error>>
type OpenDirFuture<'a> = Ready<Result<NativeRtDir, Error>>
type ReadDir<'a> = ReadDir
type ReadDirFuture<'a> = Ready<Result<ReadDir, Error>>
type MetadataFuture<'a> = MetadataFuture
type OpFuture<'a> = OpFuture
source§impl DirHandle for VirtDir
Available on crate feature virt
only.
impl DirHandle for VirtDir
virt
only.