Struct zng_task::fs::OpenOptions
pub struct OpenOptions(/* private fields */);
Expand description
A builder for opening files with configurable options.
Files can be opened in read
and/or
write
mode.
The append
option opens files in a special writing mode that
moves the file cursor to the end of file before every write operation.
It is also possible to truncate
the file right after opening,
to create
a file if it doesn’t exist yet, or to always create a
new file with create_new
.
§Examples
Open a file for reading:
use async_fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.open("a.txt")
.await?;
Open a file for both reading and writing, and create it if it doesn’t exist yet:
use async_fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("a.txt")
.await?;
Implementations§
§impl OpenOptions
impl OpenOptions
pub fn new() -> OpenOptions
pub fn new() -> OpenOptions
Creates a blank set of options.
All options are initially set to false
.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.open("a.txt")
.await?;
pub fn read(&mut self, read: bool) -> &mut OpenOptions
pub fn read(&mut self, read: bool) -> &mut OpenOptions
Configures the option for read mode.
When set to true
, this option means the file will be readable after opening.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.open("a.txt")
.await?;
pub fn write(&mut self, write: bool) -> &mut OpenOptions
pub fn write(&mut self, write: bool) -> &mut OpenOptions
Configures the option for write mode.
When set to true
, this option means the file will be writable after opening.
If the file already exists, write calls on it will overwrite the previous contents without truncating it.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.write(true)
.open("a.txt")
.await?;
pub fn append(&mut self, append: bool) -> &mut OpenOptions
pub fn append(&mut self, append: bool) -> &mut OpenOptions
Configures the option for append mode.
When set to true
, this option means the file will be writable after opening and the file
cursor will be moved to the end of file before every write operaiton.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.append(true)
.open("a.txt")
.await?;
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions
Configures the option for truncating the previous file.
When set to true
, the file will be truncated to the length of 0 bytes.
The file must be opened in write
or
append
mode for truncation to work.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.write(true)
.truncate(true)
.open("a.txt")
.await?;
pub fn create(&mut self, create: bool) -> &mut OpenOptions
pub fn create(&mut self, create: bool) -> &mut OpenOptions
Configures the option for creating a new file if it doesn’t exist.
When set to true
, this option means a new file will be created if it doesn’t exist.
The file must be opened in write
or
append
mode for file creation to work.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.write(true)
.create(true)
.open("a.txt")
.await?;
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions
Configures the option for creating a new file or failing if it already exists.
When set to true
, this option means a new file will be created, or the open operation
will fail if the file already exists.
The file must be opened in write
or
append
mode for file creation to work.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.write(true)
.create_new(true)
.open("a.txt")
.await?;
pub fn open<P>(&self, path: P) -> impl Future<Output = Result<File, Error>>
pub fn open<P>(&self, path: P) -> impl Future<Output = Result<File, Error>>
Opens a file with the configured options.
§Errors
An error will be returned in the following situations:
- The file does not exist and neither
create
norcreate_new
were set. - The file’s parent directory does not exist.
- The current process lacks permissions to open the file in the configured mode.
- The file already exists and
create_new
was set. - Invalid combination of options was used, like
truncate
was set butwrite
wasn’t, or none ofread
,write
, andappend
modes was set. - An OS-level occurred, like too many files are open or the file name is too long.
- Some other I/O error occurred.
§Examples
use async_fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.open("a.txt")
.await?;
Trait Implementations§
§impl Clone for OpenOptions
impl Clone for OpenOptions
§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 more§impl Debug for OpenOptions
impl Debug for OpenOptions
§impl Default for OpenOptions
impl Default for OpenOptions
§fn default() -> OpenOptions
fn default() -> OpenOptions
§impl OpenOptionsExt for OpenOptions
impl OpenOptionsExt for OpenOptions
§fn mode(&mut self, mode: u32) -> &mut OpenOptions
fn mode(&mut self, mode: u32) -> &mut OpenOptions
§fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions
fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions
Auto Trait Implementations§
impl Freeze for OpenOptions
impl RefUnwindSafe for OpenOptions
impl Send for OpenOptions
impl Sync for OpenOptions
impl Unpin for OpenOptions
impl UnwindSafe for OpenOptions
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more