pub trait OpenOptions: Clone + Debug {
type File: File;
// Required methods
fn read(&mut self, read: bool) -> &mut Self;
fn write(&mut self, write: bool) -> &mut Self;
fn append(&mut self, append: bool) -> &mut Self;
fn truncate(&mut self, truncate: bool) -> &mut Self;
fn create(&mut self, create: bool) -> &mut Self;
fn create_new(&mut self, create_new: bool) -> &mut Self;
fn open<P: AsRef<Path>>(&self, path: P) -> Result<Self::File>;
}
Expand description
Options and flags which can be used to configure how a file is opened.
This trait replaces [std::fs::OpenOption
] with the exception of its new
function. To create
a new OpenOptions
, use GenFS::new_openopts
.
§Examples
Opening a file to read:
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().read(true).open("foo.txt")?;
Opening a file for reading and writing and creating it if it does not exist:
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts()
.read(true)
.write(true)
.create(true)
.open("foo.txt")?;
Required Associated Types§
Required Methods§
Sourcefn read(&mut self, read: bool) -> &mut Self
fn read(&mut self, read: bool) -> &mut Self
Sets the option for read access.
This option, when true, indicates a file should be read
-able if opened.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().read(true).open("foo.txt")?;
Sourcefn write(&mut self, write: bool) -> &mut Self
fn write(&mut self, write: bool) -> &mut Self
Sets the option for write access.
This option, when true, indicates a file should be write
-able if opened.
If the file already exists, write calls will overwrite existing file contents.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().write(true).open("foo.txt")?;
Sourcefn append(&mut self, append: bool) -> &mut Self
fn append(&mut self, append: bool) -> &mut Self
Sets the option for append mode.
This option, when true, means the file will always append to the current end of the file as opposed to overwriting existing contents.
Note that .append(true)
implies .write(true)
.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().append(true).open("foo.txt")?;
Sourcefn truncate(&mut self, truncate: bool) -> &mut Self
fn truncate(&mut self, truncate: bool) -> &mut Self
Sets the option for truncating an existing file.
The file must be opened with write access for truncate to work.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().write(true).truncate(true).open("foo.txt")?;
Sourcefn create(&mut self, create: bool) -> &mut Self
fn create(&mut self, create: bool) -> &mut Self
Sets the option to create the file if it does not exist.
In order for a file to be created, write
or append
must also be used with this option.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().write(true).create(true).open("foo.txt")?;
Sourcefn create_new(&mut self, create_new: bool) -> &mut Self
fn create_new(&mut self, create_new: bool) -> &mut Self
Sets the option to always create a new file.
This option indicates whether a new file must be created, ensuring the file cannot exist already.
In order for a file to be created, write
or append
must also be used with this option.
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().write(true).create_new(true).open("foo.txt")?;
Sourcefn open<P: AsRef<Path>>(&self, path: P) -> Result<Self::File>
fn open<P: AsRef<Path>>(&self, path: P) -> Result<Self::File>
Opens a file at path
with options specified by self
.
§Errors
This function will return an error under a number of different circumstances, including but not limited to:
- Opening a file that does not exist without setting create or create_new.
- Attempting to open a file with access that the user lacks permissions for
- Filesystem-level errors (full disk, etc)
- Invalid combinations of open options (truncate without write access, no access mode set, etc)
§Examples
use rsfs::*;
use rsfs::mem::FS;
let fs = FS::new();
let file = fs.new_openopts().open("foo.txt")?;
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.