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

pub struct File { /* fields omitted */ }

A reference to a file on the filesystem.

An instance of a File can be read and/or written depending on what options it was opened with.

Files are automatically closed when they go out of scope. Errors detected on closing are ignored by the implementation of Drop. Use the method sync_all if these errors must be manually 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 Vec<u8>:

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 method for more details.

Errors

This function will return an error if path does not already exist. Other errors may also be returned according to 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 details.

Examples

use async_std::fs::File;

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

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

Attempts to synchronize all OS-internal metadata to disk.

This function will attempt to ensure that all in-memory data reaches the filesystem before returning.

This can be used to handle errors that would otherwise only be caught when the File is closed. Dropping a file will ignore errors in synchronizing this in-memory data.

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]

Similar to sync_all, except that it may not synchronize file metadata.

This is intended for use cases that must synchronize content, but don't need the metadata on disk. The goal of this method is to reduce disk operations.

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 underlying file.

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

The file's cursor isn't changed. In particular, if the cursor was at the end and the file is truncated using this operation, the cursor will now be past the end.

Errors

This function will return an error if the file is not opened for writing.

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]

Queries metadata about the file.

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 underlying file.

Errors

This function will return an error if the user lacks permission to change attributes on the underlying file, but may also return an error in other OS-specific cases.

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 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 From<File> for File[src]

fn from(file: File) -> File[src]

Converts a std::fs::File into its asynchronous equivalent.

impl Debug for File[src]

impl AsyncSeek for File[src]

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

impl AsyncWrite for File[src]

fn poll_write_vectored(
    self: Pin<&mut Self>,
    cx: &mut Context,
    bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
[src]

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

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

fn poll_write_vectored(
    self: Pin<&mut Self>,
    cx: &mut Context,
    bufs: &[IoSlice]
) -> Poll<Result<usize, Error>>
[src]

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

impl AsyncRead for File[src]

fn poll_read_vectored(
    self: Pin<&mut Self>,
    cx: &mut Context,
    bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
[src]

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

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

fn poll_read_vectored(
    self: Pin<&mut Self>,
    cx: &mut Context,
    bufs: &mut [IoSliceMut]
) -> Poll<Result<usize, Error>>
[src]

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

Auto Trait Implementations

impl Send for File

impl Unpin for File

impl Sync for File

impl UnwindSafe for File

impl RefUnwindSafe for File

Blanket Implementations

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

impl<T, U> Into<U> for T where
    U: From<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]

impl<S> AsyncSeekExt for S where
    S: AsyncSeek + ?Sized
[src]

Important traits for Seek<'_, S>
fn seek(&mut self, pos: SeekFrom) -> Seek<Self> where
    Self: Unpin
[src]

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more

impl<W> AsyncWriteExt for W where
    W: AsyncWrite + ?Sized
[src]

Important traits for Flush<'_, W>
fn flush(&mut self) -> Flush<Self> where
    Self: Unpin
[src]

Creates a future which will entirely flush this AsyncWrite. Read more

Important traits for Close<'_, W>
fn close(&mut self) -> Close<Self> where
    Self: Unpin
[src]

Creates a future which will entirely close this AsyncWrite.

Important traits for Write<'_, W>
fn write(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> where
    Self: Unpin
[src]

Creates a future which will write bytes from buf into the object. Read more

Important traits for WriteVectored<'_, W>
fn write_vectored(
    &'a mut self,
    bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
    Self: Unpin
[src]

Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more

Important traits for WriteAll<'_, W>
fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> where
    Self: Unpin
[src]

Write data into this object. Read more

fn into_sink<Item>(self) -> IntoSink<Self, Item> where
    Item: AsRef<[u8]>, 
[src]

Allow using an [AsyncWrite] as a Sink``<Item: AsRef<[u8]>>. Read more

impl<R> AsyncReadExt for R where
    R: AsyncRead + ?Sized
[src]

Important traits for CopyInto<'_, R, W>
fn copy_into<W>(self, writer: &mut W) -> CopyInto<Self, W> where
    W: AsyncWrite + Unpin + ?Sized
[src]

Creates a future which copies all the bytes from one object to another. Read more

Important traits for Read<'_, R>
fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
    Self: Unpin
[src]

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more

Important traits for ReadVectored<'_, R>
fn read_vectored(
    &'a mut self,
    bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
    Self: Unpin
[src]

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more

Important traits for ReadExact<'_, R>
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
    Self: Unpin
[src]

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more

Important traits for ReadToEnd<'_, A>
fn read_to_end(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self> where
    Self: Unpin
[src]

Creates a future which will read all the bytes from this AsyncRead. Read more

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
    Self: AsyncWrite
[src]

Helper method for splitting this read/write object into two halves. Read more