pub trait FileSystem {
type DirEntry: DirEntry;
type ReadDir: ReadDir<Self::DirEntry>;
Show 21 methods
// Required methods
fn current_dir(&self) -> Result<PathBuf>;
fn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>;
fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool;
fn is_file<P: AsRef<Path>>(&self, path: P) -> bool;
fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>;
fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()>;
fn remove_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>;
fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()>;
fn read_dir<P: AsRef<Path>>(&self, path: P) -> Result<Self::ReadDir>;
fn create_file<P, B>(&self, path: P, buf: B) -> Result<()>
where P: AsRef<Path>,
B: AsRef<[u8]>;
fn write_file<P, B>(&self, path: P, buf: B) -> Result<()>
where P: AsRef<Path>,
B: AsRef<[u8]>;
fn overwrite_file<P, B>(&self, path: P, buf: B) -> Result<()>
where P: AsRef<Path>,
B: AsRef<[u8]>;
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>>;
fn read_file_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String>;
fn read_file_into<P, B>(&self, path: P, buf: B) -> Result<usize>
where P: AsRef<Path>,
B: AsMut<Vec<u8>>;
fn remove_file<P: AsRef<Path>>(&self, path: P) -> Result<()>;
fn copy_file<P, Q>(&self, from: P, to: Q) -> Result<()>
where P: AsRef<Path>,
Q: AsRef<Path>;
fn rename<P, Q>(&self, from: P, to: Q) -> Result<()>
where P: AsRef<Path>,
Q: AsRef<Path>;
fn readonly<P: AsRef<Path>>(&self, path: P) -> Result<bool>;
fn set_readonly<P: AsRef<Path>>(
&self,
path: P,
readonly: bool,
) -> Result<()>;
fn len<P: AsRef<Path>>(&self, path: P) -> u64;
}
Expand description
Provides standard file system operations.
Required Associated Types§
Required Methods§
Sourcefn current_dir(&self) -> Result<PathBuf>
fn current_dir(&self) -> Result<PathBuf>
Returns the current working directory.
This is based on std::env::current_dir
.
Sourcefn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
fn set_current_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
Updates the current working directory.
This is based on std::env::set_current_dir
.
Sourcefn is_dir<P: AsRef<Path>>(&self, path: P) -> bool
fn is_dir<P: AsRef<Path>>(&self, path: P) -> bool
Determines whether the path exists and points to a directory.
Sourcefn is_file<P: AsRef<Path>>(&self, path: P) -> bool
fn is_file<P: AsRef<Path>>(&self, path: P) -> bool
Determines whether the path exists and points to a file.
Sourcefn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
fn create_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
Creates a new directory.
This is based on std::fs::create_dir
.
Sourcefn create_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()>
fn create_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()>
Recursively creates a directory and any missing parents.
This is based on std::fs::create_dir
.
Sourcefn remove_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
fn remove_dir<P: AsRef<Path>>(&self, path: P) -> Result<()>
Removes an empty directory.
This is based on std::fs::remove_dir
.
Sourcefn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()>
fn remove_dir_all<P: AsRef<Path>>(&self, path: P) -> Result<()>
Removes a directory and any child files or directories.
This is based on std::fs::remove_dir_all
.
Sourcefn read_dir<P: AsRef<Path>>(&self, path: P) -> Result<Self::ReadDir>
fn read_dir<P: AsRef<Path>>(&self, path: P) -> Result<Self::ReadDir>
Returns an iterator over the entries in a directory.
This is based on std::fs::read_dir
.
Sourcefn create_file<P, B>(&self, path: P, buf: B) -> Result<()>
fn create_file<P, B>(&self, path: P, buf: B) -> Result<()>
Writes buf
to a new file at path
.
§Errors
- A file or directory already exists at
path
. - The parent directory of
path
does not exist. - Current user has insufficient permissions.
Sourcefn write_file<P, B>(&self, path: P, buf: B) -> Result<()>
fn write_file<P, B>(&self, path: P, buf: B) -> Result<()>
Writes buf
to a new or existing file at buf
.
This will overwrite any contents that already exist.
§Errors
- The parent directory of
path
does not exist. - Current user has insufficient permissions.
Sourcefn overwrite_file<P, B>(&self, path: P, buf: B) -> Result<()>
fn overwrite_file<P, B>(&self, path: P, buf: B) -> Result<()>
Writes buf
to an existing file at buf
.
This will overwrite any contents that already exist.
§Errors
- No file
file
does not exist. - The node at
file
is a directory. - Current user has insufficient permissions.
Sourcefn read_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>>
fn read_file<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>>
Returns the contents of path
.
§Errors
path
does not exist.path
is a directory.- Current user has insufficient permissions.
Sourcefn read_file_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String>
fn read_file_to_string<P: AsRef<Path>>(&self, path: P) -> Result<String>
Returns the contents of path
as a string.
§Errors
path
does not exist.path
is a directory.- Current user has insufficient permissions.
- Contents are not valid UTF-8
Sourcefn read_file_into<P, B>(&self, path: P, buf: B) -> Result<usize>
fn read_file_into<P, B>(&self, path: P, buf: B) -> Result<usize>
Writes the contents of path
into the buffer. If successful, returns
the number of bytes that were read.
§Errors
path
does not exist.path
is a directory.- Current user has insufficient permissions.
Sourcefn remove_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
fn remove_file<P: AsRef<Path>>(&self, path: P) -> Result<()>
Removes the file at path
.
This is based on std::fs::remove_file
.
Sourcefn copy_file<P, Q>(&self, from: P, to: Q) -> Result<()>
fn copy_file<P, Q>(&self, from: P, to: Q) -> Result<()>
Copies the file at path from
to the path to
.
This is based on std::fs::copy
.
Sourcefn rename<P, Q>(&self, from: P, to: Q) -> Result<()>
fn rename<P, Q>(&self, from: P, to: Q) -> Result<()>
Renames a file or directory.
If both from
and to
are files, to
will be replaced.
Based on std::fs::rename
.
Sourcefn readonly<P: AsRef<Path>>(&self, path: P) -> Result<bool>
fn readonly<P: AsRef<Path>>(&self, path: P) -> Result<bool>
Returns true
if path
is a readonly file.
§Errors
path
does not exist.- Current user has insufficient permissions.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.