pub trait FileSystem {
// Required methods
fn read_to_string(&self, path: &Path) -> Result<String>;
fn write_all(&self, path: &Path, contents: &[u8]) -> Result<()>;
fn create_dir_all(&self, path: &Path) -> Result<()>;
fn remove_file(&self, path: &Path) -> Result<()>;
fn remove_dir_all(&self, path: &Path) -> Result<()>;
fn exists(&self, path: &Path) -> bool;
fn is_dir(&self, path: &Path) -> bool;
fn is_file(&self, path: &Path) -> bool;
fn canonicalize(&self, path: &Path) -> Result<PathBuf>;
fn read_dir(&self, path: &Path) -> Result<Vec<PathBuf>>;
// Provided method
fn atomic_write(&self, path: &Path, contents: &[u8]) -> Result<()> { ... }
}Required Methods§
Sourcefn read_to_string(&self, path: &Path) -> Result<String>
fn read_to_string(&self, path: &Path) -> Result<String>
Reads the entire contents of a file into a string.
§Errors
Returns an error if the file does not exist, cannot be read, or contains invalid UTF-8.
Sourcefn write_all(&self, path: &Path, contents: &[u8]) -> Result<()>
fn write_all(&self, path: &Path, contents: &[u8]) -> Result<()>
Writes a slice of bytes to a file, creating the file if it does not exist.
§Errors
Returns an error if the file cannot be written to or created.
Sourcefn create_dir_all(&self, path: &Path) -> Result<()>
fn create_dir_all(&self, path: &Path) -> Result<()>
Creates a directory and all of its parent components if they are missing.
§Errors
Returns an error if the directory cannot be created.
Sourcefn remove_file(&self, path: &Path) -> Result<()>
fn remove_file(&self, path: &Path) -> Result<()>
Removes a file from the filesystem.
§Errors
Returns an error if the file does not exist or cannot be removed.
Sourcefn remove_dir_all(&self, path: &Path) -> Result<()>
fn remove_dir_all(&self, path: &Path) -> Result<()>
Removes a directory and all of its contents.
§Errors
Returns an error if the directory does not exist or cannot be removed.
Sourcefn is_dir(&self, path: &Path) -> bool
fn is_dir(&self, path: &Path) -> bool
Returns true if the path exists and is pointing at a directory.
Sourcefn is_file(&self, path: &Path) -> bool
fn is_file(&self, path: &Path) -> bool
Returns true if the path exists and is pointing at a regular file.
Provided Methods§
Sourcefn atomic_write(&self, path: &Path, contents: &[u8]) -> Result<()>
fn atomic_write(&self, path: &Path, contents: &[u8]) -> Result<()>
Writes data to a file atomically using a temp-file + rename strategy.
The default implementation delegates to crate::atomic::atomic_write_sync.
Test doubles may override this to use a simpler (non-atomic) write.
§Errors
Returns an error if the write or rename fails.