logo
pub struct File { /* private fields */ }
Expand description

An open file on the filesystem.

Depending on what options the file was opened with, this type can be used for reading and/or writing.

Files are automatically closed when they get dropped and any errors detected on closing are ignored. Use the sync_all method before dropping a file if such errors need to be handled.

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

Examples

Create a new file and write some bytes to it:

use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::create("a.txt").await?;
file.write_all(b"Hello, world!").await?;

Read the contents of a file into a vector of bytes:

use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::open("a.txt").await?;
let mut contents = Vec::new();
file.read_to_end(&mut contents).await?;

Implementations

Opens a file in read-only mode.

See the OpenOptions::open function for more options.

Errors

An error will be returned in the following situations:

  • path does not point to an existing file.
  • The current process lacks permissions to read the file.
  • Some other I/O error occurred.

For more details, see the list of errors documented by OpenOptions::open.

Examples
use async_std::fs::File;

let file = File::open("a.txt").await?;

Opens a file in write-only mode.

This function will create a file if it does not exist, and will truncate it if it does.

See the OpenOptions::open function for more options.

Errors

An error will be returned in the following situations:

  • The file’s parent directory does not exist.
  • The current process lacks permissions to write to the file.
  • Some other I/O error occurred.

For more details, see the list of errors documented by OpenOptions::open.

Examples
use async_std::fs::File;

let file = File::create("a.txt").await?;

Synchronizes OS-internal buffered contents and metadata to disk.

This function will ensure that all in-memory data reaches the filesystem.

This can be used to handle errors that would otherwise only be caught when the file is closed. When a file is dropped, errors in synchronizing this in-memory data are ignored.

Examples
use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::create("a.txt").await?;
file.write_all(b"Hello, world!").await?;
file.sync_all().await?;

Synchronizes OS-internal buffered contents to disk.

This is similar to sync_all, except that file metadata may not be synchronized.

This is intended for use cases that must synchronize the contents of the file, but don’t need the file metadata synchronized to disk.

Note that some platforms may simply implement this in terms of sync_all.

Examples
use async_std::fs::File;
use async_std::prelude::*;

let mut file = File::create("a.txt").await?;
file.write_all(b"Hello, world!").await?;
file.sync_data().await?;

Truncates or extends the file.

If size is less than the current file size, then the file will be truncated. If it is greater than the current file size, then the file will be extended to size and have all intermediate data filled with zeros.

The file’s cursor stays at the same position, even if the cursor ends up being past the end of the file after this operation.

Examples
use async_std::fs::File;

let file = File::create("a.txt").await?;
file.set_len(10).await?;

Reads the file’s metadata.

Examples
use async_std::fs::File;

let file = File::open("a.txt").await?;
let metadata = file.metadata().await?;

Changes the permissions on the file.

Errors

An error will be returned in the following situations:

  • The current process lacks permissions to change attributes on the file.
  • Some other I/O error occurred.
Examples
use async_std::fs::File;

let file = File::create("a.txt").await?;

let mut perms = file.metadata().await?.permissions();
perms.set_readonly(true);
file.set_permissions(perms).await?;

Trait Implementations

Extracts the raw file descriptor. Read more

Extracts the raw handle, without taking any ownership.

Attempt to read from the AsyncRead into buf. Read more

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more

Attempt to read from the AsyncRead into buf. Read more

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more

Attempt to seek to an offset, in bytes, in a stream. Read more

Attempt to seek to an offset, in bytes, in a stream. Read more

Attempt to write bytes from buf into the object. Read more

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more

Attempt to close the object. Read more

Attempt to write bytes from bufs into the object using vectored IO operations. Read more

Attempt to write bytes from buf into the object. Read more

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more

Attempt to close the object. Read more

Attempt to write bytes from bufs into the object using vectored IO operations. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Converts to this type from the input type.

Constructs a new instance of Self from the given raw file descriptor. Read more

Constructs a new I/O object from the specified raw handle. Read more

Consumes this object, returning the raw underlying file descriptor. Read more

Consumes this object, returning the raw underlying handle. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Reads some bytes from the byte stream. Read more

Like read(), except it reads into a slice of buffers. Read more

Reads the entire contents and appends them to a Vec. Read more

Reads the entire contents and appends them to a String. Read more

Reads the exact number of bytes required to fill buf. Read more

Creates an adapter which will read at most limit bytes from it. Read more

Converts this AsyncRead into a Stream of bytes. Read more

Creates an adapter which will chain this stream with another. Read more

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more

Seeks to a new position in a byte stream. Read more

Writes some bytes into the byte stream. Read more

Like write(), except that it writes a slice of buffers. Read more

Writes an entire buffer into the byte stream. Read more

Flushes the stream to ensure that all buffered contents reach their destination. Read more

Closes the writer. Read more

Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.