pub trait OpenOptions: Sized {
type File: File;
// Required methods
fn new() -> Self;
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<'life0, 'async_trait, P>(
&'life0 self,
path: P,
) -> Pin<Box<dyn Future<Output = Result<Self::File>> + Send + 'async_trait>>
where P: 'async_trait + AsRef<Path> + Send,
Self: 'async_trait,
'life0: 'async_trait;
}Expand description
An async abstraction over std::fs::OpenOptions.
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 using the tokio runtime:
use tokio::fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.open("a.txt")
.await?;Open a file for reading using the async_std runtime:
use async_std::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
using the tokio runtime:
use tokio::fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("a.txt")
.await?;Open a file for both reading and writing, and create it if it doesn’t exist yet
using the async_std runtime:
use async_std::fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("a.txt")
.await?;Required Associated Types§
Required Methods§
Sourcefn read(&mut self, read: bool) -> &mut Self
fn read(&mut self, read: bool) -> &mut Self
Configures the option for read mode.
When set to true, this option means the file will be readable after opening.
Sourcefn write(&mut self, write: bool) -> &mut Self
fn write(&mut self, write: bool) -> &mut Self
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.
Sourcefn append(&mut self, append: bool) -> &mut Self
fn append(&mut self, append: bool) -> &mut Self
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.
Sourcefn create_new(&mut self, create_new: bool) -> &mut Self
fn create_new(&mut self, create_new: bool) -> &mut Self
Sourcefn open<'life0, 'async_trait, P>(
&'life0 self,
path: P,
) -> Pin<Box<dyn Future<Output = Result<Self::File>> + Send + 'async_trait>>
fn open<'life0, 'async_trait, P>( &'life0 self, path: P, ) -> Pin<Box<dyn Future<Output = Result<Self::File>> + Send + 'async_trait>>
Opens a file with the configured options.
§Errors
An error will be returned in the following situations:
- The file does not exist and neither
createnorcreate_newwere 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_newwas set. - Invalid combination of options was used, like
truncatewas set butwritewasn’t, or none ofread,write, andappendmodes 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.
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.
Implementations on Foreign Types§
Source§impl OpenOptions for OpenOptions
Available on crate feature async-std-rt only.
impl OpenOptions for OpenOptions
async-std-rt only.type File = File
fn new() -> Self
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<'life0, 'async_trait, P>( &'life0 self, path: P, ) -> Pin<Box<dyn Future<Output = Result<Self::File>> + Send + 'async_trait>>
Source§impl OpenOptions for OpenOptions
Available on crate feature tokio-rt only.
impl OpenOptions for OpenOptions
tokio-rt only.