[−]Struct heim_runtime::fs::File
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()
][futures_lite::io::AsyncWriteExt::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
pub async fn open<P>(path: P) -> Result<File, Error> where
P: AsRef<Path>,
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?;
pub async fn create<P>(path: P) -> Result<File, Error> where
P: AsRef<Path>,
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?;
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>
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>
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>
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>
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 AsRawFd for File
impl AsyncRead for File
pub fn poll_read(
self: Pin<&mut File>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
self: Pin<&mut File>,
cx: &mut Context<'_>,
buf: &mut [u8]
) -> Poll<Result<usize, Error>>
pub fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>]
) -> Poll<Result<usize, Error>>
impl AsyncSeek for File
pub fn poll_seek(
self: Pin<&mut File>,
cx: &mut Context<'_>,
pos: SeekFrom
) -> Poll<Result<u64, Error>>
self: Pin<&mut File>,
cx: &mut Context<'_>,
pos: SeekFrom
) -> Poll<Result<u64, Error>>
impl AsyncWrite for File
pub fn poll_write(
self: Pin<&mut File>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
self: Pin<&mut File>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
pub fn poll_flush(
self: Pin<&mut File>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
self: Pin<&mut File>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
pub fn poll_close(
self: Pin<&mut File>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
self: Pin<&mut File>,
cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
pub fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
impl Debug for File
impl From<File> for File
impl FromRawFd for File
pub unsafe fn from_raw_fd(raw: i32) -> File
Auto Trait Implementations
impl !RefUnwindSafe for File
[src]
impl Send for File
[src]
impl Sync for File
[src]
impl Unpin for File
[src]
impl !UnwindSafe for File
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
pub fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self> where
Self: Unpin,
pub fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
pub fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToString<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut String
) -> ReadToString<'a, Self> where
Self: Unpin,
pub fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>) where
Self: AsyncWrite,
[src]
Self: AsyncWrite,
pub fn take(self, limit: u64) -> Take<Self>
[src]
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
R: AsyncRead + ?Sized,
pub fn read(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
pub fn read_vectored(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectoredFuture<'a, Self> where
Self: Unpin,
pub fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEndFuture<'a, Self> where
Self: Unpin,
pub fn read_to_string(
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
&'a mut self,
buf: &'a mut String
) -> ReadToStringFuture<'a, Self> where
Self: Unpin,
pub fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
pub fn take(self, limit: u64) -> Take<Self>
pub fn bytes(self) -> Bytes<Self>
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
R: AsyncRead,
pub fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + 'a + Send, Global>> where
Self: Send + 'a,
Self: Send + 'a,
impl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
[src]
S: AsyncSeek + ?Sized,
impl<S> AsyncSeekExt for S where
S: AsyncSeek + ?Sized,
S: AsyncSeek + ?Sized,
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
[src]
W: AsyncWrite + ?Sized,
pub fn flush(&mut self) -> Flush<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn close(&mut self) -> Close<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self> where
Self: Unpin,
pub fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn into_sink<Item>(self) -> IntoSink<Self, Item> where
Item: AsRef<[u8]>,
[src]
Item: AsRef<[u8]>,
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
W: AsyncWrite + ?Sized,
pub fn write(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
pub fn write_vectored(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self> where
Self: Unpin,
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectoredFuture<'a, Self> where
Self: Unpin,
pub fn write_all(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self> where
Self: Unpin,
Self: Unpin,
pub fn flush(&mut self) -> FlushFuture<'_, Self> where
Self: Unpin,
Self: Unpin,
pub fn close(&mut self) -> CloseFuture<'_, Self> where
Self: Unpin,
Self: Unpin,
pub fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + 'a + Send, Global>> where
Self: Send + 'a,
Self: Send + 'a,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,