pub struct OpenOptions { /* private fields */ }
Expand description
Options to configure how an AtomicWriteFile
is opened.
This struct mimics the standard struct std::fs::OpenOptions
, and offers a subset of its
features that are applicable to AtomicWriteFile
.
Options can be set using methods like read()
. Once the desired options
are set, the file can be opened using open()
.
This crate offers some platform-specific extensions for OpenOptions
in the form of traits:
§Notable differences between std::fs::OpenOptions
and atomic_write_file::OpenOptions
The OpenOptions
provided in this crate opens all files for writing by default, and the opened
file is always initially empty (“truncated”). As such, the following methods are not provided:
write()
, truncate()
, append()
.
create()
is not provided because a new file is always created if an original file does not
exist.
create_new()
is also not provided because there is no way to ensure that a file never exists
from the time an AtomicWriteFile
is opened to the time it is committed.
§Behavior when opening a file that already exists
When passing a path to open()
that points to a file that already exists,
AtomicWriteFile
may preserve some of the metadata of the existing file (permissions,
ownership, and more). This behavior is platform-specific and can be controlled using the
platform-specific OpenOptionsExt
traits. See also the “notes and limitations” section on the
module-level documentations for more information about what
metadata is preserved, what is not preserved, and in what circumstances.
§Examples
Opening a file for writing with default options (equivalent to a call to
AtomicWriteFile::open()
):
use atomic_write_file::OpenOptions;
let file = OpenOptions::new().open("foo.txt");
Opening a file for both reading and writing:
use atomic_write_file::OpenOptions;
let file = OpenOptions::new().read(true).open("foo.txt");
Implementations§
Source§impl OpenOptions
impl OpenOptions
Sourcepub fn read(&mut self, read: bool) -> &mut Self
pub fn read(&mut self, read: bool) -> &mut Self
Sets the option for read access.
If true
, the file will be readable (other than being writeable) once opened using, for
example, the Read
trait. Note that if opening an already-existing file, the original
file contents will not be readable. Only the new contents of the file will be readable.
If false
(the default), the file is opened in write-only mode.
§Examples
use std::io::Seek;
use std::io::Write;
use std::io::read_to_string;
use atomic_write_file::OpenOptions;
let mut file = OpenOptions::new().read(true).open("foo.txt")?;
writeln!(file, "hello")?;
file.rewind()?;
assert_eq!(read_to_string(&file)?, "hello\n");
Sourcepub fn open<P: AsRef<Path>>(&self, path: P) -> Result<AtomicWriteFile>
pub fn open<P: AsRef<Path>>(&self, path: P) -> Result<AtomicWriteFile>
Opens the file at path
with this set of options.
This has the same semantics as std::fs::OpenOptions::open()
, except that it returns an
AtomicWriteFile
instead of a File
.
§Examples
use atomic_write_file::OpenOptions;
let file = OpenOptions::new().read(true).open("foo.txt");
Trait Implementations§
Source§impl Clone for OpenOptions
impl Clone for OpenOptions
Source§fn clone(&self) -> OpenOptions
fn clone(&self) -> OpenOptions
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for OpenOptions
impl Debug for OpenOptions
Source§impl Default for OpenOptions
impl Default for OpenOptions
Source§impl OpenOptionsExt for OpenOptions
impl OpenOptionsExt for OpenOptions
Source§impl OpenOptionsExt for OpenOptions
impl OpenOptionsExt for OpenOptions
Source§fn preserve_mode(&mut self, preserve_mode: bool) -> &mut Self
fn preserve_mode(&mut self, preserve_mode: bool) -> &mut Self
Source§fn preserve_owner(&mut self, preserve_owner: bool) -> &mut Self
fn preserve_owner(&mut self, preserve_owner: bool) -> &mut Self
Source§fn try_preserve_owner(&mut self, try_preserve_owner: bool) -> &mut Self
fn try_preserve_owner(&mut self, try_preserve_owner: bool) -> &mut Self
OpenOptions::open()
silently continues suppressing the error. Read more