[][src]Struct async_std::fs::File

pub struct File { /* fields omitted */ }

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?;

Methods

impl File[src]

pub async fn open<P: AsRef<Path>>(path: P) -> Result<File>[src]

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?;

pub async fn create<P: AsRef<Path>>(path: P) -> Result<File>[src]

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?;

pub async fn sync_all<'_>(&'_ self) -> Result<()>[src]

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?;

pub async fn sync_data<'_>(&'_ self) -> Result<()>[src]

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?;

pub async fn set_len<'_>(&'_ self, size: u64) -> Result<()>[src]

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?;

pub async fn metadata<'_>(&'_ self) -> Result<Metadata>[src]

Reads the file's metadata.

Examples

use async_std::fs::File;

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

pub async fn set_permissions<'_>(&'_ self, perm: Permissions) -> Result<()>[src]

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

impl Read for File[src]

impl<'_> Read for &'_ File[src]

impl Seek for File[src]

impl<'_> Seek for &'_ File[src]

impl Write for File[src]

impl<'_> Write for &'_ File[src]

impl AsRawFd for File[src]

impl FromRawFd for File[src]

impl IntoRawFd for File[src]

impl AsRawHandle for File[src]

impl FromRawHandle for File[src]

impl IntoRawHandle for File[src]

impl Drop for File[src]

impl From<File> for File[src]

impl Debug for File[src]

Auto Trait Implementations

impl Send for File

impl Sync for File

impl Unpin for File

impl !UnwindSafe for File

impl !RefUnwindSafe for File

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]