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.
NOTE: If writing to a file, make sure to call
flush()
, sync_data()
,
or sync_all()
before dropping the file or else some written data
might get lost!
Examples
Create a new file and write some bytes to it:
use async_fs::File;
use futures_lite::io::AsyncWriteExt;
let mut file = File::create("a.txt").await?;
file.write_all(b"Hello, world!").await?;
file.flush().await?;
Read the contents of a file into a vector of bytes:
use async_fs::File;
use futures_lite::io::AsyncReadExt;
let mut file = File::open("a.txt").await?;
let mut contents = Vec::new();
file.read_to_end(&mut contents).await?;
Implementations
sourceimpl File
impl File
sourcepub async fn open<P>(path: P) -> Result<File, Error> where
P: AsRef<Path>,
pub async fn open<P>(path: P) -> Result<File, Error> where
P: AsRef<Path>,
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_fs::File;
let file = File::open("a.txt").await?;
sourcepub async fn create<P>(path: P) -> Result<File, Error> where
P: AsRef<Path>,
pub async fn create<P>(path: P) -> Result<File, Error> where
P: AsRef<Path>,
Opens a file in write-only mode.
This method 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_fs::File;
let file = File::create("a.txt").await?;
sourcepub async fn sync_all(&'_ self) -> Result<(), Error>
pub async fn sync_all(&'_ self) -> Result<(), Error>
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 by closing the file. When a file is dropped, errors in synchronizing this in-memory data are ignored.
Examples
use async_fs::File;
use futures_lite::io::AsyncWriteExt;
let mut file = File::create("a.txt").await?;
file.write_all(b"Hello, world!").await?;
file.sync_all().await?;
sourcepub async fn sync_data(&'_ self) -> Result<(), Error>
pub async fn sync_data(&'_ self) -> Result<(), Error>
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_fs::File;
use futures_lite::io::AsyncWriteExt;
let mut file = File::create("a.txt").await?;
file.write_all(b"Hello, world!").await?;
file.sync_data().await?;
sourcepub async fn set_len(&'_ self, size: u64) -> Result<(), Error>
pub async fn set_len(&'_ self, size: u64) -> Result<(), Error>
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_fs::File;
let mut file = File::create("a.txt").await?;
file.set_len(10).await?;
sourcepub async fn metadata(&'_ self) -> Result<Metadata, Error>
pub async fn metadata(&'_ self) -> Result<Metadata, Error>
Reads the file’s metadata.
Examples
use async_fs::File;
let file = File::open("a.txt").await?;
let metadata = file.metadata().await?;
sourcepub async fn set_permissions(&'_ self, perm: Permissions) -> Result<(), Error>
pub async fn set_permissions(&'_ self, perm: Permissions) -> Result<(), Error>
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_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
sourceimpl AsyncRead for File
impl AsyncRead for File
sourceimpl AsyncWrite for File
impl AsyncWrite for File
sourcefn poll_write(
self: Pin<&mut File>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_write(
self: Pin<&mut File>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
Attempt to write bytes from buf
into the object. Read more
sourcefn poll_flush(
self: Pin<&mut File>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
fn poll_flush(
self: Pin<&mut File>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
Auto Trait Implementations
impl !RefUnwindSafe for File
impl Send for File
impl Sync for File
impl Unpin for File
impl !UnwindSafe for File
Blanket Implementations
sourceimpl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
sourcefn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>ⓘNotable traits for ReadFuture<'_, R>impl<'_, R> Future for ReadFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>ⓘNotable traits for ReadFuture<'_, R>impl<'_, R> Future for ReadFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
Reads some bytes from the byte stream. Read more
sourcefn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘNotable traits for ReadVectoredFuture<'_, R>impl<'_, R> Future for ReadVectoredFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self>ⓘNotable traits for ReadVectoredFuture<'_, R>impl<'_, R> Future for ReadVectoredFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
sourcefn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self>ⓘNotable traits for ReadToEndFuture<'_, R>impl<'_, R> Future for ReadToEndFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self>ⓘNotable traits for ReadToEndFuture<'_, R>impl<'_, R> Future for ReadToEndFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
sourcefn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘNotable traits for ReadToStringFuture<'_, R>impl<'_, R> Future for ReadToStringFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self>ⓘNotable traits for ReadToStringFuture<'_, R>impl<'_, R> Future for ReadToStringFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
R: AsyncRead + Unpin + ?Sized, type Output = Result<usize, Error>;
sourcefn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>ⓘNotable traits for ReadExactFuture<'_, R>impl<'_, R> Future for ReadExactFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>ⓘNotable traits for ReadExactFuture<'_, R>impl<'_, R> Future for ReadExactFuture<'_, R> where
R: AsyncRead + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
R: AsyncRead + Unpin + ?Sized, type Output = Result<(), Error>;
Reads the exact number of bytes required to fill buf
. Read more
sourcefn take(self, limit: u64) -> Take<Self>
fn take(self, limit: u64) -> Take<Self>
Creates an adapter which will read at most limit
bytes from it. Read more
sourcefn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
Creates an adapter which will chain this stream with another. Read more
sourcefn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Boxes the reader and changes its type to dyn AsyncRead + Send + 'a
. Read more
sourceimpl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
impl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
sourceimpl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
sourcefn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>ⓘNotable traits for WriteFuture<'_, W>impl<'_, W> Future for WriteFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>ⓘNotable traits for WriteFuture<'_, W>impl<'_, W> Future for WriteFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
Writes some bytes into the byte stream. Read more
sourcefn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘNotable traits for WriteVectoredFuture<'_, W>impl<'_, W> Future for WriteVectoredFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self>ⓘNotable traits for WriteVectoredFuture<'_, W>impl<'_, W> Future for WriteVectoredFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<usize, Error>;
sourcefn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>ⓘNotable traits for WriteAllFuture<'_, W>impl<'_, W> Future for WriteAllFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>ⓘNotable traits for WriteAllFuture<'_, W>impl<'_, W> Future for WriteAllFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
Writes an entire buffer into the byte stream. Read more
sourcefn flush(&mut self) -> FlushFuture<'_, Self>ⓘNotable traits for FlushFuture<'_, W>impl<'_, W> Future for FlushFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>ⓘNotable traits for FlushFuture<'_, W>impl<'_, W> Future for FlushFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
Flushes the stream to ensure that all buffered contents reach their destination. Read more
sourcefn close(&mut self) -> CloseFuture<'_, Self>ⓘNotable traits for CloseFuture<'_, W>impl<'_, W> Future for CloseFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
fn close(&mut self) -> CloseFuture<'_, Self>ⓘNotable traits for CloseFuture<'_, W>impl<'_, W> Future for CloseFuture<'_, W> where
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
where
Self: Unpin,
W: AsyncWrite + Unpin + ?Sized, type Output = Result<(), Error>;
Closes the writer. Read more
sourcefn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a
. Read more
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more