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§
§impl File
impl File
pub async fn open<P>(path: P) -> Result<File, Error>
pub async fn open<P>(path: P) -> Result<File, Error>
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?;
pub async fn create<P>(path: P) -> Result<File, Error>
pub async fn create<P>(path: P) -> Result<File, Error>
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?;
pub 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?;
pub 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?;
pub 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?;
pub 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?;
pub 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§
§impl AsyncRead for File
impl AsyncRead for File
§impl AsyncWrite for File
impl AsyncWrite for File
§fn 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>>
buf
into the object. Read more§fn 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>>
Auto Trait Implementations§
impl !Freeze for File
impl !RefUnwindSafe for File
impl Send for File
impl Sync for File
impl Unpin for File
impl !UnwindSafe for File
Blanket Implementations§
§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read more§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read more§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where
Self: Unpin,
§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectoredFuture<'a, Self>where
Self: Unpin,
§fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> ReadToEndFuture<'a, Self>where
Self: Unpin,
§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToStringFuture<'a, Self>where
Self: Unpin,
§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where
Self: Unpin,
buf
. Read more§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read more§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
buf
in asynchronous
manner, returning a future type. Read more§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
AsyncRead
into bufs
using vectored
IO operations. Read more§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
buf
,
returning an error if end of file (EOF) is hit sooner. Read more§fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
AsyncRead
. Read more§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
AsyncRead
. Read more§fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)where
Self: Sized + AsyncWrite,
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)where
Self: Sized + AsyncWrite,
§impl<S> AsyncSeekExt for S
impl<S> AsyncSeekExt for S
§impl<S> AsyncSeekExt for S
impl<S> AsyncSeekExt for S
§impl<S> AsyncSeekExt for S
impl<S> AsyncSeekExt for S
§fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self>where
Self: Unpin,
fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self>where
Self: Unpin,
§fn stream_position(&mut self) -> Seek<'_, Self>where
Self: Unpin,
fn stream_position(&mut self) -> Seek<'_, Self>where
Self: Unpin,
§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
§fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
§fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
§fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
§fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
dyn AsyncWrite + Send + 'a
. Read more§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
§fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where
Self: Unpin,
§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectoredFuture<'a, Self>where
Self: Unpin,
§fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where
Self: Unpin,
§fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> FlushFuture<'_, Self>where
Self: Unpin,
§fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a>>
dyn AsyncWrite + Send + 'a
. Read more§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
§fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
AsyncWrite
. Read more§fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
AsyncWrite
.§fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
buf
into the object. Read more§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectored<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectored<'a, Self>where
Self: Unpin,
bufs
into the object using vectored
IO operations. Read moresource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> FsChangeNote for T
impl<T> FsChangeNote for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more