pub trait File:
Debug
+ Read
+ Seek
+ Write
+ Sized {
type Metadata: Metadata;
type Permissions: Permissions;
// Required methods
fn sync_all(&self) -> Result<()>;
fn sync_data(&self) -> Result<()>;
fn set_len(&self, size: u64) -> Result<()>;
fn metadata(&self) -> Result<Self::Metadata>;
fn try_clone(&self) -> Result<Self>;
fn set_permissions(&self, perm: Self::Permissions) -> Result<()>;
}
Expand description
A reference to an open file on the filesystem.
An instance of File
can be read or written to depending on the options it was opened with.
Files also implement Seek
to alter the logical cursor position of the internal file.
Files are automatically closed when they go out of scope.
§Examples
Create a new file and write bytes to it:
use rsfs::*;
use rsfs::mem::FS;
use std::io::prelude::*;
let fs = FS::new();
let mut file = fs.create_file("foo.txt")?;
file.write_all(b"Hello, original std::fs examples!")?;
Read the contents of a file into a String
:
use rsfs::*;
use rsfs::mem::FS;
use std::io::prelude::*;
let fs = FS::new();
let mut file = fs.open_file("foo.txt")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
assert_eq!(contents, "Hello, original std::fs examples!");
It can be more efficient to read the contents of a file with a buffered
Read
er. This can be accomplished with BufReader<R>
:
use rsfs::*;
use rsfs::mem::FS;
use std::io::BufReader;
use std::io::prelude::*;
let fs = FS::new();
let file = fs.open_file("foo.txt")?;
let mut buf_reader = BufReader::new(file);
let mut contents = String::new();
buf_reader.read_to_string(&mut contents)?;
assert_eq!(contents, "Hello, world!");
Required Associated Types§
Sourcetype Permissions: Permissions
type Permissions: Permissions
The Permissions
type in the same module implementing this trait.
Required Methods§
Sourcefn sync_all(&self) -> Result<()>
fn sync_all(&self) -> Result<()>
Attempts to sync all OS-internal metadata to the filesystem.
This function will attempt to ensure all in-memory data reaches the filesystem before returning.
§Examples
use rsfs::*;
use rsfs::mem::FS;
use std::io::prelude::*;
let fs = FS::new();
let mut f = fs.create_file("foo.txt")?;
f.write_all(b"Hello, original std::fs examples!")?;
f.sync_all()?;
Sourcefn sync_data(&self) -> Result<()>
fn sync_data(&self) -> Result<()>
This function is similar to sync_all
, except that it may not synchronize metadata to
the filesystem.
This is intended for uses that must synchronize content, but do not need the metadata. The
goal of this method is to reduce filesystem operations, but note that some platforms may
implement this as sync_all
.
§Examples
use rsfs::*;
use rsfs::mem::FS;
use std::io::prelude::*;
let fs = FS::new();
let mut f = fs.create_file("foo.txt")?;
f.write_all(b"Hello, original std::fs examples!")?;
f.sync_data()?;
Sourcefn set_len(&self, size: u64) -> Result<()>
fn set_len(&self, size: u64) -> Result<()>
Truncates or extends the underlying file, updating the size of this file to become size
.
If size
is less than the file’s current size, the file will be shrunk. If greater, the
file will be zero extended to size
.
§Errors
This function will return an error if the file is not opened for writing.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let mut f = fs.create_file("foo.txt")?;
f.set_len(10)?;
Sourcefn metadata(&self) -> Result<Self::Metadata>
fn metadata(&self) -> Result<Self::Metadata>
Queries information about the underlying file.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.open_file("foo.txt")?;
let metadata = file.metadata()?;
Sourcefn try_clone(&self) -> Result<Self>
fn try_clone(&self) -> Result<Self>
Generates a new, independently owned handle to the underlying file.
The returned File
is a reference to the same file cursor that this File
reference. Both
handles will read and write with the same file cursor.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.open_file("foo.txt")?;
let file_copy = file.try_clone()?;
Sourcefn set_permissions(&self, perm: Self::Permissions) -> Result<()>
fn set_permissions(&self, perm: Self::Permissions) -> Result<()>
Changes the permissions on the underlying file.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.open_file("foo.txt")?;
let mut perms = file.metadata()?.permissions();
perms.set_readonly(true);
file.set_permissions(perms)?;
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.