Struct async_std::fs::OpenOptions

source ·
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.

This type is an async version of std::fs::OpenOptions.

§Examples

Open a file for reading:

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:

use async_std::fs::OpenOptions;

let file = OpenOptions::new()
    .read(true)
    .write(true)
    .create(true)
    .open("a.txt")
    .await?;

Implementations§

source§

impl OpenOptions

source

pub fn new() -> OpenOptions

Creates a blank set of options.

All options are initially set to false.

§Examples
use async_std::fs::OpenOptions;

let file = OpenOptions::new()
    .read(true)
    .open("a.txt")
    .await?;
source

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_std::fs::OpenOptions;

let file = OpenOptions::new()
    .read(true)
    .open("a.txt")
    .await?;
source

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_std::fs::OpenOptions;

let file = OpenOptions::new()
    .write(true)
    .open("a.txt")
    .await?;
source

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 operation.

§Examples
use async_std::fs::OpenOptions;

let file = OpenOptions::new()
    .append(true)
    .open("a.txt")
    .await?;
source

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_std::fs::OpenOptions;

let file = OpenOptions::new()
    .write(true)
    .truncate(true)
    .open("a.txt")
    .await?;
source

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_std::fs::OpenOptions;

let file = OpenOptions::new()
    .write(true)
    .create(true)
    .open("a.txt")
    .await?;
source

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_std::fs::OpenOptions;

let file = OpenOptions::new()
    .write(true)
    .create_new(true)
    .open("a.txt")
    .await?;
source

pub fn open<P: AsRef<Path>>( &self, path: P, ) -> impl Future<Output = Result<File>>

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 nor create_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 but write wasn’t, or none of read, write, and append 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_std::fs::OpenOptions;

let file = OpenOptions::new()
    .read(true)
    .open("a.txt")
    .await?;

Trait Implementations§

source§

impl Clone for OpenOptions

source§

fn clone(&self) -> OpenOptions

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for OpenOptions

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for OpenOptions

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl OpenOptionsExt for OpenOptions

source§

fn mode(&mut self, mode: u32) -> &mut Self

Sets the mode bits that a new file will be created with. Read more
source§

fn custom_flags(&mut self, flags: i32) -> &mut Self

Pass custom flags to the flags argument of open. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more